System.Net.Sockets.Socket.Connect Method

Establishes a connection to a remote host. The host is specified by an array of IP addresses and a port number.

Syntax

public void Connect (System.Net.IPAddress[] addresses, int port)

Parameters

addresses
The IP addresses of the remote host.
port
The port number of the remote host.

Remarks

This method is typically used immediately after a call to System.Net.Dns.GetHostAddresses(string), which can return multiple IP addresses for a single host. If you are using a connection-oriented protocol such as TCP, the Socket.Connect(System.Net.IPAddress[], int) method synchronously establishes a network connection between Socket.LocalEndPoint and the specified remote endpoint. If you are using a connectionless protocol, Socket.Connect(System.Net.IPAddress[], int) establishes a default remote host. After you call Socket.Connect(System.Net.IPAddress[], int) you can send data to the remote device with the Socket.Send(Byte[], int, SocketFlags) method, or receive data from the remote device with the Socket.Receive(Byte[], int, SocketFlags) method.

If you are using a connectionless protocol such as UDP, you do not have to call Socket.Connect(System.Net.IPAddress[], int) before sending and receiving data. You can use Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) and Socket.ReceiveFrom(Byte[], int, int, SocketFlags, System.Net.EndPoint@) to synchronously communicate with a remote host. If you do call Socket.Connect(System.Net.IPAddress[], int) 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 Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, int) method and set the socket option to SocketOptionName.Broadcast, or Socket.Connect(System.Net.IPAddress[], int) will throw a System.Net.Sockets.SocketException. If you receive a System.Net.Sockets.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.

Socket.Connect(System.Net.IPAddress[], int) method will block, unless you specifically set the Socket.Blocking property to false prior to calling Socket.Connect(System.Net.IPAddress[], int). If you are using a connection-oriented protocol like TCP and you do disable blocking, Socket.Connect(System.Net.IPAddress[], int) will throw a System.Net.Sockets.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 System.Net.Sockets.Socket, but has not yet completed successfully. Use the Socket.Poll(int, SelectMode) method to determine when the System.Net.Sockets.Socket is finished connecting.

Note:

If you are using a connection-oriented protocol and did not call Socket.Bind(System.Net.EndPoint) before calling Socket.Connect(System.Net.IPAddress[], int), 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 Socket.Connect(System.Net.IPAddress[], int) again with the desired endpoint.

Note:

If the socket has been previously disconnected, then you cannot use this method to restore the connection. Use one of the asynchronous erload:System.Net.Sockets.Socket.BeginConnect methods to reconnect. This is a limitation of the underlying provider.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0