Beispiel #1
0
        /// <summary>
        /// Connect to the first ip address of the host the match the address family
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="host">Host name to resolve</param>
        /// <param name="port">Port to connect</param>
        public static void Connect(this AsyncSocket socket, string host, int port)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            if (port < 0 || port > 65535)
            {
                throw new ArgumentOutOfRangeException("port");
            }

#if NETSTANDARD1_3
            var ipAddress = Dns.GetHostAddressesAsync(host).Result.FirstOrDefault(ip =>
                                                                                  ip.AddressFamily == socket.AddressFamily ||
                                                                                  (socket.AddressFamily == AddressFamily.InterNetworkV6 && socket.DualMode && ip.AddressFamily == AddressFamily.InterNetwork));
#else
            var ipAddress = Dns.GetHostAddresses(host).FirstOrDefault(ip =>
                                                                      ip.AddressFamily == socket.AddressFamily ||
                                                                      (socket.AddressFamily == AddressFamily.InterNetworkV6 && socket.DualMode && ip.AddressFamily == AddressFamily.InterNetwork));
#endif

            if (ipAddress != null)
            {
                socket.Connect(ipAddress, port);
            }
            else
            {
                throw new ArgumentException("invalid host", "host");
            }
        }
Beispiel #2
0
        /// <summary>
        /// Internal function to start the actual connection establishment.
        /// </summary>
        private void StartConnecting()
        {
            Debug.Assert(m_s == null);

            // Create the socket.
            try
            {
                m_s = AsyncSocket.Create(m_addr.Resolved.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            }
            catch (SocketException)
            {
                AddReconnectTimer();
                return;
            }

            m_ioObject.AddSocket(m_s);
            m_handleValid = true;

            // Connect to the remote peer.
            try
            {
                m_s.Connect(m_addr.Resolved.Address.Address, m_addr.Resolved.Address.Port);
                m_socket.EventConnectDelayed(m_endpoint, ErrorCode.InProgress);
            }
            catch (SocketException ex)
            {
                OutCompleted(ex.SocketErrorCode, 0);
            }
        }
Beispiel #3
0
 public static void Connect(this AsyncSocket socket, IPAddress ipAddress, int port)
 {
     socket.Connect(new IPEndPoint(ipAddress, port));
 }
Beispiel #4
0
        /// <summary>
        /// Internal function to start the actual connection establishment.
        /// </summary>
        private void StartConnecting()
        {
            Debug.Assert(m_s == null);

            // Create the socket.
            try
            {
                m_s = AsyncSocket.Create(m_addr.Resolved.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            }
            catch (SocketException)
            {
                AddReconnectTimer();
                return;
            }

            m_ioObject.AddSocket(m_s);
            m_handleValid = true;

            // Connect to the remote peer.
            try
            {
                m_s.Connect(m_addr.Resolved.Address.Address, m_addr.Resolved.Address.Port);
                m_socket.EventConnectDelayed(m_endpoint, ErrorCode.InProgress);
            }
            catch (SocketException ex)
            {
                OutCompleted(ex.SocketErrorCode, 0);
            }
            // TerminatingException can occur in above call to EventConnectDelayed via 
            // MonitorEvent.Write if corresponding PairSocket has been sent Term command
            catch (TerminatingException)
            {}
        }