This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

6678使用sendto发udp包,该包在tasksleep后才被发出



  1. 我在做6678与PC端的网络通信,尝试使用6678向PC端循环发送(sendto)udp包,如下代码所示:
  2. for(i = 0; i < 10;i++){

    tmp = sendto(udpSendSock, pBuf, 100, 0, (PSA)&dstAddr, sizeof(dstAddr));

    if(tmp >= 0){

    printf("已发送%d个字符:\n", tmp);
    //printf("%s\n",pBuf);

    }else{
    printf("failed in sendto!\n");
    printf("sendto Fail Number: %d\n", fdError());
    }

    TaskSleep(1);
    }

  3. 我在sendto和TaskSleep两行各打上了断点。执行完sendto后,但程序正常向下执行,停止在了TaskSleep之前,printf的内容也在控制台里显示了出来,发送了XX个字符,而PC端的wireshark并没有收到sendto发出的udp包。让程序再次跑起来,TaskSleep后PC端收到了之前发出的UDP包。

  4. 有没有大佬知道为什么会出现这种现象呢,按理说sendto执行完,PC端就应该立即接收到对应的udp包啊。另外,我将TaskSleep去除或者让其TaskSleep(0)休眠0ms,PC端就只能收到最后一次for循环的sendto发出的包。

  5. 希望能够得到回复,非常感谢啊!!

  • 这个任务SLEEP了,你有其他任务在切换吗?中断响应是否正常呢》?

  • Thomas Yang1 你好,非常感谢你的回复。

    在ndk的NetworkOpen的挂钩函数里,我只创建了一个任务线程,就是之前所说的发送udp包的线程,所以sleep应该没有导致任务切换。

    按理说sendto被调用后,buffer中的数据会立即被发送出去,而不是我所遇到的sendto后没有反应,需要再调用TaskSleep数据才能发出。这就感觉TaskSleep像是唤醒了之前sendto一样。