Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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)

public Socket(
	AddressFamily addressFamily,
	SocketType socketType,
	ProtocolType protocolType
)

Parameters

addressFamily
Type: System.Net.Sockets.AddressFamily
One of the AddressFamily values.
socketType
Type: System.Net.Sockets.SocketType
One of the SocketType values.
protocolType
Type: System.Net.Sockets.ProtocolType
One of the ProtocolType values.

ExceptionCondition
ArgumentException

A parameter was invalid.

SocketException

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

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.

The following example creates an instance of the Socket class.


// 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);


Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show: