.net sockets login server
Hi guys/Gals i have a small problem i can kinda work around but would much prefer a better solution at the moment i'm using a new thread for checking my received data from server but this is not good enough has i cant use a separate thread to call anything on the main thread.
the code works but i know it is not the best way to handle this, as unity uses its own loop for the update i am unable to check a while loop for this data as it locks the application and freezes, so thats why i chose to use a background thread for the receiving data. but it gives issues if the server is not running before i open the client. any tips to get the receive method working without a new thread would be most ideal.
void Start()
{
// Connect();
//Start a recieve Thread
Thread t;
t = new Thread(new ThreadStart(readMessages));
t.Start();
}
void readMessages()
{
print("read Message Thread Started.");
while (true)
{
try
{
byte[] recBuffer = new byte[1024];
int rec = login.client.Receive(recBuffer);
if (rec != 0)
print("recieved " + rec + " bytes");
byte[] data = new byte[rec];
Array.Copy(recBuffer, data, rec);
print("Received: " + Encoding.ASCII.GetString(data));
}
catch { }
}
}
Thanks in advance for anyone willing to help with this issue.
Your answer
![](https://koobas.hobune.stream/wayback/20220612065057im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sockets with unity and Web Player 0 Answers
Android .NET Sockets .dll problem 2 Answers
Unity .NET 4.6 Socket Exception 2 Answers
how can i transfer my leap motion data from serve to client ? 0 Answers
Three Party User Authentication 0 Answers