如何使 c++ 中的 recv() 函数超时?
我有一个在服务器上运行的程序与在客户端上运行的另一个程序进行通信。它们都来回发送数据和文件。
我注意到,每当有 socHandler.read() (读取套接字上传入的数据的函数)时,它就会卡住等待数据到达。
这就是函数的样子。
int CSocHandler::read(char * inBuffer, INT64 iBytesToRead){
bool blReadMore=false;
int retVal = 0;
int iOutputPointer = 0;
do{
blReadMore = false;
if (iStartOfBuffer == iEndOfBuffer) { //Buffer empty
if (blClosed ) {
return -1;
}
iStartOfBuffer = 0;
iEndOfBuffer = 0;
size_t bufferRemaining = BUFFER_SIZE-iEndOfBuffer;
int bytesRecvd = recv( sock, &buffer[iEndOfBuffer], (int)iBytesToRead<bufferRemaining?iBytesToRead:bufferRemaining), 0 );
if ( bytesRecvd <= 0) {
close();
Yield();
return retVal;
}
}
} while(blReadMore);
return retVal;
}
变量 sock 是 SOCKET 类型,并且是在其他地方定义的全局变量。如何设置选项或使此 recv() 调用仅针对此函数调用非阻塞,而不影响任何其他函数?
我相信这就是它在等待数据时陷入困境的地方,我想让它在 X 秒后超时。
I have a program running on a server communicating with another program running on the client. They both send data and files back and forth.
I notice that whenever there is a socHandler.read() (function to read data coming in on socket), it gets stuck waiting for data to arrive.
This is what the function looks like.
int CSocHandler::read(char * inBuffer, INT64 iBytesToRead){
bool blReadMore=false;
int retVal = 0;
int iOutputPointer = 0;
do{
blReadMore = false;
if (iStartOfBuffer == iEndOfBuffer) { //Buffer empty
if (blClosed ) {
return -1;
}
iStartOfBuffer = 0;
iEndOfBuffer = 0;
size_t bufferRemaining = BUFFER_SIZE-iEndOfBuffer;
int bytesRecvd = recv( sock, &buffer[iEndOfBuffer], (int)iBytesToRead<bufferRemaining?iBytesToRead:bufferRemaining), 0 );
if ( bytesRecvd <= 0) {
close();
Yield();
return retVal;
}
}
} while(blReadMore);
return retVal;
}
The variable sock is a type SOCKET and is a global variable defined elsewhere. How can I set the options or make this recv() call non-blocking only for this function call and no affect any other function?
I believe this is where it gets stuck waiting for data, and I want to make it timeout after X seconds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用
select()
查询套接字是否有任何未决数据,然后调用recv()
读取数据。Call
select()
to query if the socket has any pending data before then callingrecv()
to read it.在
recv
之前在套接字上使用select(2)
。使用 Perl 语法,
can_read
看起来像是对
select(2)
的调用。Use
select(2)
on the socket before therecv
.Using Perl syntax, it would look like
can_read
is a call toselect(2)
.