You are not logged in.
Pages: 1
Could you post a bit more code, especially the part before where you do other things and initialize imreq? Or can it be found at that url? (No time to check it out now.)
Offline
server.c
___________________________________________________________
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAXBUFSIZE 65536 // Max UDP Packet size is 64 Kbyte
int main()
{
int sock, status, socklen;
char buffer[MAXBUFSIZE];
struct sockaddr_in saddr;
struct ip_mreq imreq;
// set content of struct saddr and imreq to zero
memset(&saddr, 0, sizeof(struct sockaddr_in));
memset(&imreq, 0, sizeof(struct ip_mreq));
// open a UDP socket
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if ( sock < 0 )
perror("Error creating socket"), exit(0);
saddr.sin_family = PF_INET;
saddr.sin_port = htons(4096); // listen on port 4096
saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind socket to any interface
status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
if ( status < 0 )
perror("Error binding socket to interface"), exit(0);
imreq.imr_multiaddr.s_addr = inet_addr("226.0.0.1");
//imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface
===> imreq.imr_interface.s_addr = inet_addr("x.x.x.x"); // ip of the machine on which client is running
// JOIN multicast group on default interface
status = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(const void *)&imreq, sizeof(struct ip_mreq));
socklen = sizeof(struct sockaddr_in);
// receive packet from socket
status = recvfrom(sock, buffer, MAXBUFSIZE, 0,
(struct sockaddr *)&saddr, &socklen);
// shutdown socket
shutdown(sock, 2);
// close socket
close(sock);
return 0;
}
client.c
___________________________________________________________
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAXBUFSIZE 65536 // Max UDP Packet size is 64 Kbyte
int main()
{
int sock, status, socklen;
char buffer[MAXBUFSIZE];
struct sockaddr_in saddr;
struct in_addr iaddr;
unsigned char ttl = 3;
unsigned char one = 1;
// set content of struct saddr and imreq to zero
memset(&saddr, 0, sizeof(struct sockaddr_in));
memset(&iaddr, 0, sizeof(struct in_addr));
// open a UDP socket
sock = socket(PF_INET, SOCK_DGRAM, 0);
if ( sock < 0 )
perror("Error creating socket"), exit(0);
saddr.sin_family = PF_INET;
saddr.sin_port = htons(0); // Use the first free port
saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind socket to any interface
status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
if ( status < 0 )
perror("Error binding socket to interface"), exit(0);
// iaddr.s_addr = INADDR_ANY; // use DEFAULT interface
===> iaddr.s_addr = inet_addr("x.x.x.x"); // ip of the machine on which client is running
// Set the outgoing interface to DEFAULT
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &iaddr,
sizeof(struct in_addr));
// Set multicast packet TTL to 3; default TTL is 1
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
sizeof(unsigned char));
// send multicast traffic to myself too
status = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP,
&one, sizeof(unsigned char));
// set destination multicast address
saddr.sin_family = PF_INET;
saddr.sin_addr.s_addr = inet_addr("226.0.0.1");
saddr.sin_port = htons(4096);
// put some data in buffer
strcpy(buffer, "Hello world\n");
socklen = sizeof(struct sockaddr_in);
// receive packet from socket
status = sendto(sock, buffer, strlen(buffer), 0,
(struct sockaddr *)&saddr, socklen);
// shutdown socket
shutdown(sock, 2);
// close socket
close(sock);
return 0;
}
Offline
in interface if i use the respective local ips of the machines where client and server are running the problem gets solved temporarily i.e. datagrams are being sent from server to client successfully.
Still the one query remains why does interface => INADDR_ANY does not work??
setsockopt with IP_ADD_MEMBERSHIP fails with No such Device error errno = 19 and though setsockopt with IP_MULTICAST_IF does not throw error sento call returns error Network Unreachable errno = 101.
Offline
route add -net 224.0.0.0 netmask 224.0.0.0 eth0
Yeah, I was looking for this for hours! Thanks
Exactly the same problem, with the same example and your solution worked for me too ! Thanks a lot for your help guys !
I got this error, and checked the route table, add a default route to the working interface, the error is gone.
Thanks a lot, Its amazingly working...
Pages: 1