当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Socket Programming, How do File descriptors Work and listening for incoming data

问题描述:

I am new to socket programming and c++, After initializing and binding the UDP socket, I just have to listen for incoming data (that should not come, else testcase should be failed) for a certain duration let's say 5 seconds. Is it good enough to use Select function for this purpose? Moreover can someone explain what all file descriptors are doing here, i am unable to get enough info for it? I got the code below that is working fine but i need to have understanding. In the code below, after checking that class is initialized and if the socket is connected it listens for any unexpected data for 5 sec.

if(m_bInitialized == false)

{

error("class not initialized");

return false;

}

if(INVALID_SOCKET == socket)

{

m_SetError("send() failure: not connected");

return false;

}

struct timeval Timeout;

Timeout.tv_sec = TimeoutSecs;

Timeout.tv_usec = 0;

FD_SET fdset;

FD_ZERO(&fdset);

FD_SET(socket, &fdset);

if(SOCKET_ERROR == select(0, &fdset, NULL, NULL, &Timeout))

{

m_SetError("select() failure: " + WSAGetLastError());

closesocket(socket);

return false;

}

if(FD_ISSET(socket, &fdset) == FALSE)

{

*result= 1;//update this value if timeout occurs

closesocket(socket);

return true;

}

return false;

分享给朋友:
您可能感兴趣的文章:
随机阅读: