zoukankan      html  css  js  c++  java
  • c网络编程-多播

    1. /* 编译通过环境,Windows XP sp2,Windows 2003 server SDK,VC++6.0 sp5. */   
    2.   
    3. /************************************************************************ 
    4. * File: UDP group broadcast header file 
    5. * Author: WenXiaoyong,Wen_kernel@163.com 
    6. * Copyright: (C) WenXiaoyong,2007 
    7. *                                                                    
    8. ************************************************************************/  
    9.   
    10. #ifndef UDP_GROUP_BROADCAST_H  
    11. #define UDP_GROUP_BROADCAST_H  
    12.   
    13.   
    14. #include <stdio.h>  
    15. #include <Winsock2.h>  
    16. #include <Ws2tcpip.h>  
    17. #include <stdlib.h>  
    18. #pragma comment(lib, "Ws2_32.lib")  
    19.   
    20.   
    21. /* define microes */  
    22.   
    23.   
    24. /* declare extern variables */  
    25. extern SOCKET gServerSocket;  
    26. extern SOCKET gClientSocket;  
    27. extern char gServerIP[];  
    28. extern char gClientIP[];  
    29.   
    30.   
    31. /* declare functions */  
    32. /* server */  
    33. int CreatServerUDPSocket(SOCKET *pServerSocket, char chServerIP[], /  
    34.        unsigned short uPort);  
    35. int SetSocketOption(SOCKET *pServerSocket, int nLevel,/  
    36.  int nOptname, const char FAR *pOptval, int nOptlen);  
    37. int SendTo(SOCKET *pServerSocket, char *pBuff, int nOffset, int nLen);  
    38.   
    39.   
    40. /* client */  
    41. int CreatClientUDPSocket(SOCKET *pClientSocket, char chClientIP[], /  
    42.        unsigned short uPort);  
    43. int ReciveFrom(SOCKET *pSocket, char pBuff[], int nOffset, int nLen);  
    44.   
    45.    
    46.   
    47. #endif /* UDP_GROUP_BROADCAST_H */  
    48.   
    49. /************************************************************************ 
    50. * File: main funtion file 
    51. * Author: WenXiaoyong,Wen_kernel@163.com 
    52. * Copyright: (C) WenXiaoyong,2007 
    53. *                                                                    
    54. ************************************************************************/  
    55.   
    56.   
    57. #include "E:/MyDevLib/Windows/UDP_GroupBroadcast.h"  
    58. #include <error.h>  
    59. #include <assert.h>  
    60.   
    61.   
    62. /* define microes */  
    63. #define MAX_REC_BUFF_LEN 128  
    64. #define TIME_SLEEP 100 /* milliseconds */  
    65. #define PORT_UDP 1225  
    66.   
    67.   
    68. /* main function */  
    69. int main( int argc, char *argv[], char *envp[])  
    70. {  
    71.  long lCount;  
    72.  int nReturn;  
    73.  char chRecBuff[MAX_REC_BUFF_LEN];  
    74.  const int nOn = 1; /* 允许程序的多个实例运行在同一台机器上 */  
    75.  int nReciveCount;  
    76.    
    77.  /* config IP */  
    78.  if(argc == 2)  
    79.  {  
    80.   memcpy(gClientIP, argv[1], strlen(argv[1]));  
    81.   printf("Note: config IP[%s]./n", argv[1]);  
    82.  }  
    83.   
    84.  /* creat a UDP socket */  
    85.  nReturn = CreatClientUDPSocket(&gClientSocket, gClientIP, PORT_UDP);  
    86.  if(!nReturn)  
    87.  {  
    88.   printf("Note: creat a client UDP socket OK./n");  
    89.  }  
    90.  else   
    91.  {  
    92.   printf("Error: creat a client UDP socket was failed! [error=%d]/n", nReturn);  
    93.   goto error_exit;  
    94.  }  
    95.   
    96.  /* recive data */  
    97.  printf("Note: beginning recive data .../n");  
    98.  lCount = 1;  
    99.  nReciveCount = 15;  
    100.  while(1)  
    101.  {  
    102.   memset(chRecBuff, 0, sizeof(chRecBuff));  
    103.   nReturn = ReciveFrom(&gClientSocket, chRecBuff, 0, sizeof(chRecBuff));  
    104.   if (!nReturn)  
    105.   {   
    106.    printf("Note: recived data[%s]./n", chRecBuff);  
    107.   }  
    108.   else  
    109.   {  
    110.    printf("Error: recive data was failed! [error=%d]/n", WSAGetLastError());  
    111.    goto error_exit;  
    112.   }  
    113.   
    114.   Sleep(TIME_SLEEP);  
    115.  }  
    116.   
    117.   
    118. error_exit:  
    119.  closesocket(gClientSocket);  
    120.  WSACleanup();  
    121.  printf("Note: process exit./n");  
    122.  return 0;  
    123. }  
    124.   
    125.   
    126. /************************************************************************ 
    127. * File: main funtion file 
    128. * Author: WenXiaoyong,Wen_kernel@163.com 
    129. * Copyright: (C) WenXiaoyong,2007 
    130. *                                                                    
    131. ************************************************************************/  
    132.   
    133. #include "E:/MyDevLib/Windows/UDP_GroupBroadcast.h"  
    134. #include <error.h>  
    135. #include <assert.h>  
    136.   
    137.   
    138. /* define microes */  
    139. #define MAX_SEND_BUFF_LEN 128  
    140. #define TIME_SLEEP 100 /* milliseconds */  
    141. #define PORT_UDP 1225  
    142.   
    143.   
    144. /* main function */  
    145. int main( int argc, char *argv[], char *envp[])  
    146. {  
    147.  long lCount;  
    148.  int nReturn, nSendFlag;  
    149.  char chSendBuff[MAX_SEND_BUFF_LEN];  
    150.    
    151.  memset(chSendBuff, 0, sizeof(chSendBuff));  
    152.   
    153.  /* config IP */  
    154.  if(argc == 2)  
    155.  {  
    156.   memcpy(gServerIP, argv[1], strlen(argv[1]));  
    157.   printf("Note: config IP[%s]./n", argv[1]);  
    158.  }  
    159.  /* creat a UDP socket */  
    160.  nReturn = CreatServerUDPSocket(&gServerSocket, gServerIP, PORT_UDP);  
    161.  if(!nReturn)  
    162.  {  
    163.   printf("Note: creat server UDP socket OK./n");  
    164.  }  
    165.  else  
    166.  {  
    167.   printf("Error: creat server UDP socket was failed! [error=%d]/n", nReturn);  
    168.   goto error_exit;  
    169.  }  
    170.   
    171.  printf("Note: beginning send data .../n");  
    172.    
    173.  lCount = 1;  
    174.  nSendFlag = 0x8;  
    175.  while(/*nSendFlag*/1)  
    176.  {  
    177.   /* send some datas */  
    178.   nSendFlag = lCount != 0xf;  
    179.   sprintf(chSendBuff, "Wenxy test %d", lCount++);  
    180.   nReturn = SendTo(&gServerSocket, chSendBuff, 0, strlen(chSendBuff));  
    181.   if(!nReturn)  
    182.   {  
    183.    printf("Note: send data [%s]./n", chSendBuff);  
    184.   }  
    185.   else  
    186.   {  
    187.    printf("Error: send data was failed! [error=%d]/n", nReturn);  
    188.    goto error_exit;  
    189.   }  
    190.   Sleep(TIME_SLEEP);  
    191.  }  
    192.   
    193.   
    194. error_exit:  
    195.  closesocket(gServerSocket);  
    196.  WSACleanup();  
    197.  printf("Note: process exit./n");  
    198.  return 0;  
    199. }  
    200.   
    201.   
    202. /************************************************************************ 
    203. * File: UDP group broadcast implement file 
    204. * Author: WenXiaoyong,Wen_kernel@163.com 
    205. * Copyright: (C) WenXiaoyong,2007 
    206. *                                                                    
    207. ************************************************************************/  
    208.   
    209.   
    210. #include "UDP_GroupBroadcast.h"  
    211.   
    212.   
    213. /* define microes */  
    214. #define PORT_UDP_SERVER 1225  
    215. #define IP_SERVER  "192.168.1.125"   
    216. #define IP_MULTICAST  "224.0.0.99" /* 多播地址 */  
    217. #define MAX_IP_LEN 16  
    218.   
    219. /* globals variables */  
    220. SOCKET gServerSocket, gClientSocket;  
    221. char gServerIP[MAX_IP_LEN];   
    222. char gClientIP[MAX_IP_LEN];  
    223. struct sockaddr_in gServerAddr;  
    224. struct sockaddr_in gClientAddr;  
    225.   
    226.   
    227. /* functions */  
    228. int CreatServerUDPSocket(SOCKET *pServerSocket, char chServerIP[], /  
    229.        unsigned short uPort)  
    230. {  
    231.  unsigned long ulOptval;  
    232.  unsigned short uVersionRequested;  
    233.  WSADATA wsaData;  
    234.  struct ip_mreq mreq;  
    235.   const int nOn = 1; /* 允许程序的多个实例运行在同一台机器上 */  
    236.  const int nRouteTTL = 10;  
    237.     const int loopback = 0; /* 禁止回馈 */  
    238.   
    239.  uVersionRequested = MAKEWORD(2, 2);  
    240.    
    241.  if(0 != WSAStartup(uVersionRequested, &wsaData))  
    242.  {  
    243.   return WSAGetLastError();  
    244.  }  
    245.    
    246.  *pServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);  
    247.  if(INVALID_SOCKET == *pServerSocket)  
    248.  {  
    249.         WSACleanup();  
    250.   return WSAGetLastError();   
    251.  }  
    252.   
    253.  /* allow rebind for other instance */  
    254. #if 0  
    255.  if(SOCKET_ERROR == setsockopt(*pServerSocket, SOL_SOCKET, /  
    256.   SO_REUSEADDR, (char *)&nOn, sizeof(nOn)))  
    257.  {  
    258.   closesocket(*pServerSocket);  
    259.         WSACleanup();  
    260.         return WSAGetLastError();  
    261.  }  
    262. #endif  
    263.    
    264.  /* set route TTL */  
    265.     if(SOCKET_ERROR == setsockopt(*pServerSocket, IPPROTO_IP, IP_MULTICAST_TTL, /  
    266.                (char *)&nRouteTTL, sizeof(nRouteTTL)))  
    267.     {  
    268.   closesocket(*pServerSocket);  
    269.         WSACleanup();  
    270.         return WSAGetLastError();  
    271.     }  
    272.   
    273.  /* no loop back */  
    274.     if(SOCKET_ERROR == setsockopt(*pServerSocket, IPPROTO_IP, IP_MULTICAST_LOOP, /  
    275.                (char*)&loopback, sizeof(loopback)))  
    276.     {  
    277.   closesocket(*pServerSocket);  
    278.         WSACleanup();  
    279.         return WSAGetLastError();  
    280.     }  
    281.    
    282.  /* bind */  
    283.  memset(&gServerAddr, 0, sizeof(struct sockaddr_in));  
    284.  gServerAddr.sin_family = AF_INET;  
    285.  gServerAddr.sin_addr.s_addr = inet_addr(chServerIP); /* INADDR_ANY; */  
    286.  gServerAddr.sin_port = htons(uPort);  
    287.  if(SOCKET_ERROR == bind(*pServerSocket, &gServerAddr, sizeof(gServerAddr)))  
    288.  {  
    289.   closesocket(*pServerSocket);  
    290.   WSACleanup();  
    291.   return WSAGetLastError();  
    292.  }  
    293.   
    294.   
    295.  /* join a group of multicast */  
    296.     memset(&mreq, 0, sizeof(mreq));  
    297.     mreq.imr_interface.S_un.S_addr = inet_addr(chServerIP);  
    298.     mreq.imr_multiaddr.S_un.S_addr = inet_addr(IP_MULTICAST);   
    299.   
    300.     if(SOCKET_ERROR == setsockopt(*pServerSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,/  
    301.                &mreq, sizeof(mreq)))  
    302.     {  
    303.   closesocket(*pServerSocket);  
    304.   WSACleanup();  
    305.   return WSAGetLastError();  
    306.      }  
    307.   
    308.  return 0;  
    309. }  
    310.   
    311.   
    312. int SetSocketOption(SOCKET *pSocket, int nLevel,/  
    313.   int nOptname, const char FAR *pOptval, int nOptlen)  
    314. {  
    315.  if(SOCKET_ERROR == setsockopt(*pSocket, nLevel, /  
    316.   nOptname, pOptval, nOptlen))  
    317.  {  
    318.   return WSAGetLastError();  
    319.  }  
    320.  return 0;  
    321. }  
    322.   
    323.   
    324. int ReciveFrom(SOCKET *pSocket, char pBuff[], int nOffset, int nLen)  
    325. {  
    326.  struct sockaddr_in ClientAddr;  
    327.  int nReturn = 0, nAddrLen;  
    328.  memset(&ClientAddr, 0, sizeof(struct sockaddr_in));  
    329.   
    330.  nAddrLen = sizeof(ClientAddr);  
    331.  nReturn = recvfrom(*pSocket, pBuff, nLen, /  
    332.   0, (struct sockaddr *)&ClientAddr, &nAddrLen);  
    333. #if 0  
    334.  memcpy(pBuff, "wenxy"sizeof("wenxy"));  
    335.  nReturn = 5;  
    336. #endif  
    337.  if(nReturn == SOCKET_ERROR)  
    338.  {  
    339.   return -1;  
    340.  }  
    341.  return 0;  
    342. }  
    343.   
    344.   
    345. int SendTo(SOCKET *pServerSocket, char *pBuff, int nOffset, int nLen)  
    346. {  
    347.  int nSendLen;  
    348.     struct sockaddr_in remote;  
    349.   
    350.     memset(&remote, 0, sizeof(remote));  
    351.     remote.sin_addr.s_addr = inet_addr ( IP_MULTICAST );  
    352.     remote.sin_family = AF_INET;  
    353.     remote.sin_port = htons(PORT_UDP_SERVER);  
    354.    
    355.  nSendLen = sendto(*pServerSocket, pBuff, nLen, 0, /  
    356.   (struct sockaddr *)&remote, sizeof(remote));  
    357.  if(nSendLen <= 0)  
    358.  {  
    359.   return WSAGetLastError();  
    360.  }  
    361.  return 0;  
    362. }  
    363.   
    364.   
    365. /************************************************************************/  
    366. /* client functions */  
    367.   
    368. int CreatClientUDPSocket(SOCKET *pClientSocket, char chClientIP[], /  
    369.        unsigned short uPort)  
    370. {  
    371.  unsigned long ulOptval;  
    372.  unsigned short uVersionRequested;  
    373.  WSADATA wsaData;  
    374.  struct ip_mreq mreq;  
    375.  const int nOn = 1;  
    376.  uVersionRequested = MAKEWORD(2, 2);  
    377.    
    378.  if(0 != WSAStartup(uVersionRequested, &wsaData))  
    379.  {  
    380.   return WSAGetLastError();  
    381.  }  
    382.    
    383.  *pClientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);  
    384.  if(INVALID_SOCKET == *pClientSocket)  
    385.  {  
    386.   closesocket(*pClientSocket);  
    387.   WSACleanup();  
    388.   return WSAGetLastError();   
    389.  }  
    390.   
    391. #if 0  
    392.  /* set socket option */  
    393.  if(SOCKET_ERROR == setsockopt(*pClientSocket, SOL_SOCKET, /  
    394.   SO_REUSEADDR, (char *)&nOn, sizeof(nOn)))  
    395.  {  
    396.   closesocket(*pClientSocket);  
    397.   WSACleanup();  
    398.   return WSAGetLastError();  
    399.  }  
    400. #endif  
    401.    
    402. #if 1  
    403.  /* bind */  
    404.  memset(&gClientAddr, 0, sizeof(gClientAddr));  
    405.  gClientAddr.sin_family = AF_INET;  
    406.  gClientAddr.sin_addr.s_addr = inet_addr(chClientIP);  
    407.  gClientAddr.sin_port = htons( uPort );   
    408.  if(SOCKET_ERROR == bind(*pClientSocket, &gClientAddr, sizeof(gClientAddr)))  
    409.  {  
    410.   closesocket(*pClientSocket);  
    411.   WSACleanup();  
    412.   return WSAGetLastError();  
    413.  }  
    414. #endif  
    415.    
    416.  /* join a group of multicast */  
    417.     memset(&mreq, 0, sizeof(mreq));  
    418.     mreq.imr_interface.S_un.S_addr = inet_addr(chClientIP);  
    419.     mreq.imr_multiaddr.S_un.S_addr = inet_addr(IP_MULTICAST);   
    420.   
    421.     if(SOCKET_ERROR == setsockopt(*pClientSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,/  
    422.                &mreq, sizeof(mreq)))  
    423.     {  
    424.   closesocket(*pClientSocket);  
    425.   WSACleanup();  
    426.   return WSAGetLastError();  
    427.      }  
    428.   
    429.  return 0;  
    430. }  
  • 相关阅读:
    C博客作业02--循环结构
    博客作业01--顺序分支结构
    C博客作业00--我的第一篇博客
    实验四
    实验三
    网络管理snmp实验
    C语言博客作业02--循环结构
    C博客作业03--函数
    循环结构
    C语言博客作业02--循环结构
  • 原文地址:https://www.cnblogs.com/pengkunfan/p/3486803.html
Copyright © 2011-2022 走看看