2013年9月8日星期日

A length on the socket connection problems

Procedure is used and telecommunications service providers for a long connection with the telecommunications SMGP 3.0 protocol.
program starts, thread management library called boost to start a thread function SendData (string falg);
m_threadpool.schedule (boost :: bind (& SMGPCT :: SendData, this, "SMGPCTMT"));
In the thread function SendData (string falg) , the
do a while (1) loop , which first calls ServerRecvTask () to obtain task data from the middleware ,
Then , if there is no data for 30 seconds once will link test ActiveTest (), the problem is in this link test !
link test is to send a packet to telecommunications , and then wait for the receipt telecom package , and then by the program to determine to see acknowledgment packet is correct,
result is that if a short time interval , say, 1-5 seconds , then is recovered acknowledgment packet is correct, if more than this time, such as 30 seconds before the set , then the acknowledgment packet how are incorrect !
In simple terms it looks like this :

SendData()
{
   while (1)
   {
      sleep(5); //正确的回执包
      //sleep(30);//错误的回执包
      ActiveTest();  
   }
}

ActiveTest () the socket is used Poco: Net of StreamSocket
------ Solution --------------------- -----------------------
inappropriate use sleep it, you should be a non-blocking socket + timer to deal with this problem . sleep , then , sleep time, the chances would be lost package receipt .
------ Solution ---------------------------------------- ----
there are many ways of blocking the signal . But the easiest way is to use select nonblocking mode to achieve, given when calling select a timeout if received within the timeout time to read the data you did not receive it dormant .
------ Solution ---------------------------------------- ----
first you have to know that the data in the network there is a time limit , if you sleep , then that is blocked , because you said : there is no data in the case, for 30 seconds once the link test ActiveTest (), then the ActiveTest before sleep , and if time is too long , there will be data loss condition can be ActiveTest placed before sleep .
------ For reference only -------------------------------------- -
Thank you for your reply , have been resolved.
original is because , telecommunications side also made ​​a heartbeat packets caused.

没有评论:

发表评论