c网络编程-多播
- /* 编译通过环境,Windows XP sp2,Windows 2003 server SDK,VC++6.0 sp5. */
- /************************************************************************
- * File: UDP group broadcast header file
- * Author: WenXiaoyong,Wen_kernel@163.com
- * Copyright: (C) WenXiaoyong,2007
- *
- ************************************************************************/
- #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")
- /* define microes */
- /* declare extern variables */
- extern SOCKET gServerSocket;
- extern SOCKET gClientSocket;
- extern char gServerIP[];
- extern char gClientIP[];
- /* declare functions */
- /* server */
- 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);
- /* client */
- int CreatClientUDPSocket(SOCKET *pClientSocket, char chClientIP[], /
- unsigned short uPort);
- int ReciveFrom(SOCKET *pSocket, char pBuff[], int nOffset, int nLen);
- #endif /* UDP_GROUP_BROADCAST_H */
- /************************************************************************
- * File: main funtion file
- * Author: WenXiaoyong,Wen_kernel@163.com
- * Copyright: (C) WenXiaoyong,2007
- *
- ************************************************************************/
- #include "E:/MyDevLib/Windows/UDP_GroupBroadcast.h"
- #include <error.h>
- #include <assert.h>
- /* define microes */
- #define MAX_REC_BUFF_LEN 128
- #define TIME_SLEEP 100 /* milliseconds */
- #define PORT_UDP 1225
- /* main function */
- int main( int argc, char *argv[], char *envp[])
- {
- long lCount;
- int nReturn;
- char chRecBuff[MAX_REC_BUFF_LEN];
- const int nOn = 1; /* 允许程序的多个实例运行在同一台机器上 */
- int nReciveCount;
- /* config IP */
- if(argc == 2)
- {
- memcpy(gClientIP, argv[1], strlen(argv[1]));
- printf("Note: config IP[%s]./n", argv[1]);
- }
- /* creat a UDP socket */
- 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;
- }
- /* recive data */
- 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;
- }
- /************************************************************************
- * File: main funtion file
- * Author: WenXiaoyong,Wen_kernel@163.com
- * Copyright: (C) WenXiaoyong,2007
- *
- ************************************************************************/
- #include "E:/MyDevLib/Windows/UDP_GroupBroadcast.h"
- #include <error.h>
- #include <assert.h>
- /* define microes */
- #define MAX_SEND_BUFF_LEN 128
- #define TIME_SLEEP 100 /* milliseconds */
- #define PORT_UDP 1225
- /* main function */
- int main( int argc, char *argv[], char *envp[])
- {
- long lCount;
- int nReturn, nSendFlag;
- char chSendBuff[MAX_SEND_BUFF_LEN];
- memset(chSendBuff, 0, sizeof(chSendBuff));
- /* config IP */
- if(argc == 2)
- {
- memcpy(gServerIP, argv[1], strlen(argv[1]));
- printf("Note: config IP[%s]./n", argv[1]);
- }
- /* creat a UDP socket */
- 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(/*nSendFlag*/1)
- {
- /* send some datas */
- 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;
- }
- /************************************************************************
- * File: UDP group broadcast implement file
- * Author: WenXiaoyong,Wen_kernel@163.com
- * Copyright: (C) WenXiaoyong,2007
- *
- ************************************************************************/
- #include "UDP_GroupBroadcast.h"
- /* define microes */
- #define PORT_UDP_SERVER 1225
- #define IP_SERVER "192.168.1.125"
- #define IP_MULTICAST "224.0.0.99" /* 多播地址 */
- #define MAX_IP_LEN 16
- /* globals variables */
- SOCKET gServerSocket, gClientSocket;
- char gServerIP[MAX_IP_LEN];
- char gClientIP[MAX_IP_LEN];
- struct sockaddr_in gServerAddr;
- struct sockaddr_in gClientAddr;
- /* functions */
- 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();
- }
- /* allow rebind for other instance */
- #if 0
- if(SOCKET_ERROR == setsockopt(*pServerSocket, SOL_SOCKET, /
- SO_REUSEADDR, (char *)&nOn, sizeof(nOn)))
- {
- closesocket(*pServerSocket);
- WSACleanup();
- return WSAGetLastError();
- }
- #endif
- /* set route TTL */
- if(SOCKET_ERROR == setsockopt(*pServerSocket, IPPROTO_IP, IP_MULTICAST_TTL, /
- (char *)&nRouteTTL, sizeof(nRouteTTL)))
- {
- closesocket(*pServerSocket);
- WSACleanup();
- return WSAGetLastError();
- }
- /* no loop back */
- if(SOCKET_ERROR == setsockopt(*pServerSocket, IPPROTO_IP, IP_MULTICAST_LOOP, /
- (char*)&loopback, sizeof(loopback)))
- {
- closesocket(*pServerSocket);
- WSACleanup();
- return WSAGetLastError();
- }
- /* bind */
- memset(&gServerAddr, 0, sizeof(struct sockaddr_in));
- gServerAddr.sin_family = AF_INET;
- gServerAddr.sin_addr.s_addr = inet_addr(chServerIP); /* INADDR_ANY; */
- gServerAddr.sin_port = htons(uPort);
- if(SOCKET_ERROR == bind(*pServerSocket, &gServerAddr, sizeof(gServerAddr)))
- {
- closesocket(*pServerSocket);
- WSACleanup();
- return WSAGetLastError();
- }
- /* join a group of multicast */
- 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;
- }
- /************************************************************************/
- /* client functions */
- 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
- /* set socket option */
- if(SOCKET_ERROR == setsockopt(*pClientSocket, SOL_SOCKET, /
- SO_REUSEADDR, (char *)&nOn, sizeof(nOn)))
- {
- closesocket(*pClientSocket);
- WSACleanup();
- return WSAGetLastError();
- }
- #endif
- #if 1
- /* bind */
- 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
- /* join a group of multicast */
- 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;
- }
c网络编程-多播的更多相关文章
- Linux网络编程--多播
一.多播介绍 什么是多播? 单播用于两个主机之间的端对端通信,广播用于一个主机对整个局域网上所有主机上的数据通信.单播和广播是两个极端,要么对一个主机进行通信,要么对整个局域网上的主机进行通信.实际情 ...
- linux网络编程之一-----多播(组播)编程
什么是多播 组播(Multicast)是网络一种点对多(one to many)的通信方式,通过报文复制完成网络中一台server对应多台接收者的高效数据传 送.对其形象的比喻就是类似于广播电台和电视 ...
- c++ 网络编程(六)LINUX下 socket编程 多播与广播 实现一次发送所有组客户端都能接收到
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9614288.html 一.多播 锲子:有这么一种情况,网络电台可能需要同时向成千上万的用户传输 ...
- TCP/IP网络编程之多播与广播
多播 多播方式的数据传输是基于UDP完成的,因此,与UDP服务端/客户端的实现非常接近.区别在于,UDP数据传输以单一目标进行,而多播数据同时传递到加入(注册)特定组的大量主机.换言之,采用多播方式时 ...
- Linux IO模型和网络编程模型
术语概念描述: IO有内存IO.网络IO和磁盘IO三种,通常我们说的IO指的是后两者. 阻塞和非阻塞,是函数/方法的实现方式,即在数据就绪之前是立刻返回还是等待. 以文件IO为例,一个IO读过程是文件 ...
- UNIX网络编程-基本API介绍(二)
参考链接:http://www.cnblogs.com/riky/archive/2006/11/24/570713.aspx 1.getsockname和getpeername getsocknam ...
- Unix网络编程--卷一:套接字联网API
UNIX网络编程--卷一:套接字联网API 本书面对的读者是那些希望自己编写的程序能够使用成为套接字(socket)的API进行彼此通信的人. 目录: 0.准备环境 1.简介 2.传输层:TCP.UD ...
- Android网络编程系列 一 TCP/IP协议族
在学习和使用Android网路编程时,我们接触的仅仅是上层协议和接口如Apache的httpclient或者Android自带的httpURlconnection等等.对于这些接口的底层实现我们也有必 ...
- 【Java基础】Java网络编程基础知识
什么是网络编程 网络编程是通过使用套接字来达到进程间通信目的,那什么是套接字呢?其实套接字是支持TCP/IP的网络通信的基本操作单元,可以看做是不同主机之间的进程进行双向通信的端点,简单的说就是通信的 ...
随机推荐
- docker 概念初识
1 docker 优势,一次创建或配置可以在任意时间.任意环境让应用正常的运行. 快速交付和部署,开发人员可以使用镜像快速构建一套标准的开发环境:开发完后测试和运维人员可以直接使用相同的环境来部署代码 ...
- 利用PHPExcel实现数据保存到excel文件
include(dirname(__FILE__) .'/phpexcel-1.7.7/Classes/PHPExcel.php'); include(dirname(__FILE__) .'/php ...
- java多线程实验 滚动字
package com.rgy.Test; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt ...
- jquery+ashx checkbox 单选判断是否true 和 false 传值操作
示例图: html标签代码: <p></p> <label for="checkbox" style="float:left" & ...
- C++ Dll 编写入门
一.前言 自从微软推出16位的Windows操作系统起,此后每种版本的Windows操作系统都非常依赖于动态链接库(DLL)中的函数和数据,实际上 Windows操作系统中几乎所有的内容都由DLL以一 ...
- Oracle PLSQL Demo - 16.弱类型REF游标[没有指定查询类型,已指定返回类型]
declare Type ref_cur_variable IS REF cursor; cur_variable ref_cur_variable; rec_emp scott.emp%RowTyp ...
- git使用教程&&问题列表
git教程[转] http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git push ...
- c++之——虚析构函数
先看代码: #include<iostream> using namespace std; class Parent { public: Parent() :a(), b(), c() { ...
- VC中使用Matlab Engine出现"无法找到libeng.dll"的问题
VC中使用Matlab Engine出现"无法找到libeng.dll"的问题 本以为使这个原因 ,其实不是我2了 #include "engine.h" // ...
- 一款基于jquery和css3实现的摩天轮式分享按钮
之前分享了很多css3实现的按钮.今天要给大家带来一款基于jquery和css3实现的摩天轮式分享按钮.这款分享按钮页面底部有一个toggle按钮,单击该按钮,摩天轮按钮以动画的形式出现,各个分享按钮 ...