-
c网络编程-多播
-
-
-
-
-
-
-
-
-
- #ifndef UDP_GROUP_BROADCAST_H
- #define UDP_GROUP_BROADCAST_H
-
-
- #include <stdio.h>
- #include <Winsock2.h>
- #include <Ws2tcpip.h>
- #include <stdlib.h>
- #pragma comment(lib, "Ws2_32.lib")
-
-
-
-
-
-
- extern SOCKET gServerSocket;
- extern SOCKET gClientSocket;
- extern char gServerIP[];
- extern char gClientIP[];
-
-
-
-
- int CreatServerUDPSocket(SOCKET *pServerSocket, char chServerIP[], /
- unsigned short uPort);
- int SetSocketOption(SOCKET *pServerSocket, int nLevel,/
- int nOptname, const char FAR *pOptval, int nOptlen);
- int SendTo(SOCKET *pServerSocket, char *pBuff, int nOffset, int nLen);
-
-
-
- int CreatClientUDPSocket(SOCKET *pClientSocket, char chClientIP[], /
- unsigned short uPort);
- int ReciveFrom(SOCKET *pSocket, char pBuff[], int nOffset, int nLen);
-
-
-
- #endif /* UDP_GROUP_BROADCAST_H */
-
-
-
-
-
-
-
-
-
- #include "E:/MyDevLib/Windows/UDP_GroupBroadcast.h"
- #include <error.h>
- #include <assert.h>
-
-
-
- #define MAX_REC_BUFF_LEN 128
- #define TIME_SLEEP 100 /* milliseconds */
- #define PORT_UDP 1225
-
-
-
- int main( int argc, char *argv[], char *envp[])
- {
- long lCount;
- int nReturn;
- char chRecBuff[MAX_REC_BUFF_LEN];
- const int nOn = 1;
- int nReciveCount;
-
-
- if(argc == 2)
- {
- memcpy(gClientIP, argv[1], strlen(argv[1]));
- printf("Note: config IP[%s]./n", argv[1]);
- }
-
-
- nReturn = CreatClientUDPSocket(&gClientSocket, gClientIP, PORT_UDP);
- if(!nReturn)
- {
- printf("Note: creat a client UDP socket OK./n");
- }
- else
- {
- printf("Error: creat a client UDP socket was failed! [error=%d]/n", nReturn);
- goto error_exit;
- }
-
-
- printf("Note: beginning recive data .../n");
- lCount = 1;
- nReciveCount = 15;
- while(1)
- {
- memset(chRecBuff, 0, sizeof(chRecBuff));
- nReturn = ReciveFrom(&gClientSocket, chRecBuff, 0, sizeof(chRecBuff));
- if (!nReturn)
- {
- printf("Note: recived data[%s]./n", chRecBuff);
- }
- else
- {
- printf("Error: recive data was failed! [error=%d]/n", WSAGetLastError());
- goto error_exit;
- }
-
- Sleep(TIME_SLEEP);
- }
-
-
- error_exit:
- closesocket(gClientSocket);
- WSACleanup();
- printf("Note: process exit./n");
- return 0;
- }
-
-
-
-
-
-
-
-
-
- #include "E:/MyDevLib/Windows/UDP_GroupBroadcast.h"
- #include <error.h>
- #include <assert.h>
-
-
-
- #define MAX_SEND_BUFF_LEN 128
- #define TIME_SLEEP 100 /* milliseconds */
- #define PORT_UDP 1225
-
-
-
- int main( int argc, char *argv[], char *envp[])
- {
- long lCount;
- int nReturn, nSendFlag;
- char chSendBuff[MAX_SEND_BUFF_LEN];
-
- memset(chSendBuff, 0, sizeof(chSendBuff));
-
-
- if(argc == 2)
- {
- memcpy(gServerIP, argv[1], strlen(argv[1]));
- printf("Note: config IP[%s]./n", argv[1]);
- }
-
- nReturn = CreatServerUDPSocket(&gServerSocket, gServerIP, PORT_UDP);
- if(!nReturn)
- {
- printf("Note: creat server UDP socket OK./n");
- }
- else
- {
- printf("Error: creat server UDP socket was failed! [error=%d]/n", nReturn);
- goto error_exit;
- }
-
- printf("Note: beginning send data .../n");
-
- lCount = 1;
- nSendFlag = 0x8;
- while(1)
- {
-
- nSendFlag = lCount != 0xf;
- sprintf(chSendBuff, "Wenxy test %d", lCount++);
- nReturn = SendTo(&gServerSocket, chSendBuff, 0, strlen(chSendBuff));
- if(!nReturn)
- {
- printf("Note: send data [%s]./n", chSendBuff);
- }
- else
- {
- printf("Error: send data was failed! [error=%d]/n", nReturn);
- goto error_exit;
- }
- Sleep(TIME_SLEEP);
- }
-
-
- error_exit:
- closesocket(gServerSocket);
- WSACleanup();
- printf("Note: process exit./n");
- return 0;
- }
-
-
-
-
-
-
-
-
-
-
- #include "UDP_GroupBroadcast.h"
-
-
-
- #define PORT_UDP_SERVER 1225
- #define IP_SERVER "192.168.1.125"
- #define IP_MULTICAST "224.0.0.99" /* 多播地址 */
- #define MAX_IP_LEN 16
-
-
- SOCKET gServerSocket, gClientSocket;
- char gServerIP[MAX_IP_LEN];
- char gClientIP[MAX_IP_LEN];
- struct sockaddr_in gServerAddr;
- struct sockaddr_in gClientAddr;
-
-
-
- int CreatServerUDPSocket(SOCKET *pServerSocket, char chServerIP[], /
- unsigned short uPort)
- {
- unsigned long ulOptval;
- unsigned short uVersionRequested;
- WSADATA wsaData;
- struct ip_mreq mreq;
- const int nOn = 1;
- const int nRouteTTL = 10;
- const int loopback = 0;
-
- uVersionRequested = MAKEWORD(2, 2);
-
- if(0 != WSAStartup(uVersionRequested, &wsaData))
- {
- return WSAGetLastError();
- }
-
- *pServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
- if(INVALID_SOCKET == *pServerSocket)
- {
- WSACleanup();
- return WSAGetLastError();
- }
-
-
- #if 0
- if(SOCKET_ERROR == setsockopt(*pServerSocket, SOL_SOCKET, /
- SO_REUSEADDR, (char *)&nOn, sizeof(nOn)))
- {
- closesocket(*pServerSocket);
- WSACleanup();
- return WSAGetLastError();
- }
- #endif
-
-
- if(SOCKET_ERROR == setsockopt(*pServerSocket, IPPROTO_IP, IP_MULTICAST_TTL, /
- (char *)&nRouteTTL, sizeof(nRouteTTL)))
- {
- closesocket(*pServerSocket);
- WSACleanup();
- return WSAGetLastError();
- }
-
-
- if(SOCKET_ERROR == setsockopt(*pServerSocket, IPPROTO_IP, IP_MULTICAST_LOOP, /
- (char*)&loopback, sizeof(loopback)))
- {
- closesocket(*pServerSocket);
- WSACleanup();
- return WSAGetLastError();
- }
-
-
- memset(&gServerAddr, 0, sizeof(struct sockaddr_in));
- gServerAddr.sin_family = AF_INET;
- gServerAddr.sin_addr.s_addr = inet_addr(chServerIP);
- gServerAddr.sin_port = htons(uPort);
- if(SOCKET_ERROR == bind(*pServerSocket, &gServerAddr, sizeof(gServerAddr)))
- {
- closesocket(*pServerSocket);
- WSACleanup();
- return WSAGetLastError();
- }
-
-
-
- memset(&mreq, 0, sizeof(mreq));
- mreq.imr_interface.S_un.S_addr = inet_addr(chServerIP);
- mreq.imr_multiaddr.S_un.S_addr = inet_addr(IP_MULTICAST);
-
- if(SOCKET_ERROR == setsockopt(*pServerSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,/
- &mreq, sizeof(mreq)))
- {
- closesocket(*pServerSocket);
- WSACleanup();
- return WSAGetLastError();
- }
-
- return 0;
- }
-
-
- int SetSocketOption(SOCKET *pSocket, int nLevel,/
- int nOptname, const char FAR *pOptval, int nOptlen)
- {
- if(SOCKET_ERROR == setsockopt(*pSocket, nLevel, /
- nOptname, pOptval, nOptlen))
- {
- return WSAGetLastError();
- }
- return 0;
- }
-
-
- int ReciveFrom(SOCKET *pSocket, char pBuff[], int nOffset, int nLen)
- {
- struct sockaddr_in ClientAddr;
- int nReturn = 0, nAddrLen;
- memset(&ClientAddr, 0, sizeof(struct sockaddr_in));
-
- nAddrLen = sizeof(ClientAddr);
- nReturn = recvfrom(*pSocket, pBuff, nLen, /
- 0, (struct sockaddr *)&ClientAddr, &nAddrLen);
- #if 0
- memcpy(pBuff, "wenxy", sizeof("wenxy"));
- nReturn = 5;
- #endif
- if(nReturn == SOCKET_ERROR)
- {
- return -1;
- }
- return 0;
- }
-
-
- int SendTo(SOCKET *pServerSocket, char *pBuff, int nOffset, int nLen)
- {
- int nSendLen;
- struct sockaddr_in remote;
-
- memset(&remote, 0, sizeof(remote));
- remote.sin_addr.s_addr = inet_addr ( IP_MULTICAST );
- remote.sin_family = AF_INET;
- remote.sin_port = htons(PORT_UDP_SERVER);
-
- nSendLen = sendto(*pServerSocket, pBuff, nLen, 0, /
- (struct sockaddr *)&remote, sizeof(remote));
- if(nSendLen <= 0)
- {
- return WSAGetLastError();
- }
- return 0;
- }
-
-
-
-
-
- int CreatClientUDPSocket(SOCKET *pClientSocket, char chClientIP[], /
- unsigned short uPort)
- {
- unsigned long ulOptval;
- unsigned short uVersionRequested;
- WSADATA wsaData;
- struct ip_mreq mreq;
- const int nOn = 1;
- uVersionRequested = MAKEWORD(2, 2);
-
- if(0 != WSAStartup(uVersionRequested, &wsaData))
- {
- return WSAGetLastError();
- }
-
- *pClientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
- if(INVALID_SOCKET == *pClientSocket)
- {
- closesocket(*pClientSocket);
- WSACleanup();
- return WSAGetLastError();
- }
-
- #if 0
-
- if(SOCKET_ERROR == setsockopt(*pClientSocket, SOL_SOCKET, /
- SO_REUSEADDR, (char *)&nOn, sizeof(nOn)))
- {
- closesocket(*pClientSocket);
- WSACleanup();
- return WSAGetLastError();
- }
- #endif
-
- #if 1
-
- memset(&gClientAddr, 0, sizeof(gClientAddr));
- gClientAddr.sin_family = AF_INET;
- gClientAddr.sin_addr.s_addr = inet_addr(chClientIP);
- gClientAddr.sin_port = htons( uPort );
- if(SOCKET_ERROR == bind(*pClientSocket, &gClientAddr, sizeof(gClientAddr)))
- {
- closesocket(*pClientSocket);
- WSACleanup();
- return WSAGetLastError();
- }
- #endif
-
-
- memset(&mreq, 0, sizeof(mreq));
- mreq.imr_interface.S_un.S_addr = inet_addr(chClientIP);
- mreq.imr_multiaddr.S_un.S_addr = inet_addr(IP_MULTICAST);
-
- if(SOCKET_ERROR == setsockopt(*pClientSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,/
- &mreq, sizeof(mreq)))
- {
- closesocket(*pClientSocket);
- WSACleanup();
- return WSAGetLastError();
- }
-
- return 0;
- }
-
相关阅读:
python自动化_day6_面向对象_组合,继承,多态
python自动化_day5_二分查找,模块和正则表达式
python自动化_day4_迭代器生成器内置函数和匿名函数
python自动化_day4_装饰器复习和装饰器进阶
python目录
python3的ExecJS安装使用
python运算符^&|~>><<
python有哪些优点跟缺点
26-30题
21-25题
-
原文地址:https://www.cnblogs.com/pengkunfan/p/3486803.html
Copyright © 2020-2023
润新知