0

I am trying to execute a simple Client/Server program.But whenever i am trying to execute , i am getting "java.net.SocketException: Connection reset" . why this exception occurs ? Any suggestion for me ?

Here is the code

SocServer.java

public static void main(String[] args) throws Exception
{
    System.out.println("Server is started");

    ServerSocket ss = new ServerSocket(8886);

    System.out.println("Server is waiting for client request");

    Socket s = ss.accept();

    System.out.println("Client Connected");

    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

    String str = br.readLine();

    System.out.println("Client Data" + str);

}

SocketClient.java

public static void main(String[] args) throws Exception 
{
    String ip = "localhost";
    int port = 8886;
    Socket s = new Socket(ip,port);

    String str = "Noman";

    OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
    PrintWriter out = new PrintWriter(os);

    os.write(str);
    os.flush();
}
2
  • 2 things: 1) you are assuming all of the data is present and accounted for on the readLine in 1SocServer.java` and 2) you never close any of the streams. Please review this: stackoverflow.com/questions/62929/… Commented May 2, 2016 at 11:25
  • You do not send a newline while the listener is expecting one to unblock. Outside the completely unused PrintWriter (I discourage to use it ... better explicitly send newlines as they are expected). For a start try String str = "Noman\n" Commented May 2, 2016 at 11:40

1 Answer 1

0

Possible reasons

  • The other end has deliberately reset the connection, in a way which I will not document here. It is rare, and generally incorrect, for application software to do this, but it is not unknown for commercial software

.

  • More commonly, it is caused by writing to a connection that the other end has already closed normally. In other words an application protocol error.
  • It can also be caused by closing a socket when there is unread data in the socket receive buffer.
  • In Windows, 'software caused connection abort', which is not the same as 'connection reset', is caused by network problems sending from your end. There's a Microsoft knowledge base article about this.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.