* [Q] Help with an IP Multicast example.
@ 1994-08-22 10:09 Philip Shearer @ZUO-DC EBS-Team
1994-08-23 13:44 ` Philip Shearer @ZUO-DC EBS-Team
0 siblings, 1 reply; 3+ messages in thread
From: Philip Shearer @ZUO-DC EBS-Team @ 1994-08-22 10:09 UTC (permalink / raw)
/*
uname -a == OSF1 macihine_name V2.1 250 alpha
The following example of an IP Multicast program was posted to
comp.unix.programmer.
When I try to run it ( having made changes to the address) on an OSF1
machine . I get an error message on the second setsockopt() function call.
"[2]setsockopt: Invalid argument"
Any idears why?
Also with the first setsockopt(SO_REUSEADDR), (inserting a test on the
return), although no error is returned, it does not seem does not seem
to alow more than one process per port per machine.
Regards Philip
------------------------------------------------------------------------
: From: net@cs.tu-berlin.de (Oliver Laumann)
: Newsgroups: comp.unix.programmer
: Subject: Re: ANY_MULTICASTING_EXAMPLES_OUT_THERE
: Date: 7 Jun 1994 14:13:47 GMT
: Organization: Technical University of Berlin, Germany
:
: > I am looking for some examples of UDP/IP multicasting code.
: >
: > I need working examples, that use the following line of code.
: >
: > setsockopt(sockfd, ..., IP_ADD_MEMBERSHIP, ...
:
: Here is a trivial program that demonstrates IP multicast. I'm posting
: it rather than sending it by e-mail, because it's small and other
: readers might be interested as well. I have tested it under SunOS 5.3.
:
: The program first sends a greeting message to the multicast group and
: port number defined at the top of the program (224.0.0.0/6060); then it
: listens for the greeting messages produced by other instances of the
: program and prints everything it receives to standard output.
:
: Compile the program (don't forget -lsocket -lnsl under SysVR4/SunOS 5),
: start it on a few machines (in the same subnet if you aren't running
: mrouted), and watch the output.
:
: Note the call to bind() which is required in addition to joining
: the multicast group, and the call to setsockopt() with SO_REUSEADDR
: to enable multiple instances of the program to bind to the same
: port number.
:
-------------------------------------------------------------------------
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 6060
#define GROUP "224.0.0.0"
main() {
int s, len, cc, one = 1;
static struct sockaddr_in sin;
static struct ip_mreq imr;
char buf[100];
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket"); return 1;
}
(void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
perror("bind"); return 1;
}
/* Send greeting message to multicast group:
*/
sin.sin_addr.s_addr = htonl(inet_addr(GROUP));
if (sendto(s, "Hi!", 4, 0, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
perror("socket"); return 1;
}
/* Join the group:
*/
imr.imr_multiaddr = sin.sin_addr;
imr.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&imr,
sizeof(imr)) == -1) {
/* Using setsockopt again with the same options on the
* same socket would fail, although it is correct. Therefore:
*/
if (errno != EADDRINUSE) {
perror("[2]setsockopt");
return 1;
}
}
/* Listen for greeting messages sent to the multicast group:
*/
while (1) {
len = sizeof(sin);
if ((cc = recvfrom(s, buf, 100, 0, (struct sockaddr *)&sin, &len))
== -1) {
perror("recvfrom"); return 1;
}
printf("%s:\t%.*s\n", inet_ntoa(sin.sin_addr), cc, buf);
}
/*NOTREACHED*/
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Q] Help with an IP Multicast example.
1994-08-22 10:09 [Q] Help with an IP Multicast example Philip Shearer @ZUO-DC EBS-Team
@ 1994-08-23 13:44 ` Philip Shearer @ZUO-DC EBS-Team
1994-08-24 14:31 ` Philip Shearer @ZUO-DC EBS-Team
0 siblings, 1 reply; 3+ messages in thread
From: Philip Shearer @ZUO-DC EBS-Team @ 1994-08-23 13:44 UTC (permalink / raw)
pbs@zuunix.zuo.dec.com (Philip Shearer @ZUO-DC EBS-Team) writes:
: uname -a == OSF1 macihine_name V2.1 250 alpha
:
: The following example of an IP Multicast program was posted to
: comp.unix.programmer.
: When I try to run it ( having made changes to the address) on an OSF1
There is no need to touch the address.
: machine . I get an error message on the second setsockopt() function call.
: "[2]setsockopt: Invalid argument"
:
: Any idears why?
I found the bug causing the above in the line:
sin.sin_addr.s_addr = htonl(inet_addr(GROUP));
should be:
sin.sin_addr.s_addr = inet_addr(GROUP) ;
But a second error:
setsockopt(SO_REUSEADDR)
SO_REUSEADDR seems to be broken is this a feature in OSF1?
Regards Philip
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Q] Help with an IP Multicast example.
1994-08-23 13:44 ` Philip Shearer @ZUO-DC EBS-Team
@ 1994-08-24 14:31 ` Philip Shearer @ZUO-DC EBS-Team
0 siblings, 0 replies; 3+ messages in thread
From: Philip Shearer @ZUO-DC EBS-Team @ 1994-08-24 14:31 UTC (permalink / raw)
pbs@zuunix.zuo.dec.com (Philip Shearer @ZUO-DC EBS-Team) writes:
:
: But a second error:
: setsockopt(SO_REUSEADDR)
:
: SO_REUSEADDR seems to be broken is this a feature in OSF1?
It's a feature.
The answer lies in the <sys/socket.h> file
#define SO_REUSEADDR .. /* allow local address reuse */
#define SO_REUSEPORT ... /* allow local addr and port reuse */
So replace the instance of SO_REUSEADDR with SO_REUSEPORT and the
program works as expected.
Regards Philip
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1994-08-24 14:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-08-22 10:09 [Q] Help with an IP Multicast example Philip Shearer @ZUO-DC EBS-Team
1994-08-23 13:44 ` Philip Shearer @ZUO-DC EBS-Team
1994-08-24 14:31 ` Philip Shearer @ZUO-DC EBS-Team
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox