Socket Constructor

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the Socket class using the specified address family, socket type and protocol.

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

Syntax

'Declaration
Public Sub New ( _
    addressFamily As AddressFamily, _
    socketType As SocketType, _
    protocolType As ProtocolType _
)
public Socket(
    AddressFamily addressFamily,
    SocketType socketType,
    ProtocolType protocolType
)

Parameters

Exceptions

Exception Condition
ArgumentException

A parameter was invalid.

SocketException

The combination of addressFamily, socketType, and protocolType results in an invalid socket.

Remarks

The addressFamily parameter specifies the addressing scheme that the Socket class uses, the socketType parameter specifies the type of the Socket class, and the protocolType parameter specifies the protocol used by Socket. The three parameters are not independent. Some address families restrict which protocols can be used with them, and often the Socket type is implicit in the protocol. If the combination of address family, Socket type, and protocol type results in an invalid Socket, this constructor throws a SocketException.

NoteNote:

If this constructor throws a SocketException, use the 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.

Examples

The following example creates an instance of the Socket class.

' Create a socket and connect to the server
Dim sock As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

AddHandler socketEventArg.Completed, AddressOf SocketEventArg_Completed

socketEventArg.RemoteEndPoint = hostEntry
socketEventArg.UserToken = sock
sock.ConnectAsync(socketEventArg)
// Create a socket and connect to the server
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);

socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.