📄 c-netapi2.html
字号:
</td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89367"> </a></div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89369"> </a>DVMRP routers. </div></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="89371"> </a>224.0.0.5</div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89373"> </a></div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89375"> </a>OSPF routers. </div></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="89377"> </a>224.0.0.6</div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89379"> </a></div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89381"> </a>OSPF designated routers. </div></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="89383"> </a>224.0.0.9</div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89385"> </a></div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89387"> </a>All RIP routers. </div></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="89389"> </a>224.0.0.255</div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89391"> </a><b class="symbol_UC">INADDR_MAX_LOCAL_GROUP</b></div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89393"> </a>Unassigned. </div></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="89395"> </a>224.0.1.1</div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89397"> </a></div></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="89399"> </a>NTP (Network Time Protocol). </div></td><td width="10"> </td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p><dd><p class="Body"><a name="89401"> </a>The following code samples define two routines, <b class="routine"><i class="routine">mcastSend</i></b><b>( )</b> and <b class="routine"><i class="routine">mcastRcv</i></b><b>( )</b>. These routines demonstrate how to use UDP sockets to handle sending and receiving multicast traffic. </p><dd><p class="Body"><a name="89402"> </a><b class="routine"><i class="routine">mcastSend</i></b><b>( )</b> transmits a buffer to the specified multicast address. As input, this routine expects a multicast destination, a port number, a buffer pointer, and a buffer length. For example:</p><dl class="margin"><dd><pre class="Code2"><b><a name="89403">status = mcastSend ("224.1.0.1", 7777, bufPtr, 100);</a></b></pre></dl><dd><p class="Body"><a name="89404"> </a><b class="routine"><i class="routine">mcastRcv</i></b><b>( )</b>receives any packet sent to a specified multicast address. As input, this routine expects the interface address from which the packet is coming, a multicast address, a port number, and the number of bytes to read from the packet. The returned value of the function is a pointer a buffer containing the read bytes. For example:</p><dl class="margin"><dd><pre class="Code2"><b><a name="89405">buf = mcastRcv (ifAddress, "224.1.0.1", 7777, 100) ;</a></b></pre></dl></dl></dl><h4 class="EntityTitle"><a name="89406"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 7-3: Datagram Sockets (UDP) and Multicasting </font></a></h4><dl class="margin"><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="89408">/* includes */ #include "vxWorks.h" #include "taskLib.h" #include "socket.h" #include "netinet/in.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "sockLib.h" #include "inetLib.h" #include "ioLib.h" #include "routeLib.h" /* defines */ /* globals */ /* forward declarations */ STATUS mcastSend (char * mcastAddr, USHORT mcastPort, char * sendBuf, int sendLen); char * mcastRcv (char * ifAddr, char * mcastAddr, USHORT mcastPort, int numRead); /************************************************************************ * mcastSend - send a message to the multicast address * This function sends a message to the multicast address * The multicast group address to send, the port number, the pointer to the * send buffer and the send buffer length are given as input parameters. * RETURNS: OK if sucessful or ERROR */ STATUS mcastSend ( char * mcastAddr, /* multicast address */ USHORT mcastPort, /* udp port number */ char * sendBuf, /* send Buffer */ int sendLen /* length of send buffer */ ) { struct sockaddr_in sin; struct sockaddr_in toAddr; int toAddrLen; int sockDesc; char * bufPtr; int len; /* create a send and recv socket */ if ((sockDesc = socket (AF_INET, SOCK_DGRAM, 0)) < 0 ) { printf (" cannot open send socket\n"); return (ERROR); } /* zero out the structures */ bzero ((char *)&sin, sizeof (sin)); bzero ((char *)&toAddr, sizeof (toAddr)); sin.sa_len = (u_char) sizeof(sin); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(0); if (bind(sockDesc, (struct sockaddr *)&sin, sizeof(sin)) != 0) { perror("bind"); if (sockDesc) close (sockDesc); return (ERROR); } toAddrLen = sizeof(struct sockaddr_in); toAddr.sa_len = (u_char) toAddrLen; toAddr.sin_family = AF_INET; /* initialize the address to the send */ toAddr.sin_addr.s_addr = inet_addr (mcastAddr); /* initialize the port to send */ toAddr.sin_port = htons(mcastPort); bufPtr = sendBuf; /* initialize the buffer pointer */ /* send the buffer */ while (sendLen > 0) { if ((len = sendto (sockDesc, bufPtr, sendLen, 0, (struct sockaddr *)&toAddr, toAddrLen)) < 0 ) { printf("mcastSend sendto errno:0x%x\n", errno ); break; } sendLen -= len; bufPtr += len; taskDelay (1); /* give a taskDelay */ } if (sockDesc != ERROR) close (sockDesc); return (OK); } /************************************************************************ * mcastRcv - receive a message from a multicast address * This function receives a message from a multicast address * The interface address from which to receive the multicast packet, * the multicast address to recv from, the port number and the number of * bytes to read are given as input parameters to this routine. * RETURNS: Pointer to the Buffer or NULL if error. */ char * mcastRcv ( char * ifAddr, /* interface address to recv mcast packets */ char * mcastAddr, /* multicast address */ USHORT mcastPort, /* udp port number to recv */ int numRead /* number of bytes to read */ ) { struct sockaddr_in fromAddr; struct sockaddr_in sin; int fromLen; struct ip_mreq ipMreq; int recvLen; int sockDesc; char * bufPtr; int status = OK; char * recvBuf = NULL; if ((sockDesc = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { printf (" cannot open recv socket\n"); return (NULL); } bzero ((char *)&sin, sizeof (sin)); bzero ((char *) &fromAddr, sizeof(fromAddr)); fromLen = sizeof(fromAddr); if ((recvBuf = calloc (numRead, sizeof (char))) == NULL) { printf (" calloc error, cannot allocate memory\n"); status = ERROR; goto cleanUp; } sin.sa_len = (u_char) sizeof(sin); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; /* UDP port number to match for the received packets */ sin.sin_port = htons (mcastPort); /* bind a port number to the socket */ if (bind(sockDesc, (struct sockaddr *)&sin, sizeof(sin)) != 0) { perror("bind"); status = ERROR; goto cleanUp; } /* fill in the argument structure to join the multicast group */ /* initialize the multicast address to join */ ipMreq.imr_multiaddr.s_addr = inet_addr (mcastAddr); /* unicast interface addr from which to receive the multicast packets */ ipMreq.imr_interface.s_addr = inet_addr (ifAddr); /* set the socket option to join the MULTICAST group */ if (setsockopt (sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&ipMreq, sizeof (ipMreq)) < 0) { printf ("setsockopt IP_ADD_MEMBERSHIP error:\n"); status = ERROR; goto cleanUp; } /* get the data destined to the above multicast group */ bufPtr = recvBuf; while (numRead > 0) { if ((recvLen = recvfrom (sockDesc, bufPtr, numRead, 0, (struct sockaddr *)&fromAddr, &fromLen)) < 0) { perror("recvfrom"); status = ERROR; break; } numRead -= recvLen; /* decrement number of bytes to read */ bufPtr += recvLen; /* increment the buffer pointer */ } /* set the socket option to leave the MULTICAST group */ if (setsockopt (sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&ipMreq, sizeof (ipMreq)) < 0) printf ("setsockopt IP_DROP_MEMBERSHIP error:\n"); cleanUp: { if (sockDesc != ERROR) close (sockDesc); if ((status != OK) && (recvBuf != NULL)) { free (recvBuf); recvBuf = NULL; } } return (recvBuf); } </a></b></pre></dl></dl><a name="foot"><hr></a><p class="navbar" align="right"><a href="index.html"><img border="0" alt="[Contents]" src="icons/contents.gif"></a><a href="c-netapi.html"><img border="0" alt="[Index]" src="icons/index.gif"></a><a href="c-netapi.html"><img border="0" alt="[Top]" src="icons/top.gif"></a><a href="c-netapi1.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="c-netapi3.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p></body></html><!---by WRS Documentation (), Wind River Systems, Inc. conversion tool: Quadralay WebWorks Publisher 4.0.11 template: CSS Template, Jan 1998 - Jefro --->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -