confused: Socket InputStream != ServerSocker InputStream

Discussion in 'Java' started by R, Mar 12, 2005.

  1. R

    R Guest

    Hello everybody.

    I've got very stange situation.

    I have a client using Socket and server applicaton using ServerSocket.

    Server reads sth from client with this code:
    while (-1 != (read = is.read(buffer))) { /* 'is' is InputStream */
    for (int i = 0; i < read; i++) {
    sb.append((char)buffer); /* 'sb' is StringBuffer */
    }
    }

    read method NEVER returns -1, when client sends eg a basic message:
    os.write("why oh why? don't You see the end of this
    text?".getBytes());

    so I have to do a trick and I hate this solution - all messages are
    terminated with fullstop eg. client sends:
    os.write("why oh why? don't You see the end of this
    text?.".getBytes());

    and the server:
    while (46 != (read = is.read())) { /* 46 stands for ASCI '.' */
    sb.append((char)read);
    }
    it works now but it's so lame solution...

    but on the other hand when server sends a message to client:
    os.write("roger that and over ;-)".getBytes());

    client can read it without any prblem using exactly the same code as
    the first I show You:
    while (-1 != (read = is.read(buffer))) {
    for (int i = 0; i < read; i++) {
    sb.append((char)buffer);
    }
    }

    and that's why I am so confused

    why does Socket's and ServerSocket's Streams differs?

    Am I doing sth wrong?

    I'm using Linux but it's not a case...

    can You advise me sth?

    thanks in advance

    best regards
    R
     
    R, Mar 12, 2005
    #1
    1. Advertising

  2. R

    Guest

    What happens if you wrap the InputStream in a BufferedInputStream and
    then use the read method of BufferedInputStream?
     
    , Mar 12, 2005
    #2
    1. Advertising

  3. In article <>,
    (R) wrote:

    > Hello everybody.
    >
    > I've got very stange situation.
    >
    > I have a client using Socket and server applicaton using ServerSocket.
    >
    > Server reads sth from client with this code:
    > while (-1 != (read = is.read(buffer))) { /* 'is' is InputStream */
    > for (int i = 0; i < read; i++) {
    > sb.append((char)buffer); /* 'sb' is StringBuffer */
    > }
    > }
    >
    > read method NEVER returns -1, when client sends eg a basic message:
    > os.write("why oh why? don't You see the end of this
    > text?".getBytes());
    >
    > so I have to do a trick and I hate this solution - all messages are
    > terminated with fullstop eg. client sends:
    > os.write("why oh why? don't You see the end of this
    > text?.".getBytes());
    >
    > and the server:
    > while (46 != (read = is.read())) { /* 46 stands for ASCI '.' */
    > sb.append((char)read);
    > }
    > it works now but it's so lame solution...
    >
    > but on the other hand when server sends a message to client:
    > os.write("roger that and over ;-)".getBytes());
    >
    > client can read it without any prblem using exactly the same code as
    > the first I show You:
    > while (-1 != (read = is.read(buffer))) {
    > for (int i = 0; i < read; i++) {
    > sb.append((char)buffer);
    > }
    > }
    >
    > and that's why I am so confused
    >
    > why does Socket's and ServerSocket's Streams differs?
    >
    > Am I doing sth wrong?
    >
    > I'm using Linux but it's not a case...
    >
    > can You advise me sth?
    >
    > thanks in advance
    >
    > best regards
    > R


    Read the documentation. A length of -1 indicates that the stream is
    closed. If you don't close the stream on the server side, the client
    will never see the -1.

    Implementing record terminators is up to you. Object serialization can
    do some of the work for you.
     
    Kevin McMurtrie, Mar 12, 2005
    #3
  4. R

    HK Guest

    R wrote:
    > Hello everybody.
    >
    > I've got very stange situation.
    >
    > I have a client using Socket and server applicaton using

    ServerSocket.
    [...]
    > read method NEVER returns -1, when client sends eg a basic message:

    [...]
    > client can read it without any prblem using exactly the same code as


    I doubt you show all the code in your original post. The -1 shows
    up as soon as the sender --- whether client or server makes
    no difference --- closes its OuputStream.

    Your problem is that the client cannot close the OutputStream
    because this would close the socket too. But there is

    Socket.shutdownOutput()

    for exactly this purpose. It signals eof but leaves the
    Socket open to be able to read the response.

    If you need to exchange multiple request/response pairs,
    then indeed you need to do your own marking up of the
    end of a message and pray that nothing gets stuck in
    any buffering scheme, whether in Java or in the OS.

    Harald.
     
    HK, Mar 12, 2005
    #4
  5. R

    R Guest

    > Read the documentation. A length of -1 indicates that the stream is
    > closed. If you don't close the stream on the server side, the client
    > will never see the -1.


    thanks shutdownOutputStream() helped ;-)

    > Implementing record terminators is up to you. Object serialization can
    > do some of the work for you.


    no, using terminators wasn't the best idea


    thanks once again
    best regards
    R
     
    R, Mar 12, 2005
    #5
  6. In article <>,
    wrote:

    > What happens if you wrap the InputStream in a BufferedInputStream and
    > then use the read method of BufferedInputStream?


    No functional difference.

    A BufferedInputStream improves performance when you need to read many
    small quantities of data from an InputStream having a high call
    overhead. Streams to native sockets and files are expensive to interact
    with in some JVMs. A parser that reads one byte at a time can benefit
    greatly from a buffer. Worst case performance from a buffer usually
    comes from alternating between very small and very large transfers,
    which alternates the stream between buffering and pass-through modes.
     
    Kevin McMurtrie, Mar 13, 2005
    #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. Laszlo Nagy
    Replies:
    1
    Views:
    5,084
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,027
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    594
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    711
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    756
Loading...

Share This Page