Weird socket programming java?

Discussion in 'Java' started by bigbinc, Dec 29, 2003.

  1. bigbinc

    bigbinc Guest

    I have a question and my own answer, but I think it is strange.
    I am working with a server, and different clients.

    One client is command-line, the other servlet(yea I said servlet, see

    The commandline client works fine, but I have to modify my server(not
    servlet) to fix the servlet client.

    The problem? The servlet client reads in data twice(it is sent twice
    or something?). I think it has something to do with the out.flush.

    I can remove the newlines below and be ok. I just it is strange.

    // server

    //out.flush(); ?? remove comments fixes problem strangely

    // client

    public String GetBytes(InputStream _instream) {

    String _fin = "";

    try {

    byte [] tmp = new byte[2048];

    int bytes;

    int _debugbytes = -1;

    while((bytes = != -1) {

    //_out.write(tmp, 0, bytes);
    String _str = new String(tmp);
    _fin += _str;

    if (bytes > 0)
    _debugbytes = bytes;

    } // end of thw ehile

    } catch(IOException e) {

    } // end of the try - catch

    return _fin;

    } // end of the function

    Berlin Brown
    bigbinc, Dec 29, 2003
    1. Advertisements

  2. You don't consider the byte count when you create _str, so could very
    well be reusing "old" data in the buffer. Choose a different String

    Unrelated to your problem, you should always specify a character
    encoding when you convert bytes to Strings. Again, choose a different
    String constructor.

    Also, you are better off declaring _fin as a StringBuffer, then using
    append(_str) after each read. Create a String from the StringBuffer
    only after you've finished reading all of the data.

    Gordon Beaton, Dec 29, 2003
    1. Advertisements

  3. Your client code is broken (details below). What you've shown of your
    server code is okay either with or without the extra flush, but is
    slightly more efficient without. If the command-line client has the
    same code as the servlet client then it is also buggy, even if the bug
    has not yet manifested there.
    The main problem is here. The read does not clear the buffer before
    filling it (not necessarilly fully) with data read from the stream, and
    neither do you. Therefore, the old data remains and is duplicated to
    the string. Moreover, you do not specify the charset with which to
    convert the bytes to characters. A correct way to do this is:

    String _str = new String(tmp, 0, bytes, "UTF-8");

    (Insert the correct charset name. UTF-8 is quite possibly being used as
    the default now, but whatever is used now, you may save yourself
    headaches later by being explicit. Whatever charset you use here should
    also be specified explicitly in the server code.)
    That's a bit inefficient, but should work.

    John Bollinger
    John C. Bollinger, Dec 29, 2003
  4. bigbinc

    bigbinc Guest

    Dope, I knew I had something wrong, I had the idea when I was
    printing i.e se the out.write command.

    Berlin Brown
    bigbinc, Dec 29, 2003
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.