UDP Tx 循环每次恰好在 3970 个数据包处停止工作,在我朋友的计算机上为 14386 个数据包

发布于 2024-11-18 15:59:08 字数 1640 浏览 5 评论 0原文

在过去的四天里我一直被这个问题困扰,但我似乎无法弄清楚。我正在尝试使用 UDP 数据包向自己发送数据,以便另一个程序可以读取字节。

我可以清楚地读取数据包,但在程序的 UDP 部分挂起之前我只能读取到 3970。过剩和其他一切都继续运行良好。我给了我的朋友同样的代码,他在他的计算机上运行了它。在挂起之前他进行了 14386 次迭代。变量 temp 将计算发送的数据包数量。 -1 不好。计数器对 while 循环迭代进行计数。我正在遵循此处的示例:

http:// msdn.microsoft.com/en-us/library/ms740148(v=vs.85).aspx

示例代码:

#include "stdafx.h"
#include <WinSock2.h> // don't forget to add in 
//Project Properties>Linker>Input>Additional Dependences [Ws2_32.lib]
sockaddr_in dest;
sockaddr_in local;
WSAData data;
static void SUDP_init(void)
{
    printf("--[UDP Socket Initialized]--\r\n");
    WSAStartup( MAKEWORD( 2, 2 ), &data );

    local.sin_family = AF_INET;
    local.sin_addr.s_addr = inet_addr( "127.0.0.1" ); //same as localhost
    local.sin_port = 6000;

    dest.sin_family = AF_INET;
    dest.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    dest.sin_port = htons( 6000 );

    bind( socket( AF_INET, SOCK_DGRAM, 0 ), (sockaddr *)&local, sizeof(local) );
    printf("Socket Bound...\r\n");
}
static int counter = 0;
int _tmain(int argc, _TCHAR* argv[])
{
    SUDP_init();
    while(1){
        char packet[30];
        sprintf(packet, "%0.3f,%0.3f,%0.3f", 
        55.4,
        16.1,
        -27.88);

        int temp = sendto( socket( AF_INET, SOCK_DGRAM, 0 ), packet, strlen(packet), 0, (sockaddr *)&dest, sizeof(dest) );
        if(temp>=1){
            counter++;
        }
        printf("Bytes Sent: %d, Counter: %d\r\n",temp,counter);
    }
    return 0;
}       

I've been stuck at this issue for the last 4 days and I can't seem to figure it out. I'm trying to send data to myself using UDP packets so another program can read the bytes.

I can clearly read the packets but I only get up to 3970 before the UDP portion of the program hangs. Glut and everything else continues to run fine. I gave my friend the same code and he ran it on his computer. He got 14386 iterations before it hangs. variable temp will count the number of packets sent. -1 is bad. counter counts the while loop iterations. I'm following the example here:

http://msdn.microsoft.com/en-us/library/ms740148(v=vs.85).aspx

Example Code:

#include "stdafx.h"
#include <WinSock2.h> // don't forget to add in 
//Project Properties>Linker>Input>Additional Dependences [Ws2_32.lib]
sockaddr_in dest;
sockaddr_in local;
WSAData data;
static void SUDP_init(void)
{
    printf("--[UDP Socket Initialized]--\r\n");
    WSAStartup( MAKEWORD( 2, 2 ), &data );

    local.sin_family = AF_INET;
    local.sin_addr.s_addr = inet_addr( "127.0.0.1" ); //same as localhost
    local.sin_port = 6000;

    dest.sin_family = AF_INET;
    dest.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    dest.sin_port = htons( 6000 );

    bind( socket( AF_INET, SOCK_DGRAM, 0 ), (sockaddr *)&local, sizeof(local) );
    printf("Socket Bound...\r\n");
}
static int counter = 0;
int _tmain(int argc, _TCHAR* argv[])
{
    SUDP_init();
    while(1){
        char packet[30];
        sprintf(packet, "%0.3f,%0.3f,%0.3f", 
        55.4,
        16.1,
        -27.88);

        int temp = sendto( socket( AF_INET, SOCK_DGRAM, 0 ), packet, strlen(packet), 0, (sockaddr *)&dest, sizeof(dest) );
        if(temp>=1){
            counter++;
        }
        printf("Bytes Sent: %d, Counter: %d\r\n",temp,counter);
    }
    return 0;
}       

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2024-11-25 15:59:08

您在循环中分配新的套接字(sendto 的第一个参数),您还为 bind 分配另一个套接字,但这些套接字永远不会被释放。您最终会用完要分配的套接字句柄,因此您的程序会挂起。

相反,在 SUDP_init 中分配一次套接字,存储它而不是丢弃它,然后将其传递给 bindsendto

You are allocating new sockets in a loop (the first argument to sendto), you are also allocating another for bind, but these are never freed. You eventually run out of socket handles to allocate, hence your program hanging.

Instead, allocate a socket once in SUDP_init, store it instead of discarding it, then pass that to bind and sendto

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文