I am making client and a server program. The server pulses out a packet of information and the client is supposed to receive it. This works fine so long as the programs are running on the same machine. I need it to work on any machine in a given network. There is certainly nothing wrong with the network. This means that the problem lies entirely in my code. This is odd however as I had everything working perfectly a couple of days ago and haven't changed anything in terms of the multicasting implementation. Here are the relevant pieces of code that I have for each program.
Client : Reciever
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if((result = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))) < 0){
perror("setsockopt() failure");
exit(1);
}
if((result = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))) < 0){
perror("setsockopt() failure");
exit(1);
}
memset(&multicastAddr, 0, sizeof(multicastAddr)); /* Zero out structure */
multicastAddr.sin_family = AF_INET; /* Internet address family */
multicastAddr.sin_addr.s_addr = htonl(INADDR_ANY);/* Multicast IP address */
multicastAddr.sin_port = htons(current->port); /* Multicast port */
memset(&mreq, 0, sizeof(mreq)); /* Zero out structure */
mreq.imr_multiaddr.s_addr = inet_addr(current->address); /* multicast group */
mreq.imr_interface.s_addr = htonl(INADDR_ANY); /* local inferface */
/* join the multicast group */
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(void *)&mreq, sizeof(mreq)) < 0){
perror("setsockopt() failure");
exit(1);
}
if (bind(sock, (struct sockaddr *)&multicastAddr, sizeof(multicastAddr)) < 0)
{
perror("bind() failure");
exit(1);
}
nread = recvfrom(sock, buffer, sizeof(buffer), 0, NULL, 0);
if (nread <= 0) {
close(sock);
printf("client on descriptor #%i disconnected\n", sock);
}
Server : Multicaster
/* Construct local address structure */
memset(&multicastAddr, 0, sizeof(multicastAddr)); /* Zero out structure */
multicastAddr.sin_family = AF_INET; /* Internet address family */
multicastAddr.sin_addr.s_addr = inet_addr(address);/* Multicast IP address */
multicastAddr.sin_port = htons(retrieveport(&packet[4])); /* Multicast port */
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
perror("socket() failure");
/* Set TTL of multicast packet */
if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (void *) &multicastTTL,
sizeof(multicastTTL)) < 0)
perror("setsockopt() failure");
for (;;) /* Run forever */
{
/* Multicast sendString in datagram to clients every few seconds */
if (sendto(sock, packet, packetLen, 0, (struct sockaddr *)
&multicastAddr, sizeof(multicastAddr)) != packetLen)
perror("sendto() sent a different number of bytes than expected");
usleep(pulse * 1000);
}
Any ideas?