1
votes

I'm sorry if this is not an useful question. I'm new to socket programming. And I have to build a multicast application(with receiver and sender) that simply send a message to a group of hosts in one network using multicast. I built it by C on windows using winsock library.

The problem is when I copied receiver.exe to another computer(in the same network) and run sender.exe, the receiver cannot receive the message. I think my network modem need to be configure something to recorgnize the multicast address. I don't understand how the network manage the multicast group so much, the multicast address "239.255.10.10" is my random value.

This is the sender(For shorter source code, I ignored all errors checking because no error occured while creating socket or setsockopt, ...):

#define DEFAULT_ADDR "239.255.10.10" // a random value
#define DEFAULT_PORT 4321

int main(int argc, char *argv[])
{
    WSADATA WsaDat;
    struct sockaddr_in mc_addr;
    int sock;
    char *message = "My message";
    int message_len;
    const char mc_ttl = 1;

    WSAStartup(MAKEWORD(2,2), &WsaDat)!=0;

    // Create a socket for sending to the multicast address
    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    // Set the TTL (time to live/ hop count) for the sender
    setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &mc_ttl, sizeof(mc_ttl));

    memset(&mc_addr, 0, sizeof(mc_addr));
    mc_addr.sin_family = AF_INET;
    mc_addr.sin_addr.s_addr = inet_addr(DEFAULT_ADDR);
    mc_addr.sin_port = htons(DEFAULT_PORT);

    message_len = strlen(message);
    sendto(sock, message, message_len, 0, (struct sockaddr*)&mc_addr, sizeof(mc_addr));
    printf("Message sent\n");

    system("PAUSE");
    closesocket(sock);

    WSACleanup();
}

And this is the receiver:

#define DEFAULT_ADDR "239.255.10.10"
#define DEFAULT_PORT "4321"
#define MAX_LEN 1024

int main(int argc, char *argv[]) {
    WSADATA WsaDat;
    int sock;
    int flag_on = 1;
    struct sockaddr_in mc_addr;
    struct ip_mreq mc_req;
    char message[MAX_LEN+1];
    struct sockaddr_in from_addr;
    int from_len;

    WSAStartup(MAKEWORD(2,2), &WsaDat);

    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&flag_on, sizeof(flag_on));

    memset(&mc_addr, 0, sizeof(mc_addr));
    mc_addr.sin_family = AF_INET;
    mc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    mc_addr.sin_port = htons(atoi(DEFAULT_PORT));

    bind(sock, (struct sockaddr *) &mc_addr, sizeof(mc_addr));

    /* Construct an IGMP join request structure */
    mc_req.imr_multiaddr.s_addr = inet_addr(DEFAULT_ADDR);
    mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

    /* Send an ADD MEMBERSHIP message via setsockopt */
    setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mc_req, sizeof(mc_req));

    printf("Multicast socket ready!/nWaiting for message ...\n");

    while(1) {
        // clear buffer
        memset(message, 0, sizeof(message));
        from_len  =  sizeof(from_addr) ; 
        memset(&from_addr, 0,  from_len); 

        recvfrom(sock, message, MAX_LEN, 0, 
            (struct sockaddr*)&from_addr, &from_len);

        printf("Received %s from %s: ", message, inet_ntoa(from_addr.sin_addr));
    }
}
1
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". - John Saunders
Thank you! I'll put it in my mind :D - hnimnart
Your question doesn't make sense. If the other computer is in the same network, the network modem doesn't have anything to do with the problem. The network modem joins you to another network. - user207421

1 Answers

1
votes

I would write

message_len = strlen(message) + 1;

before sending, to send also the terminating null character.

I would also try to test both sender and receiver on the same PC to understand if this is a network problem or not.