Jump to content
  • Advertisement
Sign in to follow this  
arithma

polling a socket and receiving data [previously titled as 'ioctlsocket']

This topic is 4043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there anything wrong with using ioctlsocket/FIONREAD to poll a socket for incoming data (TCP)? It's of course for a game, so it's gonna be done very often (once a multiple of frames, or even on each frame).. [Edited by - arithma on May 25, 2007 5:44:51 AM]

Share this post


Link to post
Share on other sites
Advertisement
OK, so after select asserts there is actually data, how should I continue to know how big the buffer I should supply, or should I just: select, recv, select, recv, select, no data...?

Share this post


Link to post
Share on other sites
The socket should be non-blocking, so you can just keep doing recv() till it tells you that the operation would block.

From my code:

void PNetworkClient::OnRead(int nError)
{
if(nError)
{
ELog::Get().DebugFormat(L"NETWORK : Read from %s failed. Error: %s\n",
GetRemoteIP().ToString().c_str(), PNetworkMgr::FormatWindowsError(nError).c_str());
CleanupSocket();
return;
}

int nRet;
char szBuff[256];
do {
nRet = recv(m_sock, szBuff, sizeof(szBuff), 0);
if(nRet == SOCKET_ERROR)
{
int nError = WSAGetLastError();
if(nError != WSAEWOULDBLOCK)
{
ELog::Get().DebugFormat(L"NETWORK : Read from %s failed. Error: %s\n",
GetRemoteIP().ToString().c_str(), PNetworkMgr::FormatWindowsError(nError).c_str());
CleanupSocket();
}
}
else if(nRet == 0)
{
// Graceful close
CleanupSocket();
}
else
{
size_t nReadSize = (size_t)nRet;
if(m_nMaxRecvBufferSize > 0 && m_vRecvBuffer.size() + nReadSize > m_nMaxRecvBufferSize)
nReadSize = m_nMaxRecvBufferSize - m_vRecvBuffer.size();
if(nReadSize > 0)
{
size_t nOldLen = m_vRecvBuffer.size();
m_vRecvBuffer.resize(nOldLen + nReadSize);
memcpy(&m_vRecvBuffer[nOldLen], szBuff, nReadSize);
}
}
} while(nRet == sizeof(szBuff));
}



That code is called when select() says there's data to read.

Share this post


Link to post
Share on other sites
Quote:
Original post by arithma
Are you absolutely sure about passing std::vectors to the Winsock2 DLL?


Where does it say that (memcpy is not part of the WS library)? Or do you want to do that yourself?

Share this post


Link to post
Share on other sites
I just guessed from the .size()...
I was asking "Evil Steve" whether he was using std::vectors as buffers and passing them to Winsock.. that's all

Share this post


Link to post
Share on other sites
As far as I can see, he's not. He's pulling data into a temporary buffer allocated on the stack, and then proceeds by copying it to a UDT using memcpy. It does look like an stl-UDT though, yes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!