1

I am learning Socket Programming in Java. I am getting java.net.SocketException: Connection reset.

Client Side Code

    package com.socket;

    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.Socket;

    public class ClientSock {

        public static void main(String[] args) throws Exception {

            Socket skt = new Socket("localhost", 8888);     
            String str = "Hello Server";
            OutputStreamWriter osw = new OutputStreamWriter(skt.getOutputStream()); 

            PrintWriter out = new PrintWriter(osw);
            osw.write(str);
            osw.flush();        
        }
    }

//Server Side Code:

    package com.socket;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class ServerSock {

        public static void main(String[] args) throws Exception {

            System.out.println("Server is Started");
            ServerSocket ss = new ServerSocket(8888);

            Socket s = ss.accept();

            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String str = br.readLine();

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

Here is my console after run client code, I am getting Exception Connection reset, where I am doing wrong?

 Server is Started
Exception in thread "main" 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 com.socket.ServerSock.main(ServerSock.java:19)
halfer
  • 19,824
  • 17
  • 99
  • 186
Swapnil P.
  • 21
  • 1
  • 2
  • 6
  • Possible duplicate of [What's causing my java.net.SocketException: Connection reset?](http://stackoverflow.com/questions/585599/whats-causing-my-java-net-socketexception-connection-reset) – halfer Apr 14 '17 at 07:48
  • [Lots of other potential duplicates here](https://stackoverflow.com/search?q=java.net.SocketException%3A+Connection+reset). – halfer Apr 14 '17 at 07:48

2 Answers2

3

"Exception in thread "main" java.net.SocketException: Connection reset" error occurs when the opponent is forcibly terminated without calling close().

Add this line to ClientSock

skt.close();

and I recommend this too to ServerSock.

ss.close();

java.io.Closeable implementing object must call close ().

  • 1
    skt.close will remove the exception but not the root cause of the exception. There can be multiple casues of SocektException – Vineet Kasat Apr 14 '17 at 07:49
  • Yes, exception gone!!! I close both Socket and ServerSocket. Well Thanks @Seongmin Gwon and Vineet Kasat. – Swapnil P. Apr 14 '17 at 11:22
0

You forgot "\n" in your "Hello Server".
Reader can't get the full line and throws this exception.

oneat
  • 10,778
  • 16
  • 52
  • 70