4. IP endpoint
Now we will introduce the most important concept in. NET network development-IP endpoint, which is represented by the IPEndPoint type in the. NET base class library.
To understand it, you must start with TCP/IP.
As mentioned above, all computers connected to the network must have a unique IP address, which is used to partition different computers on the network. The problem is: multiple network applications may run on a network computer. They may use the same network interface to receive (or send) data from the network and share the same IP address, in this case, how do you forward data packets sent to the host to the real "demander "?
To solve this problem, the TCP/IP protocol designer introduced the concept of "Port", specifying that each application that provides network services must specify a "Port ", different network applications cannot use the same port.
See the following TCP packet structure:
Figure 4
Each TCP packet contains two ports (each port occupies two bytes and is a 16-bit binary value. Therefore, the maximum port value is 1 to the power of 2, is 65535 ). Source port indicates the port used by the network application that sends this packet, and Destination port indicates the port used by the network application of the receiver.
In this way, the recipient's computer can forward this packet to a real network application based on the Destination port.
On Windows, the TCP/IP Driver (tcpip. sys in the WindowsSystem32drivers folder) in the operating system kernel is responsible for processing TCP packets.
The port problem is solved, but we do not see the IP address in the TCP packet? Without this address, How do I know which computer should I send data packets?
The method is as follows.
Because the TCP protocol is built on the IP protocol, the TCP packet is carried by the IP packet (figure 5 ).
Figure 5
Figure 5 clearly shows the Host IP Address (Source IP Address) that sends the packet and the Host IP Address (Destination IP Address) that receives the packet.
The Data section in Figure 5 contains TCP packets.
We can see that the IP address and port uniquely identify the network application in a network. We call this combination "IP EndPoint )", an IP EndPoint is an access point of a network service.
Tip:
Similarly, in WCF, there is also a service endpoint (ServiceEndpoint), which represents the access point of a WCF Service. Have you seen the connection between various fields of software technology?
In. in NET, the IPEndPoint class is used to represent an IP EndPoint (figure 6), which is derived from the abstract base class EndPoint. Pay attention to the important information expressed by its three attributes (AddressAddressFamilyPort.
Figure 6
In. NET network applications, a Socket object must be bound to an IPEndPoint object.
For convenience, we have compiled a static method GetRemoteMachineIPEndPoint and added it to the AddressHelper static class described earlier:
/// Generate valid remote host access endpoints in interactive mode, applicable to console programs
Public static IPEndPoint GetRemoteMachineIPEndPoint ()
{
IPEndPoint iep = null;
Try
{
Console. Write ("Enter the IP address of the remote host :");
IPAddress address = IPAddress. Parse (Console. ReadLine ());
Console. Write ("enter the port number opened by the remote host :");
Int port = Convert. ToInt32 (Console. ReadLine ());
If (port> 65535 | port <1024)
Throw new Exception ("the port number should be an integer in the range ");
Iep = new IPEndPoint (address, port );
}
Catch (ArgumentNullException)
{
Console. WriteLine ("the input data is incorrect! ");
}
Catch (FormatException)
{
Console. WriteLine ("the input data is incorrect! ");
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
}
Return iep;
}
The following example will use this method to create a large number of IP endpoints for Socket binding.
1. Which port can I use?
In actual development, you often need to specify a port for a network service dynamically to avoid conflicts with some important network service programs (for example, port 80 is fixed as used by the Web server) generally, this port must be in the range.
Now the question is how to select an unused port from it? You must know that there may be multiple network applications running on a computer, and they may be different in different time periods.
IPAddress. any can be used when a socket object is bound with an IPAddress. when the Any object is used, Windows automatically assigns it an unused port number. When the socket object is no longer used and can be recycled, this port can be reused.
Therefore, we can get the following ideas:
Create a socket object, bind it to the IPAddress. Any object, extract the port allocated by the system, and then destroy the socket object.
The Socket object implements the IDisposable Interface