Note that there are some explanatory texts on larger screens.

plurals
  1. POjava TCP socket message breaks
    primarykey
    data
    text
    <p>i have a java client-server app in java, both using the same connection class that contains both send/receive messages. for some reason, some of the messages i send are received in a malformed order:</p> <p>here's the code</p> <pre><code>//set up _in = new BufferedReader(new InputStreamReader(this._socket.getInputStream())); _out = new BufferedWriter(new OutputStreamWriter(this._socket.getOutputStream())); this._socket.setSoTimeout(S_TIMEOUT); public synchronized boolean send(String message){ try { _out.write(message); _out.write(Connection.DELIMITER); _out.flush(); return true; } catch (IOException e) { } return false; } public String receive(){ int c; try { String message = ""; System.out.println("Getting message:"); c = _in.read(); while(c != -1 &amp;&amp; c != Connection.DELIMITER) { message += (char) c; c = _in.read(); } if (c == -1) { return null; } return message; } catch (IOException e) { } return null; } </code></pre> <p>some messages, for example "new_order" will might return with "ew_ord". some characters are lost, others are sent separately. this seems odd as its TCP</p> <p>could this be an encoding related issue?</p> <p>Delimiter is (char) 0 socket timeout is 20000 (ie 20 senconds). every 10 seconds i send an empty message to make sure socket does not close</p> <p>EDIT: although it was solved using the Scanner, i must say that the original code worked fine for many messages/various machines for a very long time (a few weeks), and then suddenly failed to work with one specific message on one specific machine (other messages went through just fine). i've done socket data transfer in java MANY times and i've written many read/write methods to handle the sockets. it's the first time i ran into this.</p> <p>although in the original code i set the encoding (in the posted code i didn't), i believe that the problem was encoding related. at one point, the message that was received had every second character missing. afterwards i changed it a bit, and the first/second character of the message were received in a separate message. from my understanding, it's either an encoding issue or some firewall/other security program that was running on the message sender machine, that decided to filter outgoing packets.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload