Share via


Connect Method

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Establishes a connection to a remote host.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

Syntax

'Declaration
Public Sub Connect ( _
    remoteEP As EndPoint _
)
public void Connect(
    EndPoint remoteEP
)
public:
void Connect(
    EndPoint^ remoteEP
)
member Connect : 
        remoteEP:EndPoint -> unit 
public function Connect(
    remoteEP : EndPoint
)

Parameters

Remarks

If you are using a connection-oriented protocol such as TCP, the Connect method synchronously establishes a network connection between LocalEndPoint and the specified remote endpoint. If you are using a connectionless protocol, Connect establishes a default remote host. After you call Connect, you can send data to the remote device with the Send method, or receive data from the remote device with the Receive method.

If you are using a connectionless protocol such as UDP, you do not have to call Connect before sending and receiving data. You can use SendTo and ReceiveFrom to synchronously communicate with a remote host. If you do call Connect, 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 Connect 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.

Note

If you are using a connection-oriented protocol and did not call Bind before calling Connect, 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 Connect again with the desired endpoint.

.NET Framework Security

See Also

Reference

Socket Class

System.Net.Sockets Namespace