使用winsock接收UDP数据包

发布于 2024-09-27 14:45:09 字数 3943 浏览 5 评论 0原文

我不断从端口收到 UDP 数据包。以下是来自wireshark的日志。 如何使用winsock编程连续接收这些数据包。我尝试过,但无法接收。在调用 recvfrom() 后,它不会写入缓冲区。请告诉我如何接收缓冲区中的每个数据包并将每个数据包写入文本文件。请帮我。预先感谢...

源 IP 是 192.168.13.25 &端口号是2780(源是一个会连续发送UDP数据包的硬件) 目标 IP 为 192.168.13.250 &端口号是45141(目标是我的电脑) 在我的代码中,我绑定到 192.168.13.250(PC)和端口 2780(硬件)。然后我调用recvfrom()。 Ip & 是否有不匹配的情况?港口?? 那么哪个 IP 和端口号 我需要从用户那里获取bind()和recvfrom()吗?

No  Time        Source          Destination     Proto  Info                                                                       
1   0.000000    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
2   0.000416    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
3   0.000846    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
4   0.001281    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
5   0.001716    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
6   0.002152    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
7   0.002589    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
8   0.003025    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141

以下是我的代码:

int main(void) {
    SOCKET recvSockID;
    WSADATA wsaData = {0};
    FILE *udp;
    FILE *fp ;

    struct sockaddr_in sock_addr;
    struct sockaddr_in cliAddr;

    static int recvData;
    int iResult = 0;
    int sock_len = sizeof(sock_addr);
    int sockCli_len = sizeof(cliAddr);
    int recvResult;
    static int iteration;

    fp = fopen("outOfSeq.txt","a");

    if((udp = fopen("udpData.txt","w")) == 0)
        printf("udpData.txt not opened\n");

    printf("\n Enter Destination IP Address : ");
    scanf_s("%s",inputData.destIPAddr,16);

    printf("\n Enter Destination port from which to receive data : "); 
    scanf_s("%d",&inputData.portNo,5);

    printf("\n Enter No.of iterations : "); 
    scanf_s("%d",&inputData.noIteration,2);

    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if(iResult < 0) {
        printf("windows socket startup error\n");
    } 

    recvSockID = socket(AF_INET, SOCK_DGRAM, 0);
    if(recvSockID < 0) {
        printf("Socket creation error\n");
        WSACleanup();
    }

    sock_addr.sin_family = AF_INET;
    sock_addr.sin_port = htons(inputData.portNo);
    sock_addr.sin_addr.s_addr = inet_addr(inputData.destIPAddr);
    //sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if(bind(recvSockID, (struct sockaddr *)&sock_addr,
        sizeof(struct sockaddr)) < 0)
    {
        printf("bind() failed: %ld.\n", WSAGetLastError());
        closesocket(recvSockID);
        return 0;
    }

    memset(udpBuf, 0, sizeof(udpBuf));

    iteration = inputData.noIteration;
    recvData = 1;

    while (recvData) {
        printf("receiving data\n");
        recvResult =  recvfrom(recvSockID, udpBuf, sizeof(udpBuf),
            0, (struct sockaddr *)&cliAddr, &sockCli_len); 

        if (recvResult <= 0) {
            printf("recvResult = %d\n", recvResult);

            printf("Error Code: %d",WSAGetLastError());

            printf("Socket receive()- error\n");
            return 0;
            //break;
            //goto exit;
        } else
            printf("Socket receive()- success\n");

        printf("completed rx data\n");

        fwrite(udpBuf, sizeof(udpBuf), 1, udp);
        memset(udpBuf, 0, sizeof(udpBuf));

        if (iteration != 0) {
            iteration--;
            if (iteration <= 0)
                recvData = 0;
        }
    }

//exit:
    if(udp) {
        fclose(udp);
        udp = 0; 
    }

    //shutdown socket
    closesocket(recvSockID);    
    fclose(udp);

    return 0;
}

I'm getting UDP packets from the port continuously. Following is the log from wireshark.
How to receive those packets continuously using winsock programming. I tried but I can't able to receive. After recvfrom() call it is not writing into a buffer.Give me idea, how to receive each packet in a buffer and write each packet into to a text file. Please help me. Thanks in advance...

