简体   繁体   中英

Recvfrom and Wireshark

I communicate with a server, i send a message with a socket (sock_raw..), it's works fine, the client (me), send the message, the server receive the right message, but when the server need to send it to me (the client), the client don't retrieve the right information with the recv from.

I retrieve this on two calls server->client :

buffer = [E] | len_buffer = [2]
buffer = [E] | len_buffer = [2]

But my buffer need to be equal to this data and have this size :

First call (Data in hexadecimal with 10 bytes) 在此处输入图片说明

And Second call (Data in hexadecimal with 2 bytes) 在此处输入图片说明

This is how i use the recvfrom function (t->sock is the socket fd of the server, t->msg_recv is the buffer created to receive the msg from the servern and t->_recv is the sockaddr* struct for the receive):

if (recvfrom(t->sock, t->msg_recv, sizeof(t->msg_recv), 0, \
    (struct sockaddr *)&t->_recv, \
    (socklen_t[1]){sizeof(struct sockaddr_in)}) < 0) {
        perror("recvto()");
        exit(84);
    }

Barmar here is the output of the Wireshark message send :

在此处输入图片说明

Here is the code of send and recvfrom on the same function :

int len = 0;
    if (sendto(t->sock, t->buffer, t->ip->tot_len, 0,
               (struct sockaddr *)&t->_sin, sizeof(t->_sin)) < 0) {
      perror("sendto()");
      exit(84);
    }
    if ((len = recvfrom(t->sock, t->msg_recv, sizeof(t->msg_recv), 0, \
    (struct sockaddr *)&t->_recv, \
    (socklen_t[1]){sizeof(struct sockaddr_in)})) < 0) {
        perror("recvto()");
        exit(84);
    }

The buffer is not a string, it's binary data, so you shouldn't print it with %s .

You need to get the length of the buffer when you call recvfrom . Then use a loop that prints each byte with %02X format.

len = recvfrom(t->sock, t->msg_recv, sizeof(t->msg_recv), 0, \
    (struct sockaddr *)&t->_recv, \
    (socklen_t[1]){sizeof(struct sockaddr_in)});
if (len < 0) {
        perror("recvto()");
        exit(84);
}
for (int i = 0; i < len; i ++) {
    printf("%02X ", t->msg_recv[i]);
    if (i % 16 == 15) {
        printf("\n");
    } else if (i % 16 == 7) {
        printf(" ");
    }
}
printf("\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM