示例#1
0
        private static void OnGetHostAddresses(IAsyncResult result)
        {
            ConnectAsyncExtension.DnsConnectState asyncState = result.AsyncState as ConnectAsyncExtension.DnsConnectState;
            IPAddress[] hostAddresses;
            try {
                hostAddresses = Dns.EndGetHostAddresses(result);
            } catch {
                asyncState.Callback((global::System.Net.Sockets.Socket)null, asyncState.State, (SocketAsyncEventArgs)null);
                return;
            }

            if (hostAddresses == null || hostAddresses.Length <= 0)
            {
                asyncState.Callback((global::System.Net.Sockets.Socket)null, asyncState.State, (SocketAsyncEventArgs)null);
            }
            else
            {
                asyncState.Addresses = hostAddresses;
                ConnectAsyncExtension.CreateAttempSocket(asyncState);
                global::System.Net.Sockets.Socket attempSocket;
                IPAddress nextAddress = ConnectAsyncExtension.GetNextAddress(asyncState, out attempSocket);
                if (nextAddress == null)
                {
                    asyncState.Callback((global::System.Net.Sockets.Socket)null, asyncState.State, (SocketAsyncEventArgs)null);
                }
                else
                {
                    SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                    e.Completed += new EventHandler <SocketAsyncEventArgs>(ConnectAsyncExtension.SocketConnectCompleted);
                    IPEndPoint ipEndPoint = new IPEndPoint(nextAddress, asyncState.Port);
                    e.RemoteEndPoint = (EndPoint)ipEndPoint;
                    e.UserToken      = (object)asyncState;
                    if (attempSocket.ConnectAsync(e))
                    {
                        return;
                    }
                    ConnectAsyncExtension.SocketConnectCompleted((object)attempSocket, e);
                }
            }
        }