简体   繁体   English

什么可能导致无限循环错误

[英]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.

相关问题 这会导致无限循环吗? - Might this cause an infinite loop? 在C ++ int数组中寻找最小值时,可能导致间歇性错误的原因是什么? - What might cause intermittent error in finding the lowest value in a C++ int array? istream运算符重载&#39;&gt;&gt;&#39;导致无限循环 - Istream operator overloading '>>' cause infinite loop clang-tidy:什么可能导致 NOLINT 评论不被尊重? - clang-tidy: what might cause NOLINT comments to not be respected? 内联替换会导致多线程代码中的无限循环吗? - Can inline substitution cause an infinite loop in multithreaded code? 无限循环的原因? (迭代中序遍历实现)C++ - Cause of Infinite Loop? (iterative inorder traversal implementation) C++ 已经定义的错误和无限循环 - already defined error & infinite loop 什么循环可用于可能在结束前中断的迭代? - What loop to use for an iteration that might be interrupted before the end? 可能导致在不同角度获得完全相同的三角函数输出的原因 - What might cause to get exactly the same output of trigonometric functions in different angles 什么可能导致相同的SSE代码在同一个函数中运行速度慢几倍? - What might cause the same SSE code to run a few times slower in the same function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM