I made some changes to my code and it connects correctly, sends data correctly but it goes wrong while receiving data.
When I poll for data (currently in a coroutine) then it works “if there is data available for the client to read”.
But , when there is no data available, then the reader.ReadLine() just hangs and hangs forever waiting for anything readable to arrive from the server to the client.
The biggest problem is dat all my other scripts are also not running anymore at that moment and it looks like unity is totally hanging (ok, its waiting for input on the socket but its the main effect)
I read somewhere that sockets are blocki,g but how could I change the code below to let my unity code check regularly if there is data to read at the socket but in meanwhile not block my other scripts ?
Regards,
Bart
import System;
import System.IO;
import System.Net;
import System.Net.Sockets;
import System.Text;
var server : TcpListener;
var client : TcpClient;
var stream : NetworkStream;
var reader: StreamReader;
var writer: StreamWriter;
var msg:String;
function OnGUI(){
if (GUI.Button(Rect(10,40,200,20), “Start Client”)) {
client = new TcpClient(“178.21.113.227”, 2222);
}
if (GUI.Button(Rect(10,70,200,20), “Send something to server”)) {
stream = client.GetStream();
writer = new StreamWriter(stream);
writer.WriteLine(“NICK:mynickname”);
writer.Flush();
}
if (GUI.Button(Rect(10,100,200,20), “Start Coroutine”)) {
StartCoroutine(“readData”);
}
}
function readData(){
stream = client.GetStream();
reader = new StreamReader(stream);
msg = reader.ReadLine();
print (msg);
readData();
}