The Socket Class

The Socket class has three socket operations: connecting (ConnectAsync), sending data (SendAsync), and receiving data (ReceiveAsync). The socket must first connect to a remote endpoint, described by either the IPEndPoint or DnsEndPoint class. The former is used to connect to an IP address, and the latter is used to connect to a hostname. Tables 4-9 and 4-10 display the methods and properties of the Socket class, respectively. You should always call the Shutdown method before Close to ensure that data is finished sending/receiving on the open socket.

Table 4-9. Methods of the System.Net.Socket Class

Name

Description

ConnectAsync

CancelConnectAsync SendAsync ReceiveAsync Shutdown

Initiates a connection to a remote host. A nonstatic version takes only a SocketAsyncEventArgs, while a static version takes a SocketType, a ProtocolType, and a SocketAsyncEventArgs. It returns true if the operation is pending, and false if the operation has completed.

Used to cancel a pending connection. It must pass the SocketAsyncEventArgs used in the ConnectAsync method.

Sends data specified in a SocketAsyncEventArgs. It returns true if the operation is pending, and false if the operation has completed.

Receives data from the open socket. It returns true if the operation is pending, and false if the operation has completed.

Shuts down sending, receiving, or both on the socket. It ensures that pending data is sent/received before shutting down a channel, so you should call this before you call Close.

Close

Closes the socket, releasing all resources.

Table 4-10. Properties of the System.Net.Socket Class

Name

Description

AddressFamily

Connected

NoDelay

OSSupportsIPv4

OSSupportsIPv6

ReceiveBufferSize

RemoteEndPoint

SendBufferSize

Addressing scheme used to resolve addresses. Valid values from the AddressFamily enumeration are Unknown, Unspecified, InterNetwork (for IPv4), and InterNetworkV6 (for IPv6). AddressFamily is initially specified when a socket is created.

Used to cancel a pending connection. It must pass the SocketAsyncEventArgs used in the ConnectAsync method.

Sends data specified in a SocketAsyncEventArgs.

Static property; indicates whether IPv4 addressing is supported or not.

Static property; indicates whether IPv6 addressing is supported or not.

The size of the socket's receive buffer.

The endpoint of the remote server.

The size of the socket's send buffer.

The time-to-live value for IP packets.

+1 0

Average user rating: 5 stars out of 1 votes

Post a comment

  • Receive news updates via email from this site