Why i can't receive 2 messages in a row from my server in Unity?

hi all, and sorry if the question looks a bit long, i’m having troubles with TCP sockets, i’m really new to networking and i’m doing some tests, i have a server that i’ve programmed in Java, Unity connects to it through tcp sockets, so when it connects i send some test message from the server and Unity receives it correctly, but when i send 2 messages in a row, Unity just reads the first one and seems to skip the second one, i don’t know if this behaviour is normal because it’s my very first time with sockets, the only way i found to fix this was to send an “ok” from unity to the server to make the server know that it can send the next message, but that way seems a bit slow for a multiplayer game, so my question is if it’s possible to read all the data incoming to the client (Unity) without having to send to the server that the client has received the packet and it can send the following one.

i’m using this class i found here around the community

using UnityEngine;
using System.Collections;
using System;
using http://System.IO;
using System.Net.Sockets;

public class s_TCP : MonoBehaviour {
    internal Boolean socketReady = false;

    TcpClient mySocket;
    NetworkStream theStream;
    StreamWriter theWriter;
    StreamReader theReader;
    String Host = "localhost";
    Int32 Port = 5111;

    void Start () {
    }
    void Update () {
    }
// **********************************************
    public void setupSocket() {
        try {
            mySocket = new TcpClient(Host, Port);
            theStream = mySocket.GetStream();
            theWriter = new StreamWriter(theStream);
            theReader = new StreamReader(theStream);
            socketReady = true;
        }
        catch (Exception e) {
            Debug.Log("Socket error: " + e);
        }
    }
    public void writeSocket(string theLine) {
        if (!socketReady)
            return;
        String foo = theLine + "

";
theWriter.Write(foo);
theWriter.Flush();
}
public String readSocket() {
if (!socketReady)
return “”;
if (theStream.DataAvailable)
return theReader.ReadLine();
return “”;
}
public void closeSocket() {
if (!socketReady)
return;
theWriter.Close();
theReader.Close();
mySocket.Close();
socketReady = false;
}
} // end class s_TCP

and i have this loop somewhere in my code

while(true) {
     var serverResponse : String = TCP.readSocket();
     if(serverResponse != "") {
         Debug.Log("Server: "+serverResponse); // always skips second message
     }
     yield;
}

I have the same problem. I insert ‘Thread.Sleep(100);’ into the two row, and it seems resolve this problem, but I do not know why.

Your DataAvilable check is broken. 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.

It also serves no purpose. Just because some data is available, that doesn’t mean a whole line is available. So what purpose does it serve? Just get rid of it.