2

I am trying to create a simple TCP server and client. I want the client to be able to send multiple messages by only opening the socket once. I have looked at similar questions here, here, and here but they haven't been much use.

My code is a follows:

SampleServerTCP.java

public class SampleServerTCP {
    private static final int DEFAULT_PORT_NUMBER = 39277;

    public static void main(String[] args) throws IOException {
        ServerSocket defaultSocket = new ServerSocket(DEFAULT_PORT_NUMBER);

        System.out.println("Listening on port: " + DEFAULT_PORT_NUMBER);
        while (true){
            Socket connectionSocket = defaultSocket.accept();
            BufferedReader fromClient= new BufferedReader(new     InputStreamReader(connectionSocket.getInputStream()));
            String msg = fromClient.readLine();
            System.out.println("Recieved: " + msg);
        }
    }
}

TCPClientTest.java

public class TCPClientTest {

    public static void main(String args[]) throws UnknownHostException, IOException, InterruptedException{
        Socket clientSocket = new Socket("localhost", 39277); 
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

        int c = 0;
        while(c<10){
            outToServer.writeBytes(c + "\n");
            outToServer.flush();
            c++;
            Thread.sleep(500);
        }
        clientSocket.close();
    }
}

The only output I get is:

Listening on port: 39277
Recieved: 0

Where am I going wrong?

Community
  • 1
  • 1
rj93
  • 523
  • 8
  • 25

2 Answers2

2

Your problem lies here:

ServerSocket defaultSocket = new ServerSocket(DEFAULT_PORT_NUMBER);

    System.out.println("Listening on port: " + DEFAULT_PORT_NUMBER);
    while (true){
        Socket connectionSocket = defaultSocket.accept();
        BufferedReader fromClient= new BufferedReader(new     InputStreamReader(connectionSocket.getInputStream()));
        String msg = fromClient.readLine();
        System.out.println("Recieved: " + msg);
    }

You are opening the socket, reading only one line and then you are waiting for the next socket.

Instead you should do Socket connectionSocket = defaultSocket.accept(); outside your while loop, and read from this socket in your loop, like this:

System.out.println("Listening on port: " + DEFAULT_PORT_NUMBER);
Socket connectionSocket = defaultSocket.accept();
BufferedReader fromClient= new BufferedReader(new     InputStreamReader(connectionSocket.getInputStream()));
String msg = "";
while ((msg = fromClient.readLine()) != null){    
    System.out.println("Recieved: " + msg);
}
gonidelis
  • 885
  • 10
  • 32
Alex
  • 1,175
  • 1
  • 8
  • 25
  • 1
    When `readLine()` returns null you must stop reading and close the socket. And the client I/O should happen in a separate thread so that the server can service multiple clients simultaneously. – user207421 Aug 19 '15 at 08:53
  • Thank you. This fixed my problem. However, now once the client has finished sending data, and the socket and the DataOutputStream have been closed the server prints "Recieved: null" until it is stopped. Is there a way to check if the client has finished? Preferably not by if(msg == null), is there a built in method to use? – rj93 Aug 19 '15 at 08:54
  • 2
    @RichardJones Standard way would be `String msg = ""; while((msg = fromClient.readLine()) != null) {`. – Kayaman Aug 19 '15 at 09:01
  • 1
    @Kayaman: Thank you, I will edit my answer to include this. EJP, you are right, but OPs problem didn't involve multithreading at all. – Alex Aug 19 '15 at 09:08
  • @Alex That's only one of the two catastrophic problems I pointed out with this code. The other led to the infinite loop that the OP complained about in comments above. You've addressed it, but without acknowledging anybody. – user207421 Aug 19 '15 at 09:58
  • @EJP: I'm sorry, I didn't mean to fix this secretly; I fixed it by including Kayamans comment; I should have mentioned it is a fix for the problem you pointed out.. – Alex Aug 19 '15 at 10:48
1

Change your server side code like below

public class SampleServerTCP { private static final int DEFAULT_PORT_NUMBER = 39277;

public static void main(String[] args) throws IOException {
    ServerSocket defaultSocket = new ServerSocket(DEFAULT_PORT_NUMBER);

    System.out.println("Listening on port: " + DEFAULT_PORT_NUMBER);
     Socket connectionSocket = defaultSocket.accept();
     BufferedReader fromClient= new BufferedReader(new     InputStreamReader(connectionSocket.getInputStream()));
     String msg = fromClient.readLine();;
    while (msg!=null){



        System.out.println("Received: " + msg);
        msg = fromClient.readLine();
    }
}

}

user207421
  • 305,947
  • 44
  • 307
  • 483
RAJKUMAR NAGARETHINAM
  • 1,408
  • 1
  • 15
  • 26