A. First of all understand the principle of the transmission model of three kinds of action
Because TCP is streaming socket, socket received data, stick may not be a complete package or bag, now need to package the application layer for unpacking group, such as the client sent three consecutive packet size, respectively is: 300500100, but the receiver to receive the data may be 200400100200, so this time we deal with the data received will set of packages and unpacking,
Because of a shortage of 200 a packet, so need to combine the next packet, 400, the data size is 600, but the client sent the first packet is 300, so at this point needs to be broken down into 600 300 + 300, 300, at this time you can get the first packet data remaining 300, but the second packet is 500, data not enough so that need to be a combination of 100 a total of 400 data, a bag is still not enough, to continue the remaining 200 group package, this time to accept the data of 600, 500, enough for a packet so will be apart for 600 500 + 100, 500, get the second packet data remaining 100, just 100 matches and third packets, packet analysis is completed, the basic logic
While (true)
{
Data_size=recv_data ();
If (data_size & lt; Packet length)
continue;//continue to receive data
///unpacking, continue to accept data when not enough a package waiting for a complete package
While (true)
{
//packet length, enough unpacking
Data_size -=packet length;
//update the data buffer, processing package
Hanle_pack (pack);
If (data_size & lt; Packet length)
break;
}
}
So above all model:
PUSH: after receive the data start OnReceive, calculated by the developers themselves to achieve the unpacking and buffer management logic,
PULL: start OnReceive after receive the data, but developers only need to maintain the length of the packet, HP internal data buffer management to help you do it, when enough of a package, the Fetch to obtain a complete packet with respect to OK,
PACK: after receive the data start OnReceive, developers don't have to manage the process above, OnReceive is a complete package of data
The original link: https://blog.csdn.net/CAir2/article/details/82188721
2.
3. The categories of HP - Socket components, details please refer to "HP - Socket network communication framework development guide" :
Server: communication model based on IOCP/EPOLL, and connecting with the buffer pool, private heap, and other technology to achieve efficient memory management, support for very large scale, high concurrency communication scenarios,
Agent: the Agent component is essentially the Multi - Client component, and Server components using the same technology architecture, an Agent component object can establish and efficient processing large-scale Socket connection at the same time,
Client: communication model based on Event the Select/POLL, each component object to create a thread communication and manage a Socket connection, is suitable for the small-scale Client scenario,
4. Working process
Create a listener
Create communication component (and binding listener)
Start the communication component
Connect to the target host components (Agent)
Handle communication event (OnConnect OnReceive/OnClose, etc.)
Stop communication components (optional: in step 7 destroyed communication component automatically stop component)
Destroy the communication component
Destroy the listener
# include & lt; Hpsocket/hpsocket. H>
/* Listener Class */
The class CListenerImpl: public CTcpPullServerListener
{
Public:
//5. The process network events
Virtual EnHandleResult OnPrepareListen (ITcpServer * pSender, SOCKET soListen);
Virtual EnHandleResult OnAccept (ITcpServer * pSender, CONNID dwConnID, UINT_PTR soClient);
Virtual EnHandleResult OnHandShake (ITcpServer * pSender, CONNID dwConnID);
Virtual EnHandleResult OnReceive (ITcpServer * pSender, CONNID dwConnID, int iLength);
Virtual EnHandleResult OnSend (ITcpServer * pSender, CONNID dwConnID, const BYTE * pData, int iLength);
Virtual EnHandleResult OnClose (ITcpServer * pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode);
Virtual EnHandleResult OnShutdown (ITcpServer * pSender);
};
Int main (int arg c, char * const argv [])
{
//1. Create a listener object
CListenerImpl s_listener;
//2. The Create component object (and binding with the lis
Tener object)
CTcpPullServerPtr s_pserver (& amp; S_listener);
//3. Start component object
if(! S_pserver - & gt; Start (" 0.0.0.0 ", 5555))
The exit (1);
/* wait for the exit */
//... .
//6. (optional) the Stop component object
S_pserver - & gt; Stop ();
return 0;
//7. Destroy component object automatically
//8. Destroy the listener object automatically
}
The original link: https://blog.csdn.net/xj9120/article/details/90692812