// Module: Mc.c
//
// Purpose:
// This sample illustrates IP multicasting using the Winsock 1
// method of joining and leaving an multicast group. This sample
// may be invoked as either a sender or a receiver. In both cases,
// the specified multicast group is joined.
//
// Compile:
// cl -o Mcastws1 Mcastws1.c Wsock32.lib
//
// Command Line Options/Parameters
// mcastws1.exe [-s] [-m:str] [-p:int] [-i:str] [-l] [-n:int]
// -s Act as sender; otherwise receive data
// -m:str Dotted decimal IP multicast address to join
// -p:int Port number to use
// -i:str Local interface to use
// -l Disable the loopback
// -n:int Number of messages to send or receive
//
#include "../InitSocket/InitSock.h"
CInitMySock InitSock;
#include <stdio.h>
//#include <windows.h>
//#include <Ws2tcpip.h>
#define MCASTADDR "234.5.6.7"
#define MCASTPORT 25000
#define BUFSIZE 1024
#define DEFAULT_COUNT 500
#define IP_ADD_SOURCE_MEMBERSHIP 15
/* join IP group/source */
#define IP_DROP_SOURCE_MEMBERSHIP 16
/* leave IP group/source */
#define IP_BLOCK_SOURCE 17
/* block IP group/source */
#define IP_UNBLOCK_SOURCE 18
/* unblock IP group/source */
#define IPPROTO_IP 0
BOOL bSender = FALSE, // Act as sender?
bLoopBack = FALSE, // Disable loopback?
bLocal = FALSE, // 是否限制为本地子网?
bLimit = FALSE; // 是否拒绝接受邻座同学发的数据?
DWORD dwInterface, // Local interface to bind to
dwMulticastGroup, // Multicast group to join
dwLimitAddr,
dwCount; // Number of messages to send/receive
short iPort; // Port number to use
struct ip_mreq_source {
struct in_addr imr_multiaddr; /* IP multicast address of group */
struct in_addr imr_interface; /* local IP address of interface */
struct in_addr imr_sourceaddr;
};
//
// Function: usage
//
// Description:
// Print usage information and exit
//
void usage(char *progname)
{
printf("usage: %s -s -m:str -p:int -i:str -l -n:int\n",
progname);
printf(" -s Act as server (send data); otherwise\n");
printf(" receive data.\n");
printf(" -m: str Dotted decimal multicast IP addres to join\n");
printf(" The default group is: %s\n", MCASTADDR);
printf(" -p: int Port number to use\n");
printf(" The default port is: %d\n", MCASTPORT);
printf(" -i: str Local interface to bind to; by default \n");
printf(" use INADDRY_ANY\n");
printf(" -l Disable loopback\n");
printf(" -n: int Number of messages to send/receive\n");
printf(" -t Set TTL value to localplace\n");
printf(" -x Set refuseing IP of recve data\n\n");
//ExitProcess(-1);
}
//
// Function: ValidateArgs
//
// Description:
// Parse the command line arguments and set some global flags,
// depending on the values
//
void ValidateArgs(int argc, char **argv)
{
int i;
dwInterface = INADDR_ANY;//inet_addr("192.168.2.138");
dwMulticastGroup = inet_addr(MCASTADDR);
iPort = MCASTPORT;
dwCount = DEFAULT_COUNT;
for(i = 1; i < argc; i++)
{
if ((argv[i][0] == '-') || (argv[i][0] == '/'))
{
switch (tolower(argv[i][1]))
{
case 's': // Sender
bSender = TRUE;
break;
case 'm': // Multicast group to join
if (strlen(argv[i]) > 3)
dwMulticastGroup = inet_addr(&argv[i][3]);
break;
case 'i': // Local interface to use
if (strlen(argv[i]) > 3)
dwInterface = inet_addr(&argv[i][3]);
break;
case 'p': // Port number to use
if (strlen(argv[i]) > 3)
iPort = atoi(&argv[i][3]);
break;
case 'l': // Disable loopback?
bLoopBack = TRUE;
break;
case 'n': // Number of messages to send/recv
dwCount = atoi(&argv[i][3]);
break;
case 't':
bLocal = TRUE;
break;
case 'x':
if (strlen(argv[i]) > 3)
{
bLimit = TRUE;
dwLimitAddr = inet_addr(&argv[i][3]);
}
break;
default:
usage(argv[0]);
break;
}
}
}
return;
}
//
// Function: main
//
// Description:
// Parse the command line arguments, load the Winsock library,
// create a socket and join the multicast group. If this program
// is started as a sender then begin sending messages to the
// multicast group; otherwise, call recvfrom() to read messages
// sent to the group.
//
int main(int argc, char **argv)
{
struct sockaddr_in local,
remote,
from;
struct ip_mreq mcast;
struct ip_mreq_source mrefuse;
SOCKET sockM;
TCHAR recvbuf[BUFSIZE],
sendbuf[BUFSIZE];
int len = sizeof(struct sockaddr_in),
optval,
ret;
DWORD i=0;
// Parse the command line and load Winsock
//
usage(argv[0]);
ValidateArgs(argc, argv);
// Create the socket. In Winsock 1 you don't need any special
// flags to indicate multicasting.
//
if ((sockM = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
printf("socket failed with: %d\n", WSAGetLastError());
return -1;
}
BOOL bReuseAddr = TRUE; //重用地址
setsockopt(sockM, SOL_SOCKET, SO_REUSEADDR, (char*)&bReuseAddr, sizeof(BOOL));
// Bind the socket to the local interface. This is done so
// that we can receive data.
//
local.sin_family = AF_INET;
local.sin_port = htons(iPort);
local.sin_addr.s_addr = dwInterface;
if (bind(sockM, (struct sockaddr *)&local,
sizeof(local)) == SOCKET_ERROR)
{
printf("bind failed with: %d\n", WSAGetLastError());
closesocket(sockM);
return -1;
}
// Setup the im_req structure to indicate what group we want
// to join as well as the interface
//
remote.sin_family = AF_INET;
remote.sin_port = htons(iPort);
remote.sin_addr.s_addr = dwMulticastGroup;
mcast.imr_multiaddr.s_addr = dwMulticastGroup;
mcast.imr_interface.s_addr = dwInterface;
//jion
if (setsockopt(sockM, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mcast, sizeof(mcast))
== SOCKET_ERROR)
{
printf("jion failed with: %d\n", WSAGetLastError());
closesocket(sockM);
return -1;
}
// Set the TTL to something else. The default TTL is 1.
//
if (!bLocal)
{
optval = 8;
if (setsockopt(sockM, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&optval, sizeof(optval))
== SOCKET_ERROR)
{
printf("setTTL failed with: %d\n", WSAGetLastError());
closesocket(sockM);
return -1;
}
}
if (bLimit)
{
mrefuse.imr_multiaddr.S_un.S_addr = dwMulticastGroup;
mrefuse.imr_interface.S_un.S_addr = inet_addr("192.168.2.138");//dwInterface;
mrefuse.imr_sourceaddr.S_un.S_addr = inet_addr("192.168.2.138");//dwLimitAddr;
if(::setsockopt(sockM,IPPROTO_IP,IP_BLOCK_SOURCE,(char*)&mrefuse,sizeof(mrefuse))
== SOCKET_ERROR)
{
printf("refuse data failed with: %d\n", WSAGetLastError());
closesocket(sockM);
return -1;
}
}
// Disable the loopback if selected. Note that on NT4 and Wi
没有合适的资源?快使用搜索试试~ 我知道了~
多播通信 加入多播组
共7个文件
plg:1个
opt:1个
dsw:1个
4星 · 超过85%的资源 需积分: 50 35 下载量 159 浏览量
2010-06-02
16:34:17
上传
评论
收藏 14KB RAR 举报
温馨提示
实现多播通信,加入多播组,相互发送信息 实现多播通信,加入多播组,相互发送信息
资源推荐
资源详情
资源评论
收起资源包目录
.rar (7个子文件)
多播
Multicasting
Multicasting.dsp 4KB
Mult.cpp 10KB
Multicasting.ncb 57KB
Multicasting.opt 49KB
Multicasting.plg 1KB
Multicasting.dsw 549B
InitSocket
InitSock.h 373B
共 7 条
- 1
资源评论
- zhenzhenmomo2012-12-28与我想要找的不符合,但是内容还是挺好的
- wudy5168655312013-05-01运行出错,join failed with 10055,不晓得哪儿出了问题
- prolove00562013-11-26还行吧,我也就参考一下
- plovem2016-06-02还好啦,入门,可以学习学习啊
- bigtools2014-08-04内容不错,试一下
pengbo1233
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功