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;
}