1

I'm trying to do make simple network communication where a client sends a user input string to the server, which server then displays to the console. When I send only one string, it works fine, but as soon as I wrap my user input code and send code in a while loop, the server receives nothing.

SERVER :

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);
            System.out.println("Server now hosted on port " + PORT);
            Socket s = serverSocket.accept();
            System.out.println("A client has connected !");

            BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
            BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());

            while(true){            
                //RECEIVE
                int data;
                String inString = "";
                while((data=bis.read()) != -1){
                    inString += (char)data;
                }
                System.out.println("SLAVE : " + inString);              
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Port déjà utilisé");
        }finally {
            try {
                serverSocket.close();
                System.out.println("Server closed");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Could not close port " + PORT);
            }
        }

CLIENT :

Scanner sc = new Scanner(System.in);
        Socket s = null;

        try {
            s = new Socket("127.0.0.1", PORT);

            BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
            BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());

            System.out.println("Connexion established !");
            while(true){ // without this while loop, it works fine
                String send = "";
                System.out.print(">> ");
                send = sc.nextLine();
                bos.write(send.getBytes());
                bos.flush();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Could not connect");;
        }
        finally {
            try {
                s.close();
                System.out.println("Closing socket");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Could not close connection");;
            }
        }
        System.out.println("End of client");
    }

I expected the server to write any data it's reading from the socket as it's comming. But it just does nothing. I'm not quiet sure is the problem is comming from the server or the client.

1
  • Get rid of the outer while (true) loop. It serves no purpose. Once you've read -1 from the input stream, that's it, there will never be any more input. Commented Apr 24, 2019 at 6:42

1 Answer 1

3

The problem is with your while((data=bis.read()) != -1){ code.

It is looping until the EOS is received -1

When you don't have a client loop your Stream is closed, allowing the -1 to be sent, but not when you have a loop. Try printing with the server loop as below

while((data=bis.read()) != -1){
   inString += (char)data;

   if (((char)data) == '\n') {
       System.out.println("SLAVE : " + inString);   
       inString = "";
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I can't make any sense of your first or second paragraphs.
Yep, that works. (Well, now I have to add the '\n' when building the input string for some reasons. I thought sc.nextLine() already did that.) But I understand why my initial attempt didn't work. I should look into file input/output. I've never taken the time to do that. Thank you very much.
nextLine will strip off the CRLF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.