Socket.Connect Method (String, Int32)
Establishes a connection to a remote host. The host is specified by a host name and a port number.
Assembly: System (in System.dll)
host is null.
The port number is not valid.
An error occurred when attempting to access the socket. See the Remarks section for more information.
The Socket has been closed.
If you are using a connection-oriented protocol such as TCP, the LocalEndPoint and the specified remote host. If you are using a connectionless protocol, establishes a default remote host. After you call you can send data to the remote device with the Send method, or receive data from the remote device with the Receive method.method synchronously establishes a network connection between
If you are using a connectionless protocol such as UDP, you do not have to call SendTo and ReceiveFrom to synchronously communicate with a remote host. If you do call any datagrams that arrive from an address other than the specified default will be discarded. If you want to set your default remote host to a broadcast address, you must first call the SetSocketOption method and set the socket option to SocketOptionName.Broadcast, or will throw a SocketException. If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.before sending and receiving data. You can use
Blocking property to false prior to calling . If you are using a connection-oriented protocol like TCP and you do disable blocking, will throw a SocketException because it needs time to make the connection. Connectionless protocols will not throw an exception because they simply establish a default remote host. You can use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. If the error returned WSAEWOULDBLOCK, the remote host connection has been initiated by a connection-oriented Socket, but has not yet completed successfully. Use the Poll method to determine when the Socket is finished connecting.method will block, unless you specifically set the
If IPv6 is enabled and themethod is called to connect to a host that resolves to both IPv6 and IPv4 addresses, the connection to the IPv6 address will be attempted first before the IPv4 address. This may have the effect of delaying the time to establish the connection if the host is not listening on the IPv6 address.
If you are using a connection-oriented protocol and did not call Bind before calling , the underlying service provider will assign the local network address and port number. If you are using a connectionless protocol, the service provider will not assign a local network address and port number until you complete a send or receive operation. If you want to change the default remote host, call again with the desired endpoint.
If the socket has been previously disconnected, then you cannot use this method to restore the connection. Use one of the asynchronous BeginConnect methods to reconnect. This is a limitation of the underlying provider.
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.
Available since 2.0