Source IP is 192.168.13.25 & port no is 2780 (Source is a Hardware which will send UDP packets continuously)
Dest IP is 192.168.13.250 & port no is 45141(Destination is my PC)
In my code I'm binding to 192.168.13.250(PC) and port 2780(Hardware). Then I'm calling recvfrom(). Is there any mismatch in Ip & port??
so which IP & port no do I need to get from user for bind() and recvfrom()?

No  Time        Source          Destination     Proto  Info                                                                       
1   0.000000    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
2   0.000416    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
3   0.000846    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
4   0.001281    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
5   0.001716    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
6   0.002152    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
7   0.002589    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141
8   0.003025    192.168.13.25   192.168.13.250  UDP    Source port: lbc-control  Destination port: 45141

Following is my code:

int main(void) {
    SOCKET recvSockID;
    WSADATA wsaData = {0};
    FILE *udp;
    FILE *fp ;

    struct sockaddr_in sock_addr;
    struct sockaddr_in cliAddr;

    static int recvData;
    int iResult = 0;
    int sock_len = sizeof(sock_addr);
    int sockCli_len = sizeof(cliAddr);
    int recvResult;
    static int iteration;

    fp = fopen("outOfSeq.txt","a");

    if((udp = fopen("udpData.txt","w")) == 0)
        printf("udpData.txt not opened\n");

    printf("\n Enter Destination IP Address : ");
    scanf_s("%s",inputData.destIPAddr,16);

    printf("\n Enter Destination port from which to receive data : "); 
    scanf_s("%d",&inputData.portNo,5);

    printf("\n Enter No.of iterations : "); 
    scanf_s("%d",&inputData.noIteration,2);

    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if(iResult < 0) {
        printf("windows socket startup error\n");
    } 

    recvSockID = socket(AF_INET, SOCK_DGRAM, 0);
    if(recvSockID < 0) {
        printf("Socket creation error\n");
        WSACleanup();
    }

    sock_addr.sin_family = AF_INET;
    sock_addr.sin_port = htons(inputData.portNo);
    sock_addr.sin_addr.s_addr = inet_addr(inputData.destIPAddr);
    //sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if(bind(recvSockID, (struct sockaddr *)&sock_addr,
        sizeof(struct sockaddr)) < 0)
    {
        printf("bind() failed: %ld.\n", WSAGetLastError());
        closesocket(recvSockID);
        return 0;
    }

    memset(udpBuf, 0, sizeof(udpBuf));

    iteration = inputData.noIteration;
    recvData = 1;

    while (recvData) {
        printf("receiving data\n");
        recvResult =  recvfrom(recvSockID, udpBuf, sizeof(udpBuf),
            0, (struct sockaddr *)&cliAddr, &sockCli_len); 

        if (recvResult <= 0) {
            printf("recvResult = %d\n", recvResult);

            printf("Error Code: %d",WSAGetLastError());

            printf("Socket receive()- error\n");
            return 0;
            //break;
            //goto exit;
        } else
            printf("Socket receive()- success\n");

        printf("completed rx data\n");

        fwrite(udpBuf, sizeof(udpBuf), 1, udp);
        memset(udpBuf, 0, sizeof(udpBuf));

        if (iteration != 0) {
            iteration--;
            if (iteration <= 0)
                recvData = 0;
        }
    }

//exit:
    if(udp) {
        fclose(udp);
        udp = 0; 
    }

    //shutdown socket
    closesocket(recvSockID);    
    fclose(udp);

    return 0;
}

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

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

发布评论

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

评论(1

樱花落人离去 2024-10-04 14:45:09
  1. fwrite(udpBuf, sizeof(udpBuf), 1, udp)

    您应该传递recvResult而不是sizeof(udpBuf)

  2. 由于文件缓冲,您可能无法立即看到文件上的字节。但是,在关闭之后,您应该会看到数据。

    如果要禁用缓冲,请使用 setvbuf(udp, NULL, _IONBF, 0),或者作为替代方案,在每次写入操作后调用 fflush(udp)

更新:

如果您根本没有收到数据报,则可能是您绑定了错误的地址或端口。

  1. fwrite(udpBuf, sizeof(udpBuf), 1, udp)

    You should pass recvResult instead of sizeof(udpBuf).

  2. You may not be immediately seeing the bytes on your file due to file buffering. However, after the close you should see the data.

    If you want to disable buffering, use setvbuf(udp, NULL, _IONBF, 0), or as alternative, call fflush(udp) after each write operation.

UPDATE:

If you're not receiving datagrams at all, it's likely you're binding the wrong address or port.

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