In the networked environment, the two namespaces we are most interested in are System.Net and System.Net.Sockets. System.Net namespaces are typically associated with higher elevation operations, such as download or upload, trial HTTP and other protocols for Web requests, and so on, while System.Net.Sockets namespaces contain classes that are typically associated with lower-path operations. This namespace class is useful if you want to use a protocol such as sockets or TCP/IP directly.
In. Net, the System.Net.Sockets namespace provides a managed implementation of the Windows Sockets (Winsock) interface for developers who need to tightly control network access. All other network access classes in the System.Net namespace are based on this socket socket implementation, such as the TcpClient, TcpListener, and UdpClient classes encapsulate details about the TCP and UDP connections that are created to the Internet The NetworkStream class provides the underlying data stream for network access, and many common Internet services can see the socket, such as Telnet, Http, Email, Echo, and so on, which, although the definition of communication protocol protocol is different , but the underlying transmission is the socket used.
In fact, a socket can be considered a data channel like a stream stream, which is built between the application side (client) and the remote server, and then the read (receive) and write (send) of the data are directed at this channel.
Visible, after the socket object is created on the application or server side, you can use the Send/sentto method to send data to a connected socket or use the Receive/receivefrom method to receive data from the connection socket.
For socket programming, the. NET Framework socket class is the managed code version of the socket service provided by the Winsock32 API. There are a number of ways to implement network programming, and in most cases the Socket class method simply marshals the data to their native Win32 copy and handles any necessary security checks. If you are familiar with the Winsock API functions, then use the socket class to write a Web program is very easy, of course, if you have not been contacted, it will not be too difficult, follow the explanations below, you will find that the use of the socket class to develop Windows network applications are originally subject to search, They follow roughly the same steps in most cases.
Before using, you need to create an instance of the socket object first, which can be done by constructing the socket class:
public Socket(AddressFamily addressFamily,SocketType socketType,ProtocolType protocolType);
Where the AddressFamily parameter specifies the addressing scheme used by the socket, the SocketType parameter specifies the type of the socket, and the ProtocolType parameter specifies the protocol used by the socket.
The following example statement creates a Socket that can be used to communicate on a TCP/ip-based network, such as the Internet.
Socket temp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
To use UDP instead of TCP, you need to change the protocol type, as shown in the following example:
Socket temp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Once you create the Socket, on the client side, you will be able to connect to the specified server via the Connect method and send data to the remote server via the Send/sendto method, which can then receive data from the server via Receive/receivefrom; You need to bind the specified interface with the Bind method to make the socket connected to a local endpoint and listen for requests on that interface through the Listen method, calling accept to complete the connection, and creating a new socket to handle incoming connection requests, when the connection is heard from the client. When you are finished using the socket, remember to disable the socket using the Shutdown method and close the socket using the closing method.
As you can see, many of these methods contain parameters for the endpoint type, in which TCP/IP uses a network address and a service port number to uniquely identify the device. The network address identifies a specific device on the network, and the port number identifies the specific service on the device to which you want to connect. The combination of a network address and a service port is called an endpoint, and it is the EndPoint class that represents the endpoint in the. NET framework, providing an abstraction that represents a network resource or service, to mark information such as a network address. NET also defines the descendants of EndPoint for each supported address family, and for the IP address family, the class is IPEndPoint. The IPEndPoint class contains the host and port information required by the application to connect to the service on the host, and the IPEndPoint class forms to the service's connection point by combining the host IP address and port number of the service.
When the IPEndPoint class is used, it inevitably involves the IP address of the computer, and there are two kinds of system.net namespaces that can get an IP address instance:
IPAddress class: The IPAddress class contains the address of the computer on the IP network. Its Parse method converts an IP address string to an IPAddress instance. The following statement creates a IPAddress instance:
IPAddress myIP = IPAddress.Parse("192.168.0.1");
Dns class: Provides domain name services to applications that use TCP/IP Internet services. Its Resolve method queries the DNS server to map user-friendly domain names (such as "host.mydomain.com") to digital forms of Internet addresses (such as 192.168.0.1). The Resolve method returns a Iphostenty instance that contains a list of addresses and aliases for the requested name. In most cases, you can use the first address returned in the AddressList array. The following code gets a IPAddress instance that contains the IP address of the server host.mydomain.com.
IPHostEntry ipHostInfo = Dns.Resolve("host.mydomain.com ");
IPAddress ipAddress = ipHostInfo.AddressList[0];