从UDP套接字C+&#x2B读取二进制数据

发布于 2025-01-18 08:53:17 字数 2460 浏览 0 评论 0原文

我正在尝试从UDP插座读取二进制数据。数据(二进制,每个步骤递增)具有16位,并通过FPGA以太网接口发送到我的主机计算机(Ubuntu 20)。我的UDP服务器正在接收数据,但未显示。我想“ printf”函数有问题,因为当我通过终端发送(本地)文本时,它起作用。如何显示此数据? UDP服务器的代码如下:

#include <QCoreApplication>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>

#define BUFLEN 512  //Max length of buffer
#define PORT 1024   //The port on which to listen for incoming data

void die(char *s)
{
    perror(s);
    exit(1);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    struct sockaddr_in si_me, si_other;
    socklen_t slen = sizeof(si_other);

    int s, i , recv_len;
    char buf[BUFLEN];

    //create a UDP socket
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    // zero out the structure
    memset((char *) &si_me, 0, sizeof(si_me));

    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);

    //bind socket to port
    if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
    {
        die("bind");
    }

    printf("Using port %d, ctrl + C abort\n", PORT);

    //keep listening for data
    while(1)
    {
        printf("Waiting for data...");
        fflush(stdout);

        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(s, buf, BUFLEN, MSG_WAITALL, (struct sockaddr *) &si_other, &slen)) == -1)
        {
            die("recvfrom()");
        }

        //print details of the client/peer and the data received
        printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
        printf("Data: %s\n" , buf);
    }

    close(s);
    return 0;

    return a.exec();
}

解决方案: printf()函数确实存在问题。为了打印收到的十六进制数据,我使用了此提示(问题C插座)打印数据:

#include <ctype.h>
#include <stdio.h>
void hexdumpunformatted(void *ptr, int buflen) {
  unsigned char *buf = (unsigned char*)ptr;
  int i, j;
  for (i=0; i<buflen; i++) {
    printf("%02x ", buf[i]);
    printf(" ");
  }
}

I am trying to read binary data from a UDP socket. The data (binary, incremented every step) has 16 bits and is sent via an FPGA Ethernet interface to my host computer (Ubuntu 20). My UDP server is receiving data but does not display it. I suppose the "printf" function has a problem, because when I am sending (local) text via terminal it works. How can I display this data? The code for the UDP server is as follows:

#include <QCoreApplication>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>

#define BUFLEN 512  //Max length of buffer
#define PORT 1024   //The port on which to listen for incoming data

void die(char *s)
{
    perror(s);
    exit(1);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    struct sockaddr_in si_me, si_other;
    socklen_t slen = sizeof(si_other);

    int s, i , recv_len;
    char buf[BUFLEN];

    //create a UDP socket
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    // zero out the structure
    memset((char *) &si_me, 0, sizeof(si_me));

    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);

    //bind socket to port
    if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
    {
        die("bind");
    }

    printf("Using port %d, ctrl + C abort\n", PORT);

    //keep listening for data
    while(1)
    {
        printf("Waiting for data...");
        fflush(stdout);

        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(s, buf, BUFLEN, MSG_WAITALL, (struct sockaddr *) &si_other, &slen)) == -1)
        {
            die("recvfrom()");
        }

        //print details of the client/peer and the data received
        printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
        printf("Data: %s\n" , buf);
    }

    close(s);
    return 0;

    return a.exec();
}

Solution:
There was indeed a problem with the printf() function. In order to print received hex data, I used this hint (Problem reading hexadecimal buffer from C socket) to print the data:

#include <ctype.h>
#include <stdio.h>
void hexdumpunformatted(void *ptr, int buflen) {
  unsigned char *buf = (unsigned char*)ptr;
  int i, j;
  for (i=0; i<buflen; i++) {
    printf("%02x ", buf[i]);
    printf(" ");
  }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文