Search code examples
javasocketsserverbufferedreaderserversocket

How to let a server receive more than a single message from clients?


I want to have a Server that is running and receives messages from Clients such as another Java Applications. I am doing this via BufferedReader with an InputStream and as long as i do it a single time it works as expected. The message gets processed by the method and writes the Test Message of the received message on the screen, but if i let it run in a while loop it says -

java.net.SocketException: Connection reset

So once the Server got a message i dont know how to get a second one, or any following one.

My main source code is:

public static void main (String[] args)  {

    int port = 13337;
    BufferedReader msgFromClient = null;
    PrintWriter msgToClient = null;

    timeDate td = new timeDate(); //Class i did for myself to get time/date

    ServerSocket s_socket = null;
    try {
        s_socket = new ServerSocket(port);
        System.out.println("Server startet at "+td.getCurrDate()+" "+td.getCurrTime());
    }
    catch (IOException ioe) {
        System.out.println("Server on Port "+port+" couldnt be created. \nException: "+ioe.getMessage());
    }

    Socket socket = null;
    try {
        socket = s_socket.accept();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        msgFromClient = utils.createInputStream(socket);
    } catch (IOException ioe) {
        System.out.println("Creation of an Input Stream failed.\n Exception - "+ioe);
    }
    try {
        msgToClient   = utils.createOutputStream(socket);
    } catch (IOException ioe) {
        System.out.println("Creation of an Output Stream failed.\n Exception - "+ioe);
    } 

    String input = null;
    while (true) {
            try {
                input = msgFromClient.readLine();
            } catch (IOException ioe) {
                System.out.println(ioe);
            }
            if(input!=null) {
                System.out.println("Jumping out of loop: "+input);
                utils.processCode(input);
            }
    } 

The both classes to create the streams look like this:

public static BufferedReader createInputStream (Socket socket) throws IOException {

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

    return br;
}

public static PrintWriter createOutputStream (Socket socket) throws IOException {

    PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);

    return pw;
}   

The "processCode" class then simply is a switch.


Solution

  • Socket socket = null;
    try {
        socket = s_socket.accept();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    You only accept one Connection an after this you are doing your handling. You need to open an new Thread for every connection

    ExecutorService threadLimit = Executors.newFixedThreadPool(10);
        while(true)  {
            Socket s = serverSocket.accept();
            treadLimit.submit(new HandleThread(s));
        }