ArticleDirectory
- 1. Error Handling canceled accept completion notification
- 2. The completion port is suitable for short connections.
- 3. Improper use of memcpy
- 4. When the socket is closed
- 5. incorrectly process TCP Data Streams
Original article JohnsonFeng@yeah.net
Dedicated to online game server. Client Engine Development
Iocp is an asynchronous socket body mechanism. It is an API for managing asynchronous Io operations in windows. Use iocp, ApplicationProgramYou can send a time-consuming request and execute other tasks. The request is executed in the kernel and the corresponding driver is called to complete the IO operation steps. In a large-scale C/S architecture, ports are often used to manage a large number of socket connections. RecentlyCodeSome common errors have been found in some reference books of the bookstore. Here, we will summarize the following Frequently Asked Questions for your reference.
1. Error Handling canceled accept completion notification
In some cases, the application layer does not process the connection request, and the connection is still waiting. If the client is disconnected for some reason before the request is processed, getqueuedcompletionstatus returns false. In some reference books, the processing of this return value is ignored, but this is an error. First, the return of false does not indicate that the port has been completed, and if it is ignored, it will lead to the loss of an accept asynchronous request. Generally, an application creates many accept requests on a listening port to improve the connection success rate. Each time an application processes a connection request, it should post another request so that the port is always receiving the connection asynchronously, the asynchronous accept request on the listening port is fixed. If the application ignores the completion result of these connection requests because getqueuedcompletionstatus returns false, the number of accept requests will decrease. When this number is reduced to 0, the application will not receive any accept notification, that is, you may not know that a client is connected. Of course, a few clients may be connected at the beginning, but the server is not notified, when the number of connected queues exceeds the configured upper limit (set by the listen function), any connected requests directly fail. I have already seen this error in many reference books and online articles. If your iocpserver cannot be connected, this may be the reason.
TIPS: when the connection request queue is full, telnet is used to connect to the queue and a connection failure is directly returned. This is the same as the port is not opened, but at this time, this port is opened when netstat-A is used on the server.
The correct method is to close the socket first and then deliver a request.
2. The completion port is suitable for short connections.
For example, HTTP server and P2P server. In fact, iocp is not required in many cases. You can simply use the select model, such as vsftp.
3. Improper use of memcpy
If the source range and target address range of this memcpy copy overlap, a pending error will occur, and similar errors will also occur in strcpy. The correct method is to use memmove;
4. When the socket is closed
A connection is closed in many cases:
A. the client is actively disconnected. At this time, it should be noted that getqueuedcompletionstatus may return one or two times because of disconnection. Generally, the request is always accepted, so at least one time is returned because of the asynchronous call of wsarecv; wsasend may not occur or is in progress when it is disconnected, and a completion event will occur. Therefore, when dealing with this situation, you need to determine whether there are two events, to avoid the first direct cast of resources, the second view to close the relevant resources and cause server logic errors.
B. The server is actively disconnected, and the server is actively disconnected. We recommend that you use closesocket and submit it to situation A for processing.
C. network interruption and notification are provided. In this case, the handling is the same as that of.
D. network interruption and no notification. Due to the characteristics of the TCP protocol, if no data needs to be transmitted and no data is sent, the server cannot know if a client suddenly crashes, unless Data fails to be sent. When no data is sent, generally, two methods can be used for processing. One is to regularly send a synchronous message. When the sending or processing fails, the connection is disconnected; another method is to often check the time when the client is not active. If the client is disconnected after a certain period of time, and of course the client still needs data transmission, establish a connection again, for example, HTTP server uses this method. When a Web page is opened in webbrowser for a long time, the server can be disconnected after a certain period of time to cast server resources.
5. incorrectly process TCP Data Streams
TCP data is transmitted by stream to ensure that data arrives in sequence. However, TCP does not ensure that data is transmitted by call and data packets are transmitted one by one, which may be decomposed or merged, this is common knowledge about TCP.
If your c/s program contains application-layer messages that shouldn't have occurred, connection failures, or frequent disconnection issues, this article may be helpful for solving the problem.
Refer:
Getqueuedcompletionstatus API documentation http://msdn.microsoft.com/en-us/library/aa364986 (vs.85). aspx
Vsftp (very safe FTP): http://vsftpd.beasts.org/