- Home /
Question by
xxmariofer · Feb 18, 2019 at 02:57 PM ·
coroutineasyncsocket
async / coroutine question
hello, i am communicating using System.Net.Sockets between 2 apps, my main problem is that initializing the sockets takes really really long, so i would rather use a Coroutine or an async method for this task, creating the TcpClients takes more than 5 seconds and even longer if isnt able to connect, and it feels like the app has crashed. is it posible to use an await/yield instruction with the constructor? is there any other posibilitys i could try? the only solution i have found was usnig threads but doesnt feel like a good practice to create a thread only for initializing the socket.
public void InitSockets()
{
try
{
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
connected = true;
}
catch (Exception e)
{
connected = false;
Debug.Log("Socket error:" + e);
failedConnectionObject.SetActive(true);
}
}
Comment