سؤال

I have setup a UDP receiver, as:

   int rx_socket;
   struct sockaddr_in my_addr;
   struct sockaddr_in rem_addr;
   socklen_t addrlen = sizeof(rem_addr);

   rx_socket = socket(AF_INET, SOCK_DGRAM,  IPPROTO_UDP);

   my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
   my_addr.sin_family = AF_INET;
   my_addr.sin_port = htons(9900);

    rc = bind(rx_socket, (struct sockaddr *) &(my_addr), sizeof(my_addr));
    if (!rc) {
        printf("BIND SUCCESSFULL\n");
    }

    char buf[250];

    while(1) {
        printf("WAITING\n");
        recvfrom(rx_socket, buf, sizeof(buf), 0, (struct sockaddr *)&rem_addr, &addrlen);
        printf("RECEIVED\n");
    }

The recvfrom() never returns. I have done some Wireshark analysis, and it indicates the packets are there:

Summary:

User Datagram Protocol, Src Port: 57506 (57506), Dst Port: iua (9900)

Checksum: 0x14a2 [validation disabled]

Data (8 bytes)


Any help will be appreciated.

EDIT:

An interesting observation is that the source, which is a DSP fails to send packets, i.e., sendto() returns -1, until I ping to it, from destination. Right after the ping, the source can start transmitting packets.

EDIT 2:

Here is the sender's code:

    int fd;
    fd = socket(AF_INET, SOCK_DGRAM,  IPPROTO_UDP);

    struct sockaddr_in my_addr;

    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(9900);
    inet_aton("10.0.201.102", &(my_addr.sin_addr));

    char buf[250];

    for (;;) {
        int bytesSent = sendto(fd, buf, 8, 0,
                (struct sockaddr *) &(my_addr), sizeof(my_addr));
        printf("sent: %d bytes\n", bytesSent);
        sleep(1000);
    }
هل كانت مفيدة؟

المحلول

So the problem turned out to be with virtualbox. My sender is on host, but receiver is on a virtual machine. If I run the receiver on the host as well, UDP packets are being received.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top