"1 Background" in socket communication, in order to transfer structured data or to carry out protocol data transmission, the sender must construct the structure for data transmission. The receiving end must also be parsed by the same structure. But when the socket transfer structure data, a little careless will appear: 1) parse the data error, 2) received incomplete data, 3) resolved to garbled and other bugs.
The "2 Example" below is a common type of error that the receiving side parses data for garbled or even crashing.
the structure is a contiguous memory. But the structure is similar to the following:
typedef struct _PER_SPIDER_INFO
{
UINT Ntimedelay;
UINT Nrtncode;
UINT Nurlpagelen;
char* Pszurlpage;
}per_spider_info;
cannot be transmitted directly through the. "3 root causes"as follows:1) struct member Pszurlpage is a pointer to a memory address other than a struct, that is, a non-contiguous address space outside the structure's per_spider_info. 2) If only pszurlpage is transmitted, that is, an address is transmitted, the current process space can be indexed to the corresponding value. But once the network transmission through the socket, that is, cross-process, cross-network transmission, an address transmission in the past, it is likely that even 100% will read the address space we do not want, read the value of the error . Once the address space is not allowed to read, it causes the program to crash. (as shown) 3) can be transmitted, as 2) said, but once accepted the other machine can not parse correctly, even the program crashes.
"4 Bug resolution Step Roadshow" STEP1: Narrow the range, transfer only int data, OK; But a single string pointer is not allowed; basic locking is a string here except for the problem.STEP2: The string pointer is replaced by a string array, and the bug no longer occurs. Description at least contiguous memory here's a problem. Period, suspected of dynamically allocating memory malloc () and strcpy,memcpy, the length of the data in the transmission structure was out of the question, and it took nearly 3 hours to troubleshoot. STEP3: And then replaced by a given length (size combined with the project) array of strings, the bug resolved out.
"5 modified" typedef struct _PER_SPIDER_INFO
{
UINT Ntimedelay;
UINT Nrtncode;
UINT Nurlpagelen;
Char Szurlpage[max_url_path]
}per_spider_info, it can be transmitted through the socket.
"6 Reflections and Reflections" but not the pointer can not transfer it? No, as long as the proper memory processing is also possible. Back there is an example attached to the parsing. Not to be continued.
Ming Yi World
Reprint please indicate source, original address: http://blog.csdn.net/laoyang360/article/details/47342541
If you feel this article is helpful, please click on the ' top ' support, your support is I insist on writing the most power, thank you!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Socket transfer Structure Data considerations