A number of properties can be set or modified on the socket, which affects the socket s behavior. An example of a property is how much data the underlying network stack buffers for the socket, which can be important if a socket is receiving very large UDP datagrams and wants the local stack to be able to buffer multiple packets.
Two methods can change certain characteristics of a socket: GetSocketOption and SetSocketOption . GetSocketOption retrieves the current property value, and SetSocketOption sets the property s value. The parameter list for these two methods is the same. The first parameter is a SocketOptionLevel enumerated type, and the second is the SocketOptionName enumerated type. The value of the options can be one of three types, depending on the option level and name : integer, byte array, or object.
SocketOptionLevel indicates on what network level the option name is being applied. Socket options can be applied at the socket level ( Socket ) or at the protocol level (IPv4, IPv6, UDP, or TCP). SocketOptionName is an enumerated type that indicates the property being queried or set. Note that a particular SocketOptionName typically applies to a single SocketOptionLevel , although this is not always the case. Table 8-4 describes some of the common SocketOptionName properties. The multicast- related options will be covered in the IP Multicasting section later in this chapter.
Option Name | Option Level | Description |
---|---|---|
Broadcast | Socket | Boolean value that enables the sending of broadcast packets on a socket. Only valid for protocols that support broadcast data. |
DontLinger | Socket | Boolean value that disables any linger value set with the Linger option. |
ExclusiveAddressUse | Socket | Boolean value that disallows any subsequent socket from binding to the same port regardless of whether the socket sets the ReuseAddress option. |
HeaderIncluded | IP or IPv6 | Boolean value that s used with raw sockets and indicates that the protocol header is included as part of data to send. |
IpTimeToLive | IP or IPv6 | Sets the integer time to live (TTL) value in the IP header. |
KeepAlive | Tcp | Boolean value that enables TCP keepalives. Note that by default keepalives are sent on the order of hours. |
Linger | Socket | Sets a time limit on unacknowledged data after the Close method has been called on a connection-oriented socket. A LingerOption object is passed to the call. |
NoDelay | Tcp | Boolean value that disables the Nagle |
ReceiveBuffer | Socket | Sets the integer size in bytes (default of 8 KB) of the per-socket buffer maintained by the stack for receiving data. |
ReceiveTimeout | Socket | Sets the timeout value for receive operations in milliseconds . An exception is thrown if timeout occurs. |
ReuseAddress | Socket | When enabled, indicates a socket can be bound to an address even if another socket is bound to the same address and port. |
SendBuffer | Socket | Sets the integer size in bytes (default of 8 KB) of the per-socket buffer maintained by the stack for sending data. |
SendTimeout | Socket | Sets the timeout value for send operations in milliseconds. If the send can t complete in the specified time, an exception is thrown. |
Warning | The ReceiveTimeout and SendTimeout socket options should never be used on TCP sockets because data might be lost when a timeout occurs. |
A few notes about socket options. First, the majority of the options are Boolean values. When setting an option s value, simply pass an integer where zero is false and non-zero is true . Retrieving the option s value is a little more complex because a byte array must be used. The following code illustrates setting the ReceiveBuffer option and then retrieving the value just set:
SocketmySocket; byte[]optionBuffer=newbyte[4]; intsendBufferSize=16384; //FirstcreateavalidSocketmySocket mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, sendBufferSize); mySocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, optionBuffer); sendBufferSize=BitConverter.ToInt32(optionBuffer,0); Console.WriteLine("RetrievedSendBuffersize={0}",sendBufferSize);
DimmySocketAsSocket DimoptionBuffer(4)AsByte DimsendBufferSizeAsInteger=16384