System.Net.Sockets.Socket.Connect C# (CSharp) Method

Connect() public method

public Connect ( string host, int port ) : void
host string
port int
return void
        public void Connect(string host, int port)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, host);

            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (host == null)
            {
                throw new ArgumentNullException(nameof(host));
            }
            if (!TcpValidationHelpers.ValidatePortNumber(port))
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (_addressFamily != AddressFamily.InterNetwork && _addressFamily != AddressFamily.InterNetworkV6)
            {
                throw new NotSupportedException(SR.net_invalidversion);
            }

            IPAddress parsedAddress;
            if (IPAddress.TryParse(host, out parsedAddress))
            {
                Connect(parsedAddress, port);
            }
            else
            {
                IPAddress[] addresses = Dns.GetHostAddressesAsync(host).GetAwaiter().GetResult();
                Connect(addresses, port);
            }

            if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
        }

Same methods

Socket::Connect ( EndPoint remoteEP ) : void
Socket::Connect ( IPAddress address, int port ) : void

Usage Example

Example #1
0
        public void ConnectHost(string host, int port)
        {
            _tcpSock.Connect(host, port);

            if (_tcpSock.Connected)
            {
                if (ClientConnected != null)
                {
                    ClientConnected(this, null);
                }
            }
        }
All Usage Examples Of System.Net.Sockets.Socket::Connect