StateObject state=new StateObject();
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipaddress = IPAddress.Parse("192.168.0.123");
int port = 1236;
try
{
//为什么我一启用异步Connect就会链接不上?
s.BeginConnect(ipaddress, port, new AsyncCallback(AnsyacConnectCallback), s);
//s.Connect(ipaddress, port);
Console.WriteLine(s.Connected.ToString());
Console.ReadLine();
public static void AnsyacConnectCallback(IAsyncResult result)
{
Socket s = result.AsyncState as Socket;
//s.EndConnect(result);
Console.WriteLine("Remote EndPoint:"+s.RemoteEndPoint);
为什么我一启用异步Connect就会链接不上?
而使用同步的connect就可以链接上?
}
------解决思路----------------------
晕。
BeginConnect是为了性能而这样处理的,它不等连接,就已经执行到 Console.WriteLine(s.Connected.ToString()) 语句了。
------解决思路----------------------
同1楼,只不过你打印的时候还没连上,不代表连不上