About Network Programming

Source: Internet
Author: User

Network Programming has always been a persistent topic. Today we will summarize some problems in network programming. As UDP is comparatively simple to TCP, this summary is about TCP.

1. About the maximum number of TCP connections on the server.

Many people think that the maximum number of TCP connections on a single server is 65536, that is, the number of server ports is limited. If the server wants to open a TCP connection of more than, it is necessary to bind multiple IP addresses. In fact, this is a major misunderstanding. In fact, according to the provisions of the TCP/IP protocol, it is determined that the unique identifier of a connection is a 4 tuples <local IP address, local port, remote IP address, remote port>, because the port and IP address bound to the server are fixed (generally, only one port and IP address are bound to each process ), therefore, the factors that determine the connection are the remote IP address and remote port of the client, which have nothing to do with the number of server ports. That is to say, the IP address range and port number range of the client are the important factors that determine the connection, we know that the IP address range is 2 ^ 32 (here the IP address and port do not consider retention issues ), if the number of ports is 2 ^ 16, the theoretical value of the maximum number of connections maintained by the server is 2 ^ 32*2 ^ 16 = 2 ^ 48. But can the server really enable so many connections? The answer is to see how much physical memory the server has. That is to say, the physical memory size determines the number of connections that the server can open (of course, it is also related to the operating system settings, for example, the maximum number of file handles, but in general, it is limited by physical memory). If only 1 GB of physical memory is used, it is impossible to open a W connection. We know that establishing a socket connection is the same as opening a file. The operating system creates a file handle that points to a windows Kernel Object called a file object, the number of file objects created determines the maximum number of TCP connections. You can also think that the maximum number of TCP connections for a limited resource server is the maximum number of files that can be opened by the operating system, if the system resources are large enough, the maximum value is 2 ^ 48.

To verify that the number of I connections on the server does not matter to the port, I wrote a test program. This program uses the. net encapsulated IOCP method to shield the memory consumed by the thread stack.

The client testing machine uses three common PCs. Each of the three machines maintains, and connections with the server respectively, and checks whether the number of connections on the server is.

The program code is as follows:

1. server code:

/// <Summary> /// IOCP soket /// </summary> class Server {private Socket connSocket; // calculates the total number of connections. private static int count; public Server () {connSocket = new Socket (SocketType. stream, ProtocolType. tcp); IPAddress ip = IPAddress. parse ("192.168.1.123"); IPEndPoint endPoint = new IPEndPoint (ip, 6530); connSocket. bind (endPoint); connSocket. listen (50000);} // <summary> // receives client connection // </summary> public void Start () {IAsyncResult acceptResult = null; acceptResult = connSocket. beginAccept (AcceptCallback, connSocket);} private void AcceptCallback (IAsyncResult ar) {Socket accetpSocket = ar. asyncState as Socket; if (accetpSocket = null) {return;} Socket receiveSocket = accetpSocket. endAccept (ar); // print the number of connections count ++; Console. writeLine ("Connections: {0}", count); Start (); Receieve (receiveSocket );} /// <summary> /// receive sent data /// </summary> /// <param name = "receiveSocket"> socket for receiving data </param> private void Receieve (Socket receiveSocket) {byte [] buffer = new byte [28]; receiveSocket. beginReceive (buffer, 0, buffer. length, SocketFlags. none, (state) => {Socket receive = state. asyncState as Socket; try {int length = receive. endReceive (state); Console. writeLine (Encoding. unicode. getString (buffer); Receieve (receive);} catch (SocketException socketEx) {receive. dispose () ;}}, receiveSocket );}}

2. client code:

/// <Summary> /// test the client // </summary> class Program {static List <Socket> socketList = new List <Socket> (); static void Main (string [] args) {for (int I = 0; I <40000; I ++) {Socket socket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); int port = 6530; IPAddress ip = IPAddress. parse ("192.168.1.123"); // local socket. connect (ip, port); socketList. add (socket);} Console. readKey ();}}

3. Test results:

(1) Server running result:

Fig 3.1

(2) connection monitored by Tcpview:

Fig 3.2

