2

I wrote a simple socket program but the sever is not printing out anything.

Client:

        public class ChatClient {
            public static void main(String[] args) throws Exception {
                Socket socket = new Socket("localhost", 9001);
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                Scanner reader = new Scanner(System.in);
                while (true) {
                    String test = reader.next();
                    out.write(test);
                    System.out.println(test);
                    //String line = in.readLine();
                }
            }
        }

Server:

    public class ServerApplication {
        private static final int PORT = 9001;
        private static HashSet<String> names = new HashSet<String>();

        private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();

        public static void main(String[] args) throws Exception {
            ServerSocket listener = new ServerSocket(PORT);
            try {
                while (true) {
                    System.out.println("Server starts!");
                    new ChatThread(listener.accept()).start();
                }

            } finally {
            }
        }

    }


public class ChatThread extends Thread{
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;

    public ChatThread(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run() {
        try {
            System.out.println("Thread Starts!");
            in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);

            while(true){
                System.out.println("Getting input!");
                String input = in.readLine();
                System.out.println("test");
                if (input!=null){
                //out.print(input);
                System.out.println(input);}
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

Any ideas how should I fix this so when I type "Hello" on client, server will print out "Hello" using system.out.println()?

11
  • what output are you getting? Commented Apr 16, 2015 at 2:15
  • I am getting no output. I only get to: Server starts! Server starts! Thread Starts! Getting input! (nothing after that!) Commented Apr 16, 2015 at 2:16
  • you are starting the client after the server? Commented Apr 16, 2015 at 2:18
  • Could it be firewall? Commented Apr 16, 2015 at 2:19
  • 4
    In the client, after writing, do what @ScaryWombat suggested, out.write("\r\n"). Then call out.flush() Commented Apr 16, 2015 at 2:30

2 Answers 2

1

This is highly likely the socket buffers not being flushed and the data is not actually sent to the socket; the server is waiting for more data before sending a chunk.

If we look at the docs for PrintWriter:

... if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked ...

However, you are using print, not println, so the flushing is not automagic.

Try manualy flushing you streams after writing data:

out.write(test);
out.flush();

yep, this was an answer to a 3 year old post... question was edited and popped up on my stream

Sign up to request clarification or add additional context in comments.

Comments

0

Testing your server using telnet (telnet localhost 9001) revealed that it's working just fine. I assume that the fact that it can only print a line at a time, rather than individual characters, is intended behavior.

I did find one bug. When terminating the telnet connection, the server would go into an infinite loop. I suggest the following change:

if (input!=null){
    System.out.println(input);}
}
else {
    break;
}

For the client, I was able to fix it simply by replacing write with println:

out.println(test);

Comments

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.