Question

I am making a chat room using simple socket-socket connections. I have a server and client program. The server runs on port 225, and then when I run the client on port 225 so that they can read/write to the sockets, the client instantly stops with the error message

java.net.SocketException: Connection resetJava Result: 1

Why is it throwing this exception? It connects, the line is printed to console, as shown:

try {
    client = new Socket(serverAdress, portNumber);
    System.out.println("Successful connection to server on port " + portNumber + ".");

So why might it not be able to connect?

This is the exception trace...

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at schoolroom10.SchoolRoomClient.SchoolRoomClientWindow.main(SchoolRoomClientWindow.java:85)

This is my Server code...

public class SchoolRoomServer {

    // 'server' will create the server over the local port:
    static ServerSocket serverSocket = null;

    //Socket for listening:
    static Socket clientcommunicate = null;

    //portnum is the number of the local port. Change to required port before running.
    static int portnum = 225;

    public static void main(String[] args) throws IOException{
        try {
            serverSocket = new ServerSocket(portnum);
        clientcommunicate = serverSocket.accept();
            Communicate c = new Communicate(Communicate.server);
            Thread t = new Thread(c);
            t.start();
        } catch (UnknownHostException uhe) {
            System.err.println(uhe);
            System.exit(1);
        } catch (IOException ioe) {
            System.err.println(ioe);
            System.exit(1);
        }


    } 
}

class Communicate implements Runnable {
    public static Socket server;
    Communicate(Socket server) {
        Communicate.server = server;
    }
    public void run() {
        String in = "";
        try {
            //i/o for clients:
            PrintStream output = new PrintStream(server.getOutputStream());
            BufferedReader input = new BufferedReader(new InputStreamReader(server.getInputStream()));
            String message;

            //Message handling:
            while(((message = input.readLine()) != null) && (!in.equals("."))) {
                output.println(message);
            }
        } catch (IOException ioe) {
            System.err.println(ioe);
        }
    }
}
Was it helpful?

Solution

'Connection reset' usually means you have written to a connection that has already been closed by the other end. Generally speaking this is an applicaton protocol error. However in some circumstances, e.g. where the peer is a browser, or possibly in your case too, it is just an inevitable consequence of the fact that the peer may choose to go away at any time. In these cases, all you can do is close the socket and forget about the connection.

OTHER TIPS

As previous posters have pointed out, you should avoid using ports below 1024 as these are reserved.

Without seeing all your code the rest is just guesswork, however on the client end you should be entering a loop to constantly ready from the input stream and process incoming messages, then a brief pause before repeating, e.g. sleep(100). Same server end for monitoring for connection requests and incoming messages.

If you don't keep reading the input stream, it will fill the buffer and drop the connection with an exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top