0

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(" ");
  }
}
  • What do you expect printf to print? – user253751 Apr 01 '22 at 11:21
  • I just want to see the received binary data in the console – Sebastian90 Apr 01 '22 at 15:07
  • in what format? 1s and 0s? – user253751 Apr 01 '22 at 15:17
  • I did some further research. In VHDL I am incrementing the signal `signal ETH_DIN : std_logic_vector (8 * 16 - 1 downto 0) := (others => '0');` by this command `ETH_DIN <= std_logic_vector(unsigned(ETH_DIN) + 1);`. By using Wireshark, I can see this data, but it is shown in hex, e.g.: `000000000000000000000000000e725d`. Is this data just displayed in hex or is it sent in hex? – Sebastian90 Apr 04 '22 at 07:35
  • Data is always binary. Hex is a way of displaying binary data with less digits. Decimal is a way of displaying it that's convenient for human-readable numbers. ASCII is a way of displaying it that's designed for English text. And so on. What did you expect printf to print? – user253751 Apr 04 '22 at 08:45
  • I expected to see the signal 'ETH_DIN' incremented in binary, e.g. 0, 1, 10, 11, 100 and so on. – Sebastian90 Apr 04 '22 at 10:32
  • I bet that it is! But often in tools like Wireshark, packets are displayed in hexadecimal, because it uses 1/4 as much space on the screen. Asking "is the data sent in hex?" is a question that makes no sense - it is like asking whether the number three is binary or decimal, or whether a basketball is in the English or French language. – user253751 Apr 04 '22 at 10:36
  • Sometimes data really is sent in ASCII codes for hex - sometimes things will send the ASCII codes for letters "f" "f" "0" "1" "2" "5" or whatever. You would know if you'd written the code to make your FPGA do that. Presumably you didn't. – user253751 Apr 04 '22 at 10:38
  • I can give you a piece of paper saying "basketball" and then we can say I transmitted in English, or a piece of paper saying "jouer au basket" and then we can say I transmitted in French, or a picture of a basketball, or an actual basketball, and then it makes no sense to ask whether it's a French or English basketball – user253751 Apr 04 '22 at 10:39

0 Answers0