Question

using this code:

Java Server side:

...
out = new PrintWriter(this.client.getOutputStream(), true);
...
public void sendMsg(String msg) {
    out.println(msg);
    //out.flush(); // we don't flush manually because there is auto flush true
}

C# Client side:

    while(connected) {
        int lData = myStream.Read(myBuffer, 0, client.ReceiveBufferSize);
        String myString = Encoding.UTF8.GetString(myBuffer);
        myString = myString.Substring(0, lData);
        myString = myString.Substring(0, myString.Length-2);
        addToQueue(myString);
    }

variable myString have many messages that server should send them one by one like

hello \r\t hello \r\t ...

they should come separately like

hello \r\t
hello \r\t ...

which means when i wait one by one they come instantly all of them in a row, how can i make it to send one by one in separate flush.

Note I send 30~ messages in a row in one second (1s), i want them separate.

Was it helpful?

Solution

TCP supports a stream of bytes. This means you have no control how the data arrives regardless of how you send it. (Other than it will comes as bytes) You should rethink your protocol if you depend on it coming in any particular manner.

You can reduce the amount of bunching of data but all this does is reduce latency at the cost of throughput and should never be relied upon. This can be reduce (but not eliminated) by turning off Nagle and reducing co-alessing setting in your TCP driver if you can change these.

i want them separate.

You can want it but TCP does not support messages as you would want them.


The solution in you case is for your reader to match your writers protocol. You send lines so you should read lines at a time, e.g. BufferedReader.readLine(), not blocks of whatever data happens to be in the buffer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top