Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1366304
  • 博文数量: 118
  • 博客积分: 3888
  • 博客等级: 中校
  • 技术积分: 2940
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-10 18:15
个人简介

一看二做三总结

文章分类

全部博文(118)

分类: LINUX

2014-07-07 23:06:24



点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. #ifdef WIN_DEVCPP
  6. #include <winsock.h>
  7. #include <io.h>
  8. #pragma comment(lib,"ws2_32.lib")
  9. int WSAStartup
  10. (
  11.     WORD wVersionRequested,
  12.     LPWSADATA lpWSAData
  13. );
  14. typedef unsigned short in_port_t;
  15. #else
  16. #include <sys/socket.h>
  17. #include <arpa/inet.h>

  18. #define TRUE (1)
  19. #define FALSE (0)
  20. #define ERROR (-1)
  21. typedef unsigned long BOOL;
  22. #endif /* WIN_DEVCPP */

  23. #define BUFLEN 255
  24. #define TC_BOARD_IP "192.168.2.3"
  25. #define TC_HOST_IP "192.168.2.253"
  26. #define TC_BOARD_PORT 7602
  27. #define TC_HOST_PORT 7601

  28. #define FIRST_SHAKE "the first handshake"
  29. #define SECOND_SHAKE "the second handshake"
  30. #define THIRD_SHAKE "the third handshake"

  31. /*********************************************************************
  32. *filename: eth_test.c
  33. *********************************************************************/
  34. #ifdef WIN_DEVCPP
  35. int inet_aton( const char * pString, struct in_addr * inetAddress )
  36. {
  37.     u_long rtnAddress;

  38.     if (inetAddress == NULL)
  39.         return (ERROR);

  40.     rtnAddress = inet_addr (pString);
  41.     inetAddress->s_addr = rtnAddress;
  42.     return (0);
  43. }
  44. #endif /* WIN_DEVCPP */

  45. int main(int argc, char **argv)
  46. {
  47. #ifdef WIN_DEVCPP
  48.     WSADATA wsaData;
  49. #endif /* WIN_DEVCPP */
  50.     int retVal;
  51.     struct sockaddr_in to;
  52.     struct sockaddr_in from;
  53.     struct sockaddr_in local;
  54.     int sockfd;
  55.     char buff[BUFLEN + 1];
  56.     struct in_addr localIpAddr;
  57.     struct ip_mreq mreq;
  58.     int pkgLen;
  59.     BOOL isMultiCAST;
  60.     struct in_addr targetIp;
  61.     struct in_addr selfIp;

  62.     printf("argc=%d\n",argc);

  63. #ifdef WIN_DEVCPP
  64.     retVal = WSAStartup( MAKEWORD(2, 2), &wsaData );
  65.     if (retVal == SOCKET_ERROR) {
  66.         printf("WSAStartup error\n");
  67.         exit(33);
  68.     }
  69. #endif /* WIN_DEVCPP */

  70.     if ((argc == 2) && (0 == strcmp(argv[1], "help"))) {
  71.         printf("Usage: mcast_server [targetIp [selfIp]]\n");
  72.         printf("default: mcast_server %s %s\n", TC_BOARD_IP, TC_HOST_IP);
  73.         exit (0);
  74.     }

  75.     if (argc >= 2) {
  76.         if (0 > inet_aton(argv[1], &targetIp))
  77.             exit (1);
  78.     } else {
  79.         if (0 > inet_aton(TC_BOARD_IP, &targetIp))
  80.             exit (2);
  81.     }

  82.     if (0xe == (htonl(targetIp.s_addr) >> 28)) {
  83.         isMultiCAST = TRUE;
  84.         printf("## multi-cast ##\n");
  85.     } else {
  86.         isMultiCAST = FALSE;
  87.         printf("## uni-cast ##\n");
  88.     }

  89.     if (argc >= 3) {
  90.         if (0 > inet_aton(argv[2], &selfIp))
  91.             exit (3);
  92.     } else {
  93.         if (0 > inet_aton(TC_HOST_IP, &selfIp))
  94.             exit (4);
  95.     }

  96.     printf("set targetIp to %s, port to %d\n", (char *)inet_ntoa(targetIp), TC_BOARD_PORT);
  97.     printf("set selfIp to %s, port to %d\n", (char *)inet_ntoa(selfIp), TC_HOST_PORT);

  98.     /* create socket */
  99.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  100.     if (sockfd < 0) {
  101.         perror("socket");
  102.         printf("socket creating error\n");
  103.         exit(1);
  104.     }

  105.     /* set self info */
  106.     memset(&local, 0, sizeof(local));
  107.     local.sin_family = AF_INET;
  108.     local.sin_port = htons(TC_HOST_PORT);
  109.     local.sin_addr.s_addr = INADDR_ANY;

  110.     /*bind socket*/
  111.     printf("bind socket to %s:%d\n", (char *)inet_ntoa(selfIp), TC_HOST_PORT);
  112.     retVal = bind(sockfd,(struct sockaddr*)&local, sizeof(local));
  113.     if (retVal < 0) {
  114.         perror("bind");
  115.         printf("bind error!\n");
  116.         close(sockfd);
  117.         exit(5);
  118.     }

  119.     if (isMultiCAST) {
  120.          /* join multi-cast group */
  121.         mreq.imr_multiaddr = targetIp;
  122.         mreq.imr_interface = selfIp;

  123.         retVal = setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mreq, sizeof(mreq));
  124.         if (retVal == ERROR) {
  125.             perror("ADD_MEMBERSHIP");
  126.             printf("setsockopt join error!\n");
  127.             close(sockfd);
  128.             exit(6);
  129.         }

  130.         /* enable multi-cast */
  131.         localIpAddr = selfIp;
  132.         retVal = setsockopt (sockfd, IPPROTO_IP, IP_MULTICAST_IF,
  133.             (char *)&localIpAddr, sizeof(localIpAddr));
  134.         if (retVal == ERROR) {
  135.             perror("MULTICAST_IF");
  136.             printf("setsockopt enable multi-cast error!\n");
  137.             close(sockfd);
  138.             exit(7);
  139.         }
  140.     }

  141.     /* set dst info */
  142.     memset(&to, 0, sizeof(to));
  143.     to.sin_family = AF_INET;
  144.     to.sin_port = htons(TC_BOARD_PORT);
  145.     to.sin_addr = targetIp;

  146.     /* send the first handshake */
  147.     retVal = sendto(sockfd, FIRST_SHAKE, sizeof(FIRST_SHAKE), 0, (struct sockaddr *)&to, sizeof(to));
  148.     if (retVal == ERROR) {
  149.         perror("sendto");
  150.         printf("sendto error!\n");
  151.         close(sockfd);
  152.         exit(8);
  153.     }

  154.     printf(">>>> [%s:%d] %s is sent >>>>\n",
  155.         (char *)inet_ntoa(to.sin_addr), htons(to.sin_port), FIRST_SHAKE);

  156.     /* receive the second handshake */
  157.     do {
  158.          memset(buff, 0, sizeof(buff));
  159.         memset(&from, 0, sizeof(from));
  160.         pkgLen = sizeof(from);
  161.         retVal = recvfrom(sockfd, buff, sizeof(buff) - 1, 0,
  162.             (struct sockaddr*)&from, &pkgLen);
  163.         if (retVal < 0) {
  164.             perror("recvfrom");
  165.             printf("recvfrom error!\n");
  166.             close(sockfd);
  167.             exit(9);
  168.          }

  169.         printf("received msg: len=%d, ip=%s, port=%d, msg=%s\n", retVal,
  170.             (char *)inet_ntoa(from.sin_addr), htons(from.sin_port), buff);
  171.     } while ((retVal != sizeof(SECOND_SHAKE)) || (0 != strcmp(buff, SECOND_SHAKE)));

  172.     printf("<<<< [%s:%d] %s is received <<<<\n",
  173.         (char *)inet_ntoa(from.sin_addr), htons(from.sin_port), SECOND_SHAKE);

  174.     /* send the first handshake */
  175.     retVal = sendto(sockfd, THIRD_SHAKE, sizeof(THIRD_SHAKE), 0,
  176.             (struct sockaddr *)&to, sizeof(to));
  177.     if (retVal == ERROR) {
  178.         perror("sendto");
  179.         printf("sendto error!\n");
  180.         close(sockfd);
  181.         exit(10);
  182.     }

  183.     printf(">>>> [%s:%d] %s is sent >>>>\n",
  184.         (char *)inet_ntoa(to.sin_addr), htons(to.sin_port), THIRD_SHAKE);

  185.     if (isMultiCAST)
  186.         {
  187.          retVal = setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const char*)&mreq, sizeof(mreq));
  188.         if (retVal == ERROR) {
  189.             perror("DROP_MEMBERSHIP");
  190.             printf("setsockopt join error!\n");
  191.             close(sockfd);
  192.             exit(11);
  193.         }
  194.     }

  195.     close(sockfd);
  196. }

阅读(2975) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~