126 lines
5.0 KiB
HTML
126 lines
5.0 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html lang="en-us" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow" />
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="DC.Type" content="reference" />
|
|
<meta name="DC.Title" content="Example: Send multicast datagrams" />
|
|
<meta name="abstract" content="This example enables a socket to send multicast datagrams." />
|
|
<meta name="description" content="This example enables a socket to send multicast datagrams." />
|
|
<meta name="DC.Relation" scheme="URI" content="xmulticast.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="xmulticast.htm" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2001, 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2001, 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="x1multicast" />
|
|
<meta name="DC.Language" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
|
<title>Example: Send multicast datagrams </title>
|
|
</head>
|
|
<body id="x1multicast"><a name="x1multicast"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Send multicast datagrams </h1>
|
|
<div><p>This example enables a socket to send multicast datagrams.</p>
|
|
<div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
|
<pre>#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
|
|
struct in_addr localInterface;
|
|
struct sockaddr_in groupSock;
|
|
int sd;
|
|
int datalen;
|
|
char databuf[1024];
|
|
|
|
int main (int argc, char *argv[])
|
|
{
|
|
|
|
/* ------------------------------------------------------------*/
|
|
/* */
|
|
/* Send Multicast Datagram code example. */
|
|
/* */
|
|
/* ------------------------------------------------------------*/
|
|
|
|
/*
|
|
* Create a datagram socket on which to send.
|
|
*/
|
|
sd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
if (sd < 0) {
|
|
perror("opening datagram socket");
|
|
exit(1);
|
|
}
|
|
|
|
/*
|
|
* Initialize the group sockaddr structure with a
|
|
* group address of 225.1.1.1 and port 5555.
|
|
*/
|
|
memset((char *) &groupSock, 0, sizeof(groupSock));
|
|
groupSock.sin_family = AF_INET;
|
|
groupSock.sin_addr.s_addr = inet_addr("225.1.1.1");
|
|
groupSock.sin_port = htons(5555);
|
|
|
|
/*
|
|
* Disable loopback so you do not receive your own datagrams.
|
|
*/
|
|
{
|
|
char loopch=0;
|
|
|
|
if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP,
|
|
(char *)&loopch, sizeof(loopch)) < 0) {
|
|
perror("setting IP_MULTICAST_LOOP:");
|
|
close(sd);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Set local interface for outbound multicast datagrams.
|
|
* The IP address specified must be associated with a local,
|
|
* multicast-capable interface.
|
|
*/
|
|
localInterface.s_addr = inet_addr("9.5.1.1");
|
|
if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF,
|
|
(char *)&localInterface,
|
|
sizeof(localInterface)) < 0) {
|
|
perror("setting local interface");
|
|
exit(1);
|
|
}
|
|
|
|
|
|
/*
|
|
* Send a message to the multicast group specified by the
|
|
* groupSock sockaddr structure.
|
|
*/
|
|
datalen = 10;
|
|
if (sendto(sd, databuf, datalen, 0,
|
|
(struct sockaddr*)&groupSock,
|
|
sizeof(groupSock)) < 0)
|
|
{
|
|
perror("sending datagram message");
|
|
}
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="xmulticast.htm" title="IP multicasting provides the capability for an application to send a single IP datagram that a group of hosts in a network can receive.">Examples: Use multicasting with AF_INET</a></div>
|
|
</div>
|
|
<div class="relref"><strong>Related reference</strong><br />
|
|
<div><a href="xmulticast.htm" title="IP multicasting provides the capability for an application to send a single IP datagram that a group of hosts in a network can receive.">Examples: Use multicasting with AF_INET</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |