Question

I have similar problem maybe the same like here.

From the server (Java TCP Server) im doing this:

public void sendMsg(String msg) {

    out.println(msg); // msg is: "MSG Hello" without quetes

    out.flush();

}

when i push it twice or more i receive only first message in client code which is unity3d code c# socket

void Update() {

    if(connected) {

        try {

            if(theStream.DataAvailable) {

                String data = sr.ReadLine();
// bla bla
Was it helpful?

Solution

Get rid of the if(theStream.DataAvailable). You cannot check if data is available that way since if you have already received it, it is not available. While the ReadLine function only returns one line to you, it may read much more than one line.

So here's what happens:

  1. All data is sent.

  2. Data is available, you call ReadLine. It reads all the data and returns one line to you.

  3. No data is available now, since it has already been read from the connection.

There are other problems with that check too. If it's trying to avoid calling ReadLine if a line isn't available, it won't do that. Some data being available doesn't mean a whole line is. (Imagine if the other end maliciously sends just a single X byte.)

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