[英]What might cause an infinite loop error
I am working on a network programming and I have this code 我正在进行网络编程,我有这段代码
void WorkHandler::workLoop(){
.
.
.
while(1){
if(remainLength >= MAX_LENGTH)
currentSentLength = send(client->getFd(), sBuffer, MAX_LENGTH, MSG_NOSIGNAL);
else
currentSentLength = send(client->getFd(), sBuffer, remainLength,MSG_NOSIGNAL);
if(currentSentLength == -1){
log("WorkHandler::workLoop, connection has been lost \n");
break;
}
sBuffer += currentSentLength;
remainLength -= currentSentLength;
if(remainLength == 0)
break;
}
}
Also, I am creating a child thread like this 另外,我正在创建这样的子线程
bool WorkHandler::initThreads(){
for(int i=0; i < m_maxThreads; i++){
pthread_t *thread(new pthread_t);
m_workThreadList.push_back(thread);
if(pthread_create(thread, NULL, runWorkThread, reinterpret_cast<void *>(this))!=0){
log("WorkHandler::initThreads, pthread_create error \n");
return false;
}
pthread_detach(*thread);
}
return true;
} }
void* WorkHandler::runWorkThread(void *delegate){
printf("WorkHandler::runWorkThread, called\n");
WorkHandler *ptr = reinterpret_cast<WorkHandler*>(delegate);
ptr->workLoop();
return NULL;
}
I am running this code on gdb and it doesn't blow up but it gets stuck at the second send function in the if then else loop. 我在gdb上运行此代码,它不会崩溃,但是卡在if then else循环的第二个send函数中。 I put log statements every single line and it prints a log right above the second send function and stopped. 我在每一行中放置了日志语句,它在第二个send函数上方打印了一条日志,并停止了。
currentSentLength = send(client->getFd(), sBuffer, remainLength, MSG_NOSIGNAL);
What might cause this problem and how do I fix this issue? 什么可能导致此问题,以及如何解决此问题? Thanks in advance.. 提前致谢..
With blocking IO send will block
if the kernel buffer is full and will block untill the clients have read the data. 使用阻塞IO时,如果内核缓冲区已满,则send将block
,并且将阻塞直到客户端读取数据为止。 Do you send large chunks? 您发送大块吗? If so, check your client. 如果是这样,请检查您的客户。
If you don't trust clients (they can abuse this to do denial of service attacks) there are a couple of ways to do this properly: poll (with timeout) on the sockets for writeability, send with timeout, use nonblocking I/O, ... 如果您不信任客户端(它们可能会滥用此地址进行拒绝服务攻击),则有两种方法可以正确地执行此操作:在套接字上轮询(超时)以提高可写性,超时发送,使用非阻塞I / O ,...
我猜您正在以负大小调用send()...退出while的测试应为keepLength <= 0而不是keepLength == 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.