I'm trying to better understand C# Socket classes. I'm working with a TcpClient object. I'm using it into a Unity (game development) project, so I cannot block the main thread. I would like to use method with Begin/End form like, for example, BeginConnect() and EndConnect().
I'm doing this:
public class TcpTest : MonoBehaviour
{
private TcpClient m_TcpClient = null;
private void Start()
{
m_TcpClient = new TcpClient();
m_TcpClient.BeginConnect("127.0.0.1", 40000, OnConnectAsyncCallback, null);
}
private void OnConnectAsyncCallback(IAsyncResult i_AsyncResult)
{
m_TcpClient.EndConnect(i_AsyncResult);
MyAsyncDebug.Log("Connect callback received.");
}
}
I'm using hercules as a socket debugging tool. I've started a TCP server with it and everythings was fine. Then I've stopped the TCP server on hercules. I've re-ran my code and I was expecting an error or an exception. Nothing, the code just starts the BeginConnect() and the AsyncCallback is never called anymore. I've also tried to start the server after the BeginConnect method was called but it does not work.
How can I know if Begin Connect is failed (maybe the destination is unreachable or any other reason)?
I cannot understand how they are designed (I've already read the MSDN docs).
Thank you and sorry for the newbie question!