u_mctest.c
资源名称:multicast.zip [点击查看]
上传用户:kangyuanty
上传日期:2007-01-03
资源大小:17k
文件大小:4k
源码类别:
Internet/IE编程
开发平台:
Visual C++
- /*
- * u_mctest.c (c) Bob Quinn 2/4/97
- *
- * Released to the public domain
- *
- * Description:
- * Tries out BSD-compatible Multicast APIs, and sends multicast
- * messages in response to multicast messages received.
- */
- #ifndef _WIN32
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #else
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #endif
- #define BUFSIZE 1024
- #define TTL_VALUE 2
- #define TEST_ADDR "234.5.6.7"
- #define TEST_PORT 3456
- #define LOOPMAX 20
- int main()
- {
- struct sockaddr_in stLocal, stTo, stFrom;
- char achIn[BUFSIZE];
- char achOut[] = "Message number: ";
- int s, i;
- struct ip_mreq stMreq;
- int iTmp, iRet;
- #ifdef WIN32
- /* Init WinSock */
- WSADATA stWSAData;
- iTmp = WSAStartup(0x0202, &stWSAData);
- if (iTmp) {
- printf ("WSAStartup failed: Err: %drn", iTmp);
- exit (1);
- }
- #endif
- /* get a datagram socket */
- s = socket(AF_INET,
- SOCK_DGRAM,
- 0);
- if (s == INVALID_SOCKET) {
- printf ("socket() failed, Err: %dn", WSAGetLastError());
- exit(1);
- }
- /* avoid EADDRINUSE error on bind() */
- iTmp = TRUE;
- iRet = setsockopt(s,
- SOL_SOCKET,
- SO_REUSEADDR,
- (char *)&iTmp,
- sizeof(iTmp));
- if (iRet == SOCKET_ERROR) {
- printf ("setsockopt() SO_REUSEADDR failed, Err: %dn",
- WSAGetLastError());
- }
- /* name the socket */
- stLocal.sin_family = AF_INET;
- stLocal.sin_addr.s_addr = htonl(INADDR_ANY);
- stLocal.sin_port = htons(TEST_PORT);
- iRet = bind(s, (struct sockaddr*) &stLocal, sizeof(stLocal));
- if (iRet == SOCKET_ERROR) {
- printf ("bind() failed, Err: %dn",
- WSAGetLastError());
- }
- /* join the multicast group. */
- stMreq.imr_multiaddr.s_addr = inet_addr(TEST_ADDR);
- stMreq.imr_interface.s_addr = INADDR_ANY;
- iRet = setsockopt(s,
- IPPROTO_IP,
- IP_ADD_MEMBERSHIP,
- (char *)&stMreq,
- sizeof(stMreq));
- if (iRet == SOCKET_ERROR) {
- printf ("setsockopt() IP_ADD_MEMBERSHIP failed, Err: %dn",
- WSAGetLastError());
- }
- /* set TTL to traverse up to multiple routers */
- iTmp = TTL_VALUE;
- iRet = setsockopt(s,
- IPPROTO_IP,
- IP_MULTICAST_TTL,
- (char *)&iTmp,
- sizeof(iTmp));
- if (iRet == SOCKET_ERROR) {
- printf ("setsockopt() IP_MULTICAST_TTL failed, Err: %dn",
- WSAGetLastError());
- }
- /* disable loopback */
- iTmp = FALSE;
- iRet = setsockopt(s,
- IPPROTO_IP,
- IP_MULTICAST_LOOP,
- (char *)&iTmp,
- sizeof(iTmp));
- if (iRet == SOCKET_ERROR) {
- printf ("setsockopt() IP_MULTICAST_LOOP failed, Err: %dn",
- WSAGetLastError());
- }
- /* assign our destination address */
- stTo.sin_family = AF_INET;
- stTo.sin_addr.s_addr = inet_addr(TEST_ADDR);
- stTo.sin_port = htons(TEST_PORT);
- printf ("Now sending to (and receiving from) multicast group: %sn",
- TEST_ADDR);
- for (i=0;i<LOOPMAX;i++) {
- int addr_size = sizeof(struct sockaddr_in);
- static iCounter = 1;
- /* send to the multicast address */
- itoa(iCounter++, &achOut[16], 10);
- iRet = sendto(s,
- achOut,
- sizeof(achOut),
- 0,
- (struct sockaddr*)&stTo,
- addr_size);
- if (iRet < 0) {
- // perror("sendto() failedn");
- printf ("sendto() failed, Error: %dn", WSAGetLastError());
- exit(1);
- }
- iRet = recvfrom(s,
- achIn,
- BUFSIZE,
- 0,
- (struct sockaddr*)&stFrom,
- &addr_size);
- if (iRet < 0) {
- // perror("recvfrom() failedn");
- printf ("sendto() failed, Error: %dn", WSAGetLastError());
- exit(1);
- }
- printf("From host:%s port:%d, %sn",
- inet_ntoa(stFrom.sin_addr),
- ntohs(stFrom.sin_port), achIn);
- } /* end for(;;) */
- #ifdef WIN32
- /* sign-off WinSock */
- WSACleanup();
- #endif
- } /* end main() */