Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recv only gets first 4 bytes of package

Tags:

c

linux

sockets

udp

I'm writing a client application in C++ to receive binary data via UDP broadcast, however I'm unable to receive anything beyond the first 4 bytes, regardless of the buffer size. I've checked the packets in Wireshark, and I can see that my machine is receiving roughly 1200 bytes of data. When I compare this to what my client is receiving, I can see that I'm getting the packets, however the remaining data is lost. Here's my code:

#define BUFFERSIZE 4096
int main()
{
    struct addrinfo hints, *servinfo, *p;
    int sockfd, i, nbyt;

    int *buf = (int*) malloc(BUFFERSIZE);

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE;

    getaddrinfo(NULL, "9011", &hints, &servinfo);

    sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
    bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);

    FILE *file;
    file = fopen("file.txt", "w+");

    for (i=0;i<=45;i++)
    {
        nbyt = recv(sockfd, buf, BUFFERSIZE, 0);            
        fprintf(file, "%s %d; %s %x\n", "Bytes received:", nbyt, "Message:", *buf);
    }

    fclose(file);
    close(sockfd);
    free(buf);

    printf("%s\n", "Execution ended.");

    return 0;
}

An example of some of the data I'm receiving:

Bytes received: 1131; Message: 5b0
Bytes received: 1131; Message: 5b3
Bytes received: 1131; Message: 5b6
Bytes received: 1092; Message: 4e0

I've tried setting the socket to non-blocking with the MSG_DONTWAIT flag, as this fixed the problem for a similar Python application, however this only returns errors:

Bytes received: -1; Message: 0

I'm relatively new to C++ and sockets, so it's possible there's something I'm just not seeing. Hopefully somebody can help me figure out what's wrong? I can provide additional information if necessary.

like image 858
user1896512 Avatar asked Jan 30 '26 19:01

user1896512


2 Answers

You're receiving it just fine. Your debug code just doesn't do what you think it does.

The x specifier to printf prints the unsigned int argument in hex. Integers on your platform are probably 32-bits. So you're only printing the first 4 bytes.

If you want to print the entire buffer in hex, you need to write the loop to do that yourself (or use a library, of course).

For example, and I warn you my C is a little rusty (and this is by far not the best performing way to do this) ... Also, this assumes your buffer is always a multiple of sizeof(int), or it'll lose some data:

nbyt = recv(sockfd, buf, BUFFERSIZE, 0);            
fprintf(file, "%s %d; %s\n", "Bytes received:", nbyt, "Message:");
for (offset = 0; offset < nbyt/sizeof(int); ++offset) {
    fprintf(file, "%x ", buf[offset]);
}
fprintf(file, "\n");
like image 97
derobert Avatar answered Feb 01 '26 08:02

derobert


*buf in your code means the first integer from the array of the received data, so you are printing just four first bytes of the received UDP payload.

like image 44
Nikolai Fetissov Avatar answered Feb 01 '26 10:02

Nikolai Fetissov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!