目前我的代码看起来像这样
class XattyServer
{
private Socket _client;
private Socket _worker;
private ManualResetEvent _state;
private string host;
private int port;
public XattyServer(string addr, int to)
{
_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
host = addr;
port = to;
}
public void Connect()
{
_client.BeginConnect(IPAddress.Parse(host), port, new AsyncCallback(AsyncBeginConnect), _client);
}
private void AsyncBeginConnect(IAsyncResult ar)
{
_worker = (Socket)ar.AsyncState;
Console.WriteLine("Connected to - {0}", host);
}
}
XattyServer _server = new XattyServer("174.36.242.42", 10030);
_server.Connect();
Console.ReadLine();
事情是BeginConnect看起来从未激发过,因为从未执行过Console.Wriline。 ip和端口都是正确的。