Article From:https://www.cnblogs.com/zhugaopeng/p/9688882.html

The first substantive contact socket communications work, so encountered a lot of problems, write a blog record, improve memory.

The requirement is to transmit binary data through private protocols, so we must ensure that the packets can not be lost, so we choose the socket of TCP to communicate.

1. The first problem encountered is that the socket communication between the client and the server does not maintain a continuous connection state.

   This is a mistake of course. It is assumed that TCP is connected communication, but if you don’t communicate for a long time, the server keeps communicating, which is a great waste of server resources. The connection between the client and the server needs some resources, and the resources of the server are limited.. If you are always connected, then the performance of your server must be problematic.

   The solution to this problem is to keep alive. The server sends the heartbeat package, and the client receives the response. The client tells the server that it is online, and does not disconnect. If the client does not respond for a long time, the server disconnects from the client and reduces resource usage.

 

2. Reading and writing of binary files

    The first attempt to read binary files took a piecewise read mode. During the testing process, a big problem was discovered: the read part of the file interrupted reading. Under debug, it is concluded that the empty character (‘\0’) is read. This directly causes a read to the content before it is lost, or even some.There will be empty characters in the case, and the client will crash if accepted.

   Since segmentation is not acceptable, simply read it all and send it in segments. The boost. ASIO library supports sending packages to vectors. When sent in segments, a binary stream is copied into the vector and sent as a whole.

 

3. Improving the transmission efficiency of binary streams

   Direct transmission of binary streams is not an efficient behavior, nor is it a safe behavior. Sending a binary stream directly is easily intercepted, resulting in information leakage.

   The way to improve the transmission efficiency of binary streams is encrypted compression and re transmission. Server encryption compression, client decryption decompression. There are two ways to do this:

   1)Global compression, segmented transmission

   2)Segmented compression, segmented transmission

   If the request is not very high, it is better to adopt a way. Simple and crude.

 

Paste some code to read binary files as a whole.

First of all, C++:

 1     string fileName = "D:/XtAmpClient/XtAmpTradeClient_x64_3.0.1.14473.exe";
 2     ifstream ifs(fileName, ios_base::binary);
 3 
 4     filebuf* pbuf = ifs.rdbuf();
 5     int size = pbuf->pubseekoff(0, ios_base::end, ios_base::in);
 6     pbuf->pubseekpos(0, ios_base::in);
 7 
 8     char* buf = new char[size];
 9     pbuf->sgetn(buf, size);
10     ifs.close();
11     delete []buf;

The last is C:

 1     const char* fileName = "D:/XtAmpClient/XtAmpTradeClient_x64_3.0.1.14473.exe";
 2     FILE* fp = fopen(fileName, "rb");
 3     if (NULL == fp)
 4     {
 5         cout << "open file failed" << endl;
 6         return ;
 7     }
 8 
 9     fseek(fp, 0, SEEK_END);
10     long size = ftell(fp);
11     rewind(fp);
12 
13     char* buffer = (char*)malloc(sizeof(char) * size);
14     if (NULL == buffer)
15     {
16         cout << "malloc failed" << endl;
17         return ;
18     }
19 
20     int ret = fread(buffer, 1, size, fp);
21     if (ret != size)
22     {
23         cout << "reading failed" << endl;
24         return ;
25     }
26 
27     fclose(fp);
28     free(buffer);

C is a little better than C++, but C++’s code is obviously more succinct.

Leave a Reply

Your email address will not be published. Required fields are marked *