TCP/IP Socket stops receiving  
Author Message
sedge9711





PostPosted: Visual C++ Language, TCP/IP Socket stops receiving Top

My VC++ program has 3 threads, each establishes a stream socket to a legacy system. The one socket (derived from CAsyncSocket) stops being notified of received data every so often. This sockets receives the most data of any of the 3 but still not a HUGE amount. It works fine on our NT system but the OnReceive notification stops every so often. I do a peek at it and there is data there. My clug now is to peek every second, and if there is data there, process it all. It usually resumes working after that for awhile. Any time the OnReceive callback is notified of data, I Receive & process the data until there is no more data there.

Here is my ReceiveMsg code:

CMsg* CClntSocket::ReceiveMsg()
{
#define MAX_TRYS 20
#define BUF_SIZE 8192

char flush_buf[BUF_SIZE];
DWORD dwBytesToBeRead=0;
CString sTemp;
int nRet, nCount;
long lSize;

BYTE buf[MAX_SIZE];//must be the size of the largest struct
try
{

IOCtl(FIONREAD, &dwBytesToBeRead);
while (dwBytesToBeRead != 0) {
nRet = Receive(&buf[0], 4);
if (nRet<0)
{
tm = CTime::GetCurrentTime();
CString* pStrTrace1 = new CString();
pStrTrace1->Format("%02d:%02d:%02d; Received %d bytes, flushing",
tm.GetHour(), tm.GetMinute(), tm.GetSecond(),nRet);
m_pMainWnd->SendMessage(WM_WRITE_TRACE, (WPARAM)pStrTrace1, 5);

nRet = Receive(&flush_buf[0], BUF_SIZE); /* flush buffer */
return NULL;
}

nCount=0;
while(nRet<4 && nCount<MAX_TRYS)
{
tm = CTime::GetCurrentTime();
CString* pStrTrace2 = new CString();
pStrTrace2->Format("%02d:%02d:%02d; While reading msg header -Read %d bytes of %ld; looping count:%d",
tm.GetHour(), tm.GetMinute(), tm.GetSecond(),nRet, 4, nCount+1);
m_pMainWnd->SendMessage(WM_WRITE_TRACE, (WPARAM)pStrTrace2, 1);
Sleep(100);
nRet += Receive(&buf[nRet], 4-nRet);
nCount++;
}
if(nCount>=MAX_TRYS)
{
tm = CTime::GetCurrentTime();
CString* pStrTrace3 = new CString();
pStrTrace3->Format("%02d:%02d:%02d; Failed to read complete -Read %d bytes of %ld; tried:%d times",
tm.GetHour(), tm.GetMinute(), tm.GetSecond(),nRet, 4, nCount+1);
m_pMainWnd->SendMessage(WM_WRITE_TRACE, (WPARAM)pStrTrace3, 5);
return NULL;
}
lSize = GetNumBuffer(buf, 0, 4);
if ((lSize<0) || (lSize > MAX_SIZE))
{
tm = CTime::GetCurrentTime();
CString* pStrTrace4 = new CString();
pStrTrace4->Format("%02d:%02d:%02d; Invalid Size read of %ld - returning NULL",
tm.GetHour(), tm.GetMinute(), tm.GetSecond(),lSize);
m_pMainWnd->SendMessage(WM_WRITE_TRACE, (WPARAM)pStrTrace4, 5);
nRet = Receive(&flush_buf[0], BUF_SIZE); /* flush buffer */
return NULL;
}

nRet = Receive(&buf[4], lSize);
nCount=0;
while((nRet<lSize && nCount<MAX_TRYS) && (nRet > -1))
{
tm = CTime::GetCurrentTime();
CString* pStrTrace5 = new CString();
pStrTrace5->Format("%02d:%02d:%02d; While reading msg body -Read %d bytes of %ld; looping count:%d",
tm.GetHour(), tm.GetMinute(), tm.GetSecond(),nRet, lSize, nCount+1);
m_pMainWnd->SendMessage(WM_WRITE_TRACE, (WPARAM)pStrTrace5, 1);
Sleep(1);
nRet += Receive(&buf[nRet+4], lSize-nRet);
nCount++;
}
if(nCount>=MAX_TRYS)
{
tm = CTime::GetCurrentTime();
CString* pStrTrace6 = new CString();
pStrTrace6->Format("%02d:%02d:%02d; Failed to read complete -Read %d bytes of %ld; tried:%d times",
tm.GetHour(), tm.GetMinute(), tm.GetSecond(),nRet, lSize, nCount+1);
m_pMainWnd->SendMessage(WM_WRITE_TRACE, (WPARAM)pStrTrace6, 5);
return NULL;
}

ProcessBuffer(&buf[0], lSize+SIZE_SIZE);

IOCtl(FIONREAD, &dwBytesToBeRead);
tm = CTime::GetCurrentTime();
CString* pStrTrace7 = new CString();
pStrTrace7->Format("%02d:%02d:%02d; Read complete the first time and Bytes To Be Read:%ld, Port: %d",
tm.GetHour(), tm.GetMinute(), tm.GetSecond(),dwBytesToBeRead, m_nPortType);
m_pMainWnd->SendMessage(WM_WRITE_TRACE, (WPARAM)pStrTrace7, 1);
}
}
catch(CFileException* e)
{
TCHAR szBuf[255];
e->GetErrorMessage(szBuf, 255);
tm = CTime::GetCurrentTime();
CString* pStrTrace8 = new CString();
pStrTrace8->Format("%02d:%02d:%02d; Could not read from socket because -%s",
tm.GetHour(), tm.GetMinute(), tm.GetSecond(),szBuf);
m_pMainWnd->SendMessage(WM_WRITE_TRACE, (WPARAM)pStrTrace8, 5);
}

return NULL;
}

All the sockets are derived from the AsyncSocket call and use the same OnReceive callback & then call this ReceiveMsg. The other two sockets still receive properly even if the one socket is "hung".



Visual C++11  
 
 
Ayman Shoukry - MSFT





PostPosted: Visual C++ Language, TCP/IP Socket stops receiving Top

For such issues, please use the win32 dev newsgroups at http://msdn.microsoft.com/newsgroups.

OTP

Thanks,
Ayman Shoukry
VC++ Team