(3) server resource usage:

Fig 3.3

According to the result in Figure 3.1, the number of connections accepted by the server is 10 W, so the maximum number of TCP connections is irrelevant to the number of ports on the server, in addition, according to Figure 3.2, the server port remains unchanged, but only the Client IP address and port are changed. Therefore, the unique identifier of a connection is a 4-tuple <local IP address, local port, remote IP, remote port>.

Further analysis assumes that all the memory of the program is occupied by connections. As shown in Figure 3.3, 10 million connections occupy 200 MB of memory, that is to say, each connection occupies 2 kb and occupies 4 GB physical memory. In 32 operating systems, the maximum number of open connections is approximately 100 W (2 GB addressing space is used in user mode ), however, even with 4 GB physical memory, connections may not be enabled, because address space in kernel mode is divided into paging memory pool and non-Paging Memory Pool (always paging in user mode ), file object the windows Kernel Object is a non-Paging memory pool stored in kernel mode, the so-called non-Paging memory pool means that the memory area is always in the physical memory instead of in the disk file Page Swap file, and the kernel mode paging memory pool also occupies part of the physical memory, as a result, the non-Paging memory pool of the entire kernel mode cannot reach 2 GB, so connections cannot be enabled in 32-bit operating systems. In the 64-bit operating system, theoretically there is no limit on memory size. Amazon has tested node. js connections: connections occupy 16 GB of memory (. net program has not tested W memory, so the 4G connections are just speculation, and the actual situation may be quite different) http://blog.caustik.com/2012/08/19/node-js-w1m-concurrent-connections /.

Finally, I would like to add that the performance of the server does not matter how many connections are processed. Even if connections only connect without sending data, the server just wastes some memory. If connections send data at the same time, the server may not be able to process the messages. If each connection message sent is very small, the server is configured, and the server's message processing logic is relatively simple, it may be processed. On the contrary, a connection sends only one message, but the logic of the server to process this message is very review. For example, it takes several minutes to perform large-scale computing, then the server cannot handle it. Therefore, it is not advisable for the server to blindly pursue the number of connections, mainly to measure the logic for processing messages.

Ii. Faster TCP connections than one connection

The TCP/IP protocol limits the maximum transmission speed of each connection. In addition, the speed of each major connection is maintained. Therefore, the data transmission speed of multiple connections is faster than that of a single connection, regardless of other factors such as the processing speed of the server, therefore, thunder uses multi-thread download. Of course, the number of connections we use in actual development needs to be calculated based on the server's processing capabilities for each connection to find a balance point.

Iii. Processing of byte order

In. when communication between platforms, you can ignore the host's bytecode issue ,. the net platform adopts the small-end method in a unified manner. If cross-platform transmission is required, a unified byte sequence is required. Generally, there are two solutions:

1. Use a string. Pay attention to the problem that UNICODE-encoded strings occupy 2 bytes, so there will also be a matter of byte order, so the ANSCI string should be used.

2. Both use big or small ends, and use one byte in the message to make a table to indicate the byte sequence of the host.

4. Handling TCP packet disconnection and sticking packets

Because TCP is a stream transmission, there is no data boundary, so we do not know the length of each data transmission. Therefore, each time we receive data, there may be two or an incomplete package, in this case, we need to deal with the TCP packet disconnection and sticky scenarios. The general solution is to identify the length of the entire packet in the first four bytes of the header, when receiving data, the system first accepts the length of 4 bytes, and then receives the data cyclically Based on the parsed packet length until the data packet is fully received, note that the packet length of the first four bytes must be received cyclically until the four bytes are fully received.

Sometimes an error occurs during data packet transmission. It is generally used to check whether CRC is complete to verify the correctness of the package. In case of a packet error, there are two testing methods: first, directly discard the entire packet. This method is simple but may lose some important data. The second is to traverse the entire packet based on the packet to keep the data that may not be wrong, but this solution is troublesome.

 

Last sentence: No matter what platform or language, the basic knowledge is universal. The factor restricting development is not the language we use, but the ability to use the basic knowledge to solve practical problems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.