System.Net.Sockets.Socket.Connect Method

Establishes a connection to a remote host.

Syntax

public void Connect (System.Net.EndPoint remoteEP)

Parameters

remoteEP
An System.Net.EndPoint that represents the remote device.

Exceptions

TypeReason
ArgumentNullException remoteEP is null.
InvalidOperationExceptionAn asynchronous call is pending and a blocking method has been called.
System.Net.Sockets.SocketException
Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.
ObjectDisposedExceptionThe current instance has been disposed.
System.Security.SecurityException A caller in the call stack does not have the required permission.

Remarks

If you are using a connection-oriented protocol such as TCP, the Socket.Connect(System.Net.EndPoint) 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.EndPoint) establishes a default remote host. After you call Socket.Connect(System.Net.EndPoint), 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.EndPoint) 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.EndPoint), 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.EndPoint) 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.

The Socket.Connect(System.Net.EndPoint) method will block, unless you specifically set the Socket.Blocking property to false prior to calling Socket.Connect(System.Net.EndPoint). If you are using a connection-oriented protocol like TCP and you do disable blocking, Socket.Connect(System.Net.EndPoint) 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.EndPoint), 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.EndPoint) 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>].

Permissions

TypeReason
System.Net.SocketPermissionRequires permission to make a connection to the endpoint defined by remoteEP. See System.Net.NetworkAccess.Connect.

Requirements

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