- Home /
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 + "\r\n";
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;
}
Also i have this problem... when server send two messages in row i get only first one, to get the second one client have to write something back to server to receive second message from server...
Answer by areca_chen · Jun 15, 2012 at 05:05 AM
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.
The sleep "helped" because it increased the chance that calling ReadLine
would only read precisely that line from the underlying connection, increasing the chances that DataAvailable
would later be true.
Answer by JoelKatz · Dec 27, 2012 at 12:04 AM
Your DataAvilable
check is broken. Here's what happens:
All data is sent.
Data is available, you call ReadLine. It reads all the data and returns one line to you.
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.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Help with Java socket server 1 Answer
TCP and Sockets in unity 0 Answers
Unity sockets 1 Answer