Client/Server Socket problem

Discussion in 'Java' started by Java and Swing, Oct 2, 2006.

  1. Hi, I have am trying to write a simple client/server, where the client
    reads in a line of text and sends it to the server. the server
    capitalizes the text and sends it back...but the server is never
    finishing reading from the client socket input stream...any ideas?

    client (snippet):

    // connect to the server
    sock = new Socket("127.0.0.1", 8000);

    byte[] bytes = msg.getBytes();

    // send the user input to the server
    BufferedOutputStream bos = new
    BufferedOutputStream(sock.getOutputStream());
    bos.write(bytes);
    bos.flush();

    -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    server (snippet):

    ServerSocket sock = new ServerSocket(8000);
    while (serving) {
    try {
    Socket client = sock.accept();
    int val = -1;
    BufferedInputStream bis = new
    BufferedInputStream(client.getInputStream());
    while ((val = bis.read()) != -1) {
    System.out.print((char) val);
    }
    System.out.println("done reading from client...");

    BufferedOutputStream bos = new
    BufferedOutputStream(client.getOutputStream());
    bos.write("GOT IT!".getBytes());
    bos.flush();

    client.close();
    } catch (IOException e) {
    e.printStackTrace();
    serving = false;
    } finally {
    if (sock != null) {
    try {
    sock.close();
    } catch (IOException e) {}
    }
    }
    }
    sock.close();


    thanks
    Java and Swing, Oct 2, 2006
    #1
    1. Advertising

  2. On 2 Oct 2006 10:04:25 -0700, Java and Swing wrote:
    > Hi, I have am trying to write a simple client/server, where the
    > client reads in a line of text and sends it to the server. the
    > server capitalizes the text and sends it back...but the server is
    > never finishing reading from the client socket input stream...any
    > ideas?


    Your server reads until EOF on the client connection, i.e. it will
    read until the client *closes* the OutputStream (or does
    shutdownOutput). Perhaps you can see why that might be a problem if
    the client intends to communicate more with the server after sending
    the initial request.

    I'd suggest changing the protocol slightly, for example the server
    could read a single *line* of text (look at
    BufferedInputStream.readLine()).

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Oct 2, 2006
    #2
    1. Advertising

  3. "Java and Swing" <> wrote in message
    news:...
    > Hi, I have am trying to write a simple client/server, where the client
    > reads in a line of text and sends it to the server. the server
    > capitalizes the text and sends it back...but the server is never
    > finishing reading from the client socket input stream...any ideas?
    >
    > client (snippet):
    >
    > // connect to the server
    > sock = new Socket("127.0.0.1", 8000);
    >
    > byte[] bytes = msg.getBytes();
    >
    > // send the user input to the server
    > BufferedOutputStream bos = new
    > BufferedOutputStream(sock.getOutputStream());
    > bos.write(bytes);
    > bos.flush();
    >
    > -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    >
    > server (snippet):
    >
    > ServerSocket sock = new ServerSocket(8000);
    > while (serving) {
    > try {
    > Socket client = sock.accept();
    > int val = -1;
    > BufferedInputStream bis = new
    > BufferedInputStream(client.getInputStream());
    > while ((val = bis.read()) != -1) {
    > System.out.print((char) val);
    > }
    > System.out.println("done reading from client...");
    >
    > BufferedOutputStream bos = new
    > BufferedOutputStream(client.getOutputStream());
    > bos.write("GOT IT!".getBytes());
    > bos.flush();


    The server is waiting for EOF which the client does not send. Flush does
    not send an EOF and the absence of data does not constitute EOF. Only
    closing the stream does that, which you may or may not want to do. If you
    are intendeding to keep the stream open, you must either send some marker
    (e.g. CR/LF) to tell the server to stop reading or send a length to tell it
    how much to read. TCP is stream-oriented, not packet oriented.

    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Oct 2, 2006
    #3
  4. > I'd suggest changing the protocol slightly, for example the server
    > could read a single *line* of text (look at
    > BufferedInputStream.readLine()).


    well i changed the client to do....
    BufferedOutputStream bos = new
    BufferedOutputStream(sock.getOutputStream());
    bos.write(bytes);
    bos.write("\n".getBytes());
    bos.flush();


    and the server to do

    String line = null;
    BufferedReader reader = new BufferedReader(new
    InputStreamReader(client.getInputStream()));
    while ((line = reader.readLine()) != null) {
    System.out.println(line);
    }

    ....same problem however.
    Java and Swing, Oct 2, 2006
    #4
  5. On 2 Oct 2006 11:08:27 -0700, Java and Swing wrote:
    > ...same problem however.


    Same mistake too. Your server doesn't send a reply until it has read
    to EOF from the client (when readLine() returns null). Send the reply
    after each line instead. At EOF, close the client connection.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Oct 2, 2006
    #5
  6. Java and Swing

    Joshua Jung Guest

    On Mon, 02 Oct 2006 18:14:13 +0000, Gordon Beaton wrote:

    > On 2 Oct 2006 11:08:27 -0700, Java and Swing wrote:
    >> ...same problem however.

    >
    > Same mistake too. Your server doesn't send a reply until it has read
    > to EOF from the client (when readLine() returns null). Send the reply
    > after each line instead. At EOF, close the client connection.
    >
    > /gordon


    Hey thanks guys for your help! I figured out what was wrong:

    Problem 1. I was not allocating enough heap memory (the -Xmx option for
    java JRE) for my server.
    Problem 2. I had to set the maximum file descriptors to a much higher
    number (using the UNIX ulimit -S -n command).

    I think the reason things were so hard to track was because I was having
    problems with both of these issues.

    Once I did both these things... I am now cranking out thousands of
    connections with no problem.

    Thanks again!

    Josh <><
    Joshua Jung, Oct 2, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Avizz
    Replies:
    3
    Views:
    13,749
    Andy Fish
    Sep 29, 2003
  2. greatestmclarenfan
    Replies:
    2
    Views:
    563
    Steve Horsley
    Feb 16, 2006
  3. Laszlo Nagy
    Replies:
    1
    Views:
    4,754
    Mark Wooding
    Jan 27, 2009
  4. Jean-Paul Calderone
    Replies:
    0
    Views:
    940
    Jean-Paul Calderone
    Jan 27, 2009
  5. Laszlo Nagy
    Replies:
    0
    Views:
    524
    Laszlo Nagy
    Feb 1, 2009
Loading...

Share This Page