0

I am creating two sockets: a client socket and a server socket.

ServerSock class:

public class ServerSock {

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

        //creating welcome socket @param: port number
        System.out.println("Server is started...");
        ServerSocket serverSocket = new ServerSocket(55555);

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

        //creating individual sockets for clients
        Socket socket0 = serverSocket.accept();

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

        //reading the input data
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket0.getInputStream()));

        //reading the data from bufferedReader
        String str = bufferedReader.readLine();

        //console the data
        System.out.println(" client data : " + str);
    }
}

ClientSocket class:

public class ClientSocket {

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

        //ip address of the server
        String ip = "localhost";
        //port number of the application
        int port = 55555;

        //creating a socket
        Socket socket = new Socket(ip, port);

        //data to send
        String data0 = "lakshan waruna";

        //converting data to ByteStream param: where to send the data
        OutputStreamWriter os = new OutputStreamWriter(socket.getOutputStream());

        //writing the data
        os.write(data0);
        os.flush();
    }
}

ServerSock runs until I start the client socket. It also prints the "client connected" statement in the ServerSock class. But when it tries to read data from bufferedReader using the statement String str = bufferedReader.readLine();, it throws a SocketException stating "connection reset". How can I fix this issue?

DevLaka
  • 193
  • 3
  • 13
  • 1
    You failed to close the socket in the client. This will result in resetting the conneciton on some operating systems, i.e. Windows. You also failed to close anything in the server class. And finally you are reading lines but you aren't writing lines. – user207421 Apr 13 '19 at 07:23

2 Answers2

1

Your problem is you don't close the Socket in the client, "Connection reset" is usually thrown when a remote Socket has ungracefully closed a connection which is the case here since you don't call Socket.close(). If you do you'll notice it will work but you're probably thinking well why does that fix it? Why not just give me the data then next read tell me the Socket has closed, well it does but since you're using readLine() which constantly reads until it has received \n or \r or if the given InputStream returns less than zero in read. Pretty much it is reading until it has received a full line (to declare a full line like System.out.println does you just add \n at the end), and it has still yet to find a new line, if you add \n at the end of the message you'll notice it will also work.

Also, remove PrintWriter, you never use it.

ALWAYS CLOSE A SOCKET.

OughtToPrevail
  • 380
  • 3
  • 10
-2

Tested your code, worked for me.

Edited typo and included link to plausible information on question

Possible reasons for connection reset could be that the socket is closed before sending the data.

java.net.SocketException: Connection reset

simptri
  • 80
  • 9
  • There is no connection timeout here, and the socket isn't closed before sending the data. – user207421 Apr 13 '19 at 07:23
  • meant to write reset..nonetheless, the code worked for me..it could be an issue with the tcp protocol.. – simptri Apr 13 '19 at 07:39
  • 1
    The socket *isn't* closed before sending the data: in fact it isn't closed at all; and closing a socket before sending data doesn't produce 'connection reset', it produces 'socket closed'; so that isn't a plausible explanation for anything here. 'Issue with the TCP protocol' is too vague to even comment on. – user207421 Apr 13 '19 at 08:17
  • *Tested your code, worked for me* .... It could not have worked, for the simple reason that the server is waiting to read a line-terminator sequence of characters and the client never sends any. – President James K. Polk Apr 13 '19 at 13:18
  • @JamesKPolk You are mistaken. An unterminated line at end of stream is read by `readKine()`. – user207421 Apr 13 '19 at 20:24
  • @user207421: I just tested it and you are correct. But the javadocs don't seem to allow this behavior. – President James K. Polk Apr 13 '19 at 21:42