Overview
Programming .NET Sockets applications is similar to developing a Winsock application in C/C++ because the interface uses methods that have nearly the same name and parameters as the Winsock API. The basic design centers around creating a Socket object and using methods included in that object. The Socket methods mimic the standard Winsock 1.1 calls that we described previously in this book; internally, however, the calls use Winsock 1 and 2 functionality. Although the methods are similar to the Winsock API, one of the most noticeable programming differences is that you don't have startup Winsock (by calling WSAStartup) to use the Sockets namespace as you do when writing a Winsock application. Instead, once you create a Socket object the startup feature is handled within the interface.
Creating a Socket object is simple: Create a new System.Net.Sockets.Socket object. The constructor for this object is defined as
System.Net.Sockets.Socket( System.Net.Sockets.AddressFamily AddressFamily, System.Net.Sockets.SocketType SocketType, System.Net.Sockets.ProtocolType ProtocolType );
The socket constructor method takes System.Net.Sockets enumeration type parameters AddressFamily, SocketType, and ProtocolType to create the socket. These three enumerations types have values that are similar to the Winsock 1 socket parameter types. Table 13-1 contains all of the enumerated types that all of the objects in the Sockets namespace use. Once you have successfully created a Socket object, you can begin developing a client or server application using the available methods defined in Table 13-2.
Property | Description |
AddressFamily | Similar to Address Families in Winsock—use InterNetwork for IPv4 addresses and InterNetworkV6 for IPv6 addresses. |
ProtocolFamily | Identifies which protocol to use in the Winsock catalog. |
ProtocolType | Similar to protocol types in Winsock—use IP for Internet Protocol. |
SelectMode | Used for polling on attributes on a Socket. |
SocketFlags | Similar to socket flags in Winsock used for Send and Receive methods. |
SocketOptionLevel | Available socket option levels for GetSocketOption and SetSocketOption methods. |
SocketOptionName | Available socket options for GetSocketOption and SetSocketOption methods. |
SocketShutdown | Similar to Winsock shutdown parameters. |
SocketType | Similar to socket types in Winsock—use Stream for TCP sockets. |
Method | Description |
Accept, BeginAccept, EndAccept | Same as Winsock Accept; in addition, there is an asynchronous BeginAccept and EndAccept pair. We will describe aynchronous .NET Sockets later in the chapter. |
Bind | Same as Winsock bind. |
Close | Same as Winsock closesocket. |
Connect, BeginConnect, EndConnect | Same as Winsock connect; in addition, there is an asynchronous BeginConnect and EndConnect pair. |
GetSocketOption | Similar to Winsock getsockopt. |
IOControl | Similar to Winsock ioctlsocket. |
Listen | Same as Winsock listen. |
Poll | Can be used to determine the status of a socket, such as if data is available to be read. |
Receive, BeginReceive, EndReceive | Similar to Winsock recv; in addition, there is an asynchronous BeginReceive and EndReceive pair. |
ReceiveFrom, BeginReceiveFrom, EndReceiveFrom | Similar to Winsock recvfrom; in addition, there is an asynchronous BeginReceiveFrom and EndReceiveFrom pair. |
Select | Similar to Winsock select. |
Send, BeginSend, EndSend | Similar to Winsock send; in addition, there is an asynchronous BeginSend and EndSend pair. |
SendTo, BeginSendTo, EndSendTo | Similar to Winsock sendto; in addition, there is an asynchronous BeginSendTo and EndSendTo pair. |
SetSocketOption | Similar to Winsock setsockopt. |
Shutdown | Similar to Winsock shutdown. |
The following C# code demonstrates how to create a stream socket to communicate over TCP using IPv4:
using System; namespace MySocketApplication { class MySocketClass { static void Main(string[] args) { System.Net.Sockets.Socket MySocket = new System.Net.Sockets.Socket( System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.IP ); } } }