Simple Client / Server app, not working?

Hi,

I am using VS.NET 2005 beta 2, and I'm having a problem setting up a simple console client / server application using ServerSockets and Sockets.

Using my code, the server gets up and running, and receives a connection from the client. However, for some reason I cannot send information to the server and receive information back after this happens. Here is my code:

SERVER
=================

private static class sessionThread extends Thread {
        private Socket socket;

        public sessionThread(Socket s) {
            socket = s;
        }

        public void run() {
            BufferedReader input;
            PrintWriter output;

            try {
                input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                output = new PrintWriter(socket.getOutputStream());

                while ((s = input.readLine()) != null) {
                        /* this for some reason never happens! */
                    output.println(s);
                }
                socket.close();
            }
        }
    }

public static void main(String[] args) throws Exception {
        ServerSocket sSocket = new ServerSocket(3000, 20);
        Socket socket;

        while ((socket = sSocket.accept()) != null)
            new sessionThread(socket).start();

        sSocket.close();
    }

 


If you look in the sessionThread.run() procedure, the commented code that says "This will never happen" is as far as the code on the server side gets to. For some reason, it never reads anything from the client past when the connection is made. Here is the client code:

CLIENT
=================

public static void main(String[] args) throws Exception {
        Socket socket;
        BufferedReader stdin, input;
        PrintWriter output;

        socket = new Socket(args[0], 3000);

        stdin = new BufferedReader(new InputStreamReader(System.in));
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

        while ((s = stdin.readLine()) != null) {
            output.println(s);
             /* Again, will never happen! */
            result = input.readLine();
            System.out.println(result);
        }

        socket.close();
    }

 


Again, the commented code will never happen as it never receives anything from the server past the connection.

Can anyone tell me why this is happening

Thanks!

Yaron


Answer this question

Simple Client / Server app, not working?

  • Hal Kilmer

    Since you are using BufferedStreams, you should do flush in order to send few bytes of data immediately.

    Add output.flush(); in both client and server code after output.println(....);.


    Thanks,
    Ramakrishna Neela

    PS: Same code works for me after adding flush call


  • didier_g

    Thanks so much, this seemed to fix it.

    Kinda strange tho, as the same code, without the flush() works fine in java, but not in j#.

    Thanks again.

  • Simple Client / Server app, not working?