HyperEngineer
Programmer
I'm having a problem with the recv() function hanging on me. If I call the recv() function with no flags it will hang there until data has arrived. So I put another recv() call with the MSG_PEEK flag, but it hangs. I don't know what is happening. Here is the program snippet that uses the recv():
Sorry for the long code snippet, but I left the log in stuff just in case I did something wrong there. Any help would be appreciated. I've been wresling with this for a week now.
thanks,
HyperEngineer
If it ain't broke, it probably needs improvement.
Code:
DWORD CSocketRx::RdTh(CSocketRx *pSocketRx)
{
char pBuf[512];
char* scan, * pPos, *msg;
int nRet, nRett, iCount = 0, nStateSpace = 0;
bool bLoggedIn = false;
msg = (char*) malloc(64);
// Clear the buffer
for (int i = 0; i < 512; i++) pBuf[i] = '\0';
// Can't go anywhere until the main thread is ready.
nRet = WaitForSingleObject(pSocketRx->m_TelnetDlg->m_hStartSessionEvent,INFINITE);
while(1)
{
if (pSocketRx->m_nExit == 1)
{
break;
}
nRet = recv(pSocketRx->m_hSocket, pBuf, 1, MSG_PEEK);
if (nRet > 0)
{
nRet = recv(pSocketRx->m_hSocket, pBuf, sizeof(pBuf), 0);
}
else
{
nRet = IdleFunction();
continue;
}
if ( nRet == SOCKET_ERROR )
{
MessageBox(NULL, "RX socket failed", "RX Socket", MB_OK);
continue;
}
if ( nRet == 0)
{
Sleep(10);
continue;
}
scan = pBuf;
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// *************************** Log in sequence *********************************
//
////////////////////////////////////////////////////////////////////////////////////////////////////
if (!bLoggedIn)
{
if ((pPos = strstr((const char*) scan, "Username:")) != NULL)
{
// Send username
pSocketRx->m_TelnetDlg->m_cSendCommand = pSocketRx->m_TelnetDlg->m_cUsername;
SetEvent(pSocketRx->m_TelnetDlg->m_hSendRequestEvent);
}
else if ((pPos = strstr((const char*) scan, "Password:")) != NULL)
{
// Send password
pSocketRx->m_TelnetDlg->m_cSendCommand = pSocketRx->m_TelnetDlg->m_cPassword;
SetEvent(pSocketRx->m_TelnetDlg->m_hSendRequestEvent);
}
else if ((pPos = strstr((const char*) scan, "UltraLine Series3>")) != NULL)
{
// Get shell prompt
pSocketRx->m_TelnetDlg->m_cSendCommand = "system shell";
SetEvent(pSocketRx->m_TelnetDlg->m_hSendRequestEvent);
}
else if ((pPos = strstr((const char*) scan, "/ #")) != NULL)
{
// Log in is successful
bLoggedIn = true;
SetEvent(pSocketRx->m_TelnetDlg->m_hLoggedInEvent);
}
else
{
nRett = nRet;
while (nRett--)
{
pSocketRx->m_Protocol.TelnetProtcol(pSocketRx->m_hSocket, *scan++);
}
}
// Clear the buffer
while (nRet--)
pBuf[nRet] = '\0';
// If not logged in then continue
if (!bLoggedIn)
continue;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// *************************** End of Login Sequence ***********************
//
//////////////////////////////////////////////////////////////////////////////////////////////////
// We are logged in, wait for data to arrive and process it.
if (nRet > 0)
{
while(nRet--)
{
pSocketRx->m_Protocol.TelnetProtcol(pSocketRx->m_hSocket,*scan++);
pBuf[nRet] = '\0';
}
}
}
pSocketRx->m_Protocol.~CProtocolRx();
ExitThread(0);
return 0;
}
Sorry for the long code snippet, but I left the log in stuff just in case I did something wrong there. Any help would be appreciated. I've been wresling with this for a week now.
thanks,
HyperEngineer
If it ain't broke, it probably needs improvement.