Results 1 to 2 of 2
  1. #1
    veerusoft is offline Member
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Default Socketexception :Connection reset

    I got the below exception .how to resolve this .It's receiving the data up to 10 min then suddenly below error happening
    I have embedded the source code below

    Kindly help me it's very urgent

    java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at MultiThreadedSocketServer$ClientServiceThread.run( MultiThreadedSocket
    Server.java:130)



    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;


    public class MultiThreadedSocketServer {

    ServerSocket myServerSocket;
    boolean ServerOn = true;


    public MultiThreadedSocketServer()
    {
    try
    {
    myServerSocket = new ServerSocket(5020);
    }
    catch(IOException ioe)
    {
    System.out.println("Could not create server socket on port 5020. Quitting.");
    System.exit(-1);
    }
    Calendar now = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
    System.out.println("It is now : " + formatter.format(now.getTime()));




    // Successfully created Server Socket. Now wait for connections.
    while(ServerOn)
    {
    try
    {
    // Accept incoming connections.
    Socket clientSocket = myServerSocket.accept();

    // accept() will block until a client connects to the server.
    // If execution reaches this point, then it means that a client
    // socket has been accepted.

    // For each client, we will start a service thread to
    // service the client requests. This is to demonstrate a
    // Multi-Threaded server. Starting a thread also lets our
    // MultiThreadedSocketServer accept multiple connections simultaneously.

    // Start a Service thread

    ClientServiceThread cliThread = new ClientServiceThread(clientSocket);
    cliThread.start();

    }
    catch(IOException ioe)
    {
    System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
    ioe.printStackTrace();
    }

    }

    try
    {
    myServerSocket.close();
    System.out.println("Server Stopped");
    }
    catch(Exception ioe)
    {
    System.out.println("Problem stopping server socket");
    System.exit(-1);
    }



    }

    public static void main (String[] args)
    {
    new MultiThreadedSocketServer();
    }


    class ClientServiceThread extends Thread
    {
    Socket myClientSocket;
    boolean m_bRunThread = true;

    public ClientServiceThread()
    {
    super();
    }

    ClientServiceThread(Socket s)
    {
    myClientSocket = s;

    }

    public void run()
    {
    // Obtain the input stream and the output stream for the socket
    // A good practice is to encapsulate them with a BufferedReader
    // and a PrintWriter as shown below.
    BufferedReader in = null;
    PrintWriter out = null;


    // Print out details of this connection
    System.out.println("Accepted Client Address - " + myClientSocket.getInetAddress().getHostName());

    try
    {
    in = new BufferedReader(new InputStreamReader(myClientSocket.getInputStream()) );
    out = new PrintWriter(new OutputStreamWriter(myClientSocket.getOutputStream( )));

    // At this point, we can read for input and reply with appropriate output.

    // Run in a loop until m_bRunThread is set to false
    while(m_bRunThread)
    {


    // read incoming stream
    String clientCommand = in.readLine();

    System.out.println("Client Says :" + clientCommand);




    if(!ServerOn)
    {
    // Special command. Quit this thread
    System.out.print("Server has already stopped");
    out.println("Server has already stopped");
    out.flush();
    m_bRunThread = false;

    }

    if(clientCommand.equalsIgnoreCase("quit")) {
    // Special command. Quit this thread
    m_bRunThread = false;
    System.out.print("Stopping client thread for client : ");
    } else if(clientCommand.equalsIgnoreCase("end")) {
    // Special command. Quit this thread and Stop the Server
    m_bRunThread = false;
    System.out.print("Stopping client thread for client : ");
    ServerOn = false;
    } else {
    // Process it
    out.println("Server Says : " + clientCommand);
    out.flush();
    }
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    finally
    {
    // Clean up
    try
    {
    in.close();
    out.close();
    myClientSocket.close();
    System.out.println("...Stopped");
    }
    catch(IOException ioe)
    {
    ioe.printStackTrace();
    }
    }
    }


    }
    }
    Last edited by veerusoft; 05-13-2014 at 05:09 PM.

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Socketexception :Connection reset

    Please wrap your code with [code] tags so that it is easier to read, e.g.,
    [code]
    // your code here
    [/code]

    It preserves formatting and provides syntax highlighting, e.g.,
    Java Code:
    // your code here
    Problems like this are quite difficult to diagnose. It is likely that the problem is on the network level due to an abnormal termination of the network connection, but there is no info on the cause of the problem. An Internet search using the search term "java.net.SocketException: Connection reset" will return relevant results, e.g.,

    If you have access to the client application, check if there is a corresponding exception there. Alternatively, you can write your own client to see if the problem can be reproduced.

    Since you mentioned that "it's receiving the data up to 10 min," check also if there is an intermediary between the client and the server such as a firewall and/or a proxy. A firewall, for instance, may be configured to terminate a network connection that is up beyond a set period, so problems like this have been known to happen during large file transfers.

    If there is a firewall, see if the problem can be reproduced with the firewall switched of. If there is a proxy, see if it'll be possible for the client-server connection to be established bypassing the proxy.

    If you or your colleagues have sufficient networking knowledge, or are not afraid of doing low-level investigation, you can try using a network packet analyser such as Wireshark, snoop or tcpdump, to find out what's happening on the network level when this problem occurs. See Comparison of packet analyzers - Wikipedia, the free encyclopedia for a list of packet analysers.

    Good luck!

Similar Threads

  1. java.net.SocketException: Connection reset
    By mania in forum Networking
    Replies: 1
    Last Post: 10-19-2013, 06:42 PM
  2. java.net.SocketException: Connection reset
    By mania in forum Threads and Synchronization
    Replies: 0
    Last Post: 10-08-2013, 07:34 PM
  3. Replies: 1
    Last Post: 02-01-2013, 04:55 AM
  4. Replies: 1
    Last Post: 11-22-2012, 06:04 PM
  5. Replies: 2
    Last Post: 01-24-2010, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •