Export (0) Print
Expand All

Socket.Connect Method

Establishes a connection to a remote host.

[Visual Basic]
Public Sub Connect( _
   ByVal remoteEP As EndPoint _
)
[C#]
public void Connect(
 EndPoint remoteEP
);
[C++]
public: void Connect(
 EndPoint* remoteEP
);
[JScript]
public function Connect(
   remoteEP : EndPoint
);

Parameters

remoteEP
An EndPoint that represents the remote device.

Exceptions

Exception Type Condition
ArgumentNullException remoteEP is a null reference (Nothing in Visual Basic).
SocketException An error occurred when attempting to access the socket. See the Remarks section for more information.
ObjectDisposedException The Socket has been closed.
SecurityException A caller higher in the call stack does not have permission for the requested operation.

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 SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.

The Connect method will block, unless you specifically set the Blocking property to false prior to calling Connect. If you are using a connection-oriented protocol like TCP and you do disable blocking, Connect 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. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN 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.

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.

Example

[Visual Basic, C#, C++] The following example connects to a remote endpoint and then verifies the connection.

[Visual Basic] 
aSocket.Connect(anEndPoint)
If Not aSocket.Connected Then
    Console.WriteLine("Winsock error: " _
       + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()))
End If

[C#] 
aSocket.Connect(anEndPoint);
if (!aSocket.Connected) {
   Console.WriteLine("Winsock error: "
      + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
} 

[C++] 
aSocket->Connect(anEndPoint);
if (!aSocket->Connected) {
   Console::WriteLine(S"Winsock error: {0}", Convert::ToString(System::Runtime::InteropServices::Marshal::GetLastWin32Error()));
} 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace | Send | SendTo | Receive | ReceiveFrom | LocalEndPoint | SetSocketOption | Blocking | Poll

Show:
© 2014 Microsoft