Socket Constructor

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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.

Note

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

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Platforms

Windows Phone

See Also

Reference

Socket Class

System.Net.Sockets Namespace

SocketException

AddressFamily

ProtocolType

SocketType