コード例 #1
0
        private static void SocketConnectCompleted(object sender, SocketAsyncEventArgs e)
        {
            DnsConnectState dnsConnectState = e.UserToken as DnsConnectState;

            if (e.SocketError == SocketError.Success)
            {
                ClearSocketAsyncEventArgs(e);
                dnsConnectState.Callback((Socket)sender, dnsConnectState.State, e, null);
                return;
            }
            if (e.SocketError != SocketError.HostUnreachable && e.SocketError != SocketError.ConnectionRefused)
            {
                ClearSocketAsyncEventArgs(e);
                dnsConnectState.Callback(null, dnsConnectState.State, e, null);
                return;
            }
            Socket    attempSocket;
            IPAddress nextAddress = GetNextAddress(dnsConnectState, out attempSocket);

            if (nextAddress == null)
            {
                ClearSocketAsyncEventArgs(e);
                e.SocketError = SocketError.HostUnreachable;
                dnsConnectState.Callback(null, dnsConnectState.State, e, null);
                return;
            }
            e.RemoteEndPoint = new IPEndPoint(nextAddress, dnsConnectState.Port);
            if (!attempSocket.ConnectAsync(e))
            {
                SocketConnectCompleted(attempSocket, e);
            }
        }
コード例 #2
0
        private static void OnGetHostAddresses(IAsyncResult result)
        {
            DnsConnectState dnsConnectState = result.AsyncState as DnsConnectState;

            IPAddress[] array;
            try
            {
                array = Dns.EndGetHostAddresses(result);
            }
            catch (Exception exception)
            {
                dnsConnectState.Callback(null, dnsConnectState.State, null, exception);
                return;
            }
            if (array == null || array.Length == 0)
            {
                dnsConnectState.Callback(null, dnsConnectState.State, null, new SocketException(11001));
                return;
            }
            dnsConnectState.Addresses = array;
            CreateAttempSocket(dnsConnectState);
            Socket    attempSocket;
            IPAddress nextAddress = GetNextAddress(dnsConnectState, out attempSocket);

            if (nextAddress == null)
            {
                dnsConnectState.Callback(null, dnsConnectState.State, null, new SocketException(10047));
                return;
            }
            if (dnsConnectState.LocalEndPoint != null)
            {
                try
                {
                    attempSocket.ExclusiveAddressUse = false;
                    attempSocket.Bind(dnsConnectState.LocalEndPoint);
                }
                catch (Exception exception2)
                {
                    dnsConnectState.Callback(null, dnsConnectState.State, null, exception2);
                    return;
                }
            }
            SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs();

            socketAsyncEventArgs.Completed += SocketConnectCompleted;
            IPEndPoint iPEndPoint = (IPEndPoint)(socketAsyncEventArgs.RemoteEndPoint = new IPEndPoint(nextAddress, dnsConnectState.Port));

            socketAsyncEventArgs.UserToken = dnsConnectState;
            if (!attempSocket.ConnectAsync(socketAsyncEventArgs))
            {
                SocketConnectCompleted(attempSocket, socketAsyncEventArgs);
            }
        }