基于UDP协议的组播通信程序设计
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
SIO_MULTICAST_SCOPE,&nIP_TTL, sizeof(nIP_TTL),NULL,0,&cbRet,NULL,NULL); if (nRet) printf("WSAioctl(SIO_MULTICAST_SCOPE)
failed,Err:%d\n",WSAGetLastError()); //设置套接口工作方式,允许内部回送 bFlag = TRUE; nRet = WSAIoctl(hSock,
printf("WSAStartup failed:%d\r\n",nRet);
exit(1);}
//④将组播接收地址224.1.1.1转换为INET地址结构
nRet = WSAStringToAddress(strDestMulti,
AF_INET,NULL,(LPSOCKADDR)&stDestAddr,&iLen);
Err:%d\n",strDestMulti,WSAGetLastError()); exit(1); }
13
四、组播接收程序分析
//④初始化UDP套接口
hSock = WSASocket(AF_INET, SOCK_DGRAM,IPPROTO_UDP, (LPWSAPROTOCOL_INFO)NULL,0, WSA_FLAG_OVERLAPPED| WSA_FLAG_MULTIPOINT_C_LEAF| WSA_FLAG_MULTIPOINT_D_LEAF);
stSrcAddr.sin_addr.s_addr = INADDR_来自百度文库NY;
nRet = bind(hSock,(struct sockaddr FAR *)
&stSrcAddr,sizeof(struct sockaddr));
if (nRet == SOCKET_ERROR)
printf("bind failed,
Err:%d\n",WSAGetLastError()); exit(1); }
6
三、组播发送程序分析
//⑥设置套接口选项,允许重用本地地址和端口
bFlag = TRUE;
nRet = setsockopt(hSock,SOL_SOCKET,
SO_REUSEADDR,(char *)&bFlag,sizeof(bFlag));
if (nRet) {printf("WSAstringToAddress(%s)failed,
Err:%d\n",strDestMulti,WSAGetLastError());
exit(1);
}
5
三、组播发送程序分析
//⑤初始化一个组播发送套接口 hSock = WSASocket(AF_INET,
SIO_MULTIPOINT_LOOPBACK, &bFlag,sizeof(bFlag),NULL,0,
if (nRet) printf("WSAioctl(SIO_MULTICAST_SCOPE)failed,
Err:%d\n",WSAGetLastError()); //设置套接口工作方式,允许内部回送 bFlag = TRUE; nRet = WSAIoctl(hSock,
SIO_MULTIPOINT_LOOPBACK, &bFlag,sizeof(bFlag),NULL,0,&cbRet,NULL,NULL); if (nRet) printf("WSAioctl(SIO_MULTIPOINT_LOOPBACK)
u_short nDestPort = 6666;
char achInBuf[BUFSIZE];
12
四、组播接收程序分析
nRet = CheckWinsockVersion(); //②检查Winsock版本 if (nRet) {
printf("WSAStartup failed:%d\r\n",nRet); exit(1); } //③将字符串组播地址“224.1.1.1” 转换为套接口地址 nRet = WSAStringToAddress(strDestMulti, AF_INET,NULL,(LPSOCKADDR)&stDestAddr,&iLen); if (nRet) { printf("WSAstringToAddress(%s)failed,
if (hSock == INVALID_SOCKET) { printf("WSASocket()failed,Err:%d\n",WSAGetLastError()); exit(1);}
//⑤设置套接口选项,允许重用本地地址和端口
bFlag = TRUE; nRet = setsockopt(hSock,SOL_SOCKET,
1
一、组播发送程序
编程步骤
① 创建对话框工程multicastsend; ② 编辑对话框,添加编辑框控件类变量: m_SendMsg ③ 添加包含文件和Winsock动态链接库 ④ 添加按钮函数OnOK() ⑤ 编译、运行和测试
2
二、组播接收程序
编程步骤
① 创建对话框工程multicastRecv; ② 编辑对话框,添加编辑框控件类变量: m_RecvMsg ③ 添加包含文件和Winsock动态链接库 ④ 添加按钮函数OnOK() ⑤ 编译、运行和测试
if (nRet == SOCKET_ERROR)
printf("setsockopt()SO_REUSEADDR failed,
Err:%d\n",WSAGetLastError());
//⑦填写发送端套接口地址结构
stSrcAddr.sin_family = PF_INET;
stSrcAddr.sin_port = htons(nDestPort);
SOCK_DGRAM,IPPROTO_UDP, (LPWSAPROTOCOL_INFO)NULL, 0,WSA_FLAG_OVERLAPPED |WSA_FLAG_MULTIPOINT_C_LEAF| WSA_FLAG_MULTIPOINT_D_LEAF); if (hSock == INVALID_SOCKET) { printf("WSASocket()failed,
SO_REUSEADDR,(char *)&bFlag,sizeof(bFlag)); if (nRet == SOCKET_ERROR)
printf("setsockopt()SO_REUSEADDR failed, Err:%d\n",WSAGetLastError());
14
四、组播接收程序分析
11
四、组播接收程序分析
void CMulticastRecvDlg::OnButton1()
{
UdpReceiver(); //hwp 增加初始化代码
}
int CMulticastRecvDlg::UdpReceiver(VOID)
{
//① 变量声明
int nRet,i;
int nIP_TTL = 1;
}
//关闭套接口,释放内存
closesocket(hSock); WSACleanup(); }
10
三、组播发送程序分析
int CMulticastsendDlg::CheckWinsockVersion(VOID) {
WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(2,2); err = WSAStartup(wVersionRequested,&wsaData); if (err == 0) {if ((LOBYTE(wsaData.wVersion) == 2)
Err:%d\n",WSAGetLastError());
7
三、组播发送程序分析
//⑧ 设置套接口工作方式,将IP数据报的TTL值设定为1 nRet = WSAIoctl(hSock,
SIO_MULTICAST_SCOPE, &nIP_TTL,sizeof(nIP_TTL), NULL,0,&cbRet,NULL,NULL);
4
三、组播发送程序分析
//②读取发送字符串,并复制到发送缓冲区
achOutBuf UpdateData(TRUE);
strcpy(achOutBuf,m_SendMsg.GetBuffer(0));
nRet = CheckWinsockVersion();//③检查Winsock版本
if (nRet) {
&& (HIBYTE(wsaData.wVersion) == 2)) return 0; WSACleanup(); err = WSAVERNOTSUPPORTED;} printf("Winsock DLL does not support requested API version.\n"); return err;}
WSAGetLastError());
9
三、组播发送程序分析
//⑨循环发送数据
for (i=0;i<LOOPCOUNT;i++) { //组播发送 nRet = sendto(hSock,achOutBuf,1024,0, (SOCKADDR *)&stDestAddr,sizeof(stDestAddr)); if (nRet == SOCKET_ERROR) printf("WSASendTo()failed, Err:%d\n",WSAGetLastError());
BOOL bFlag;
DWORD dFlag,cbRet;
int iLen = MAXADDRSTR;
char strDestMulti[MAXADDRSTR] = "224.1.1.1";
SOCKADDR_IN stSrcAddr,stDestAddr;
SOCKET hSock,hNewSock;
3
三、组播发送程序分析
void CMulticastsendDlg::OnOK() { //① 变量声明
int nRet,i; int nIP_TTL = 1; //IP数据报的TTL值 BOOL bFlag; DWORD cbRet; int iLen = MAXADDRSTR; //定义组播组地址 char strDestMulti[MAXADDRSTR] = "224.1.1.1"; //发送、接收端地址结构 SOCKADDR_IN stSrcAddr,stDestAddr; SOCKET hSock; //消息发送、接收套接口 u_short nDestPort = 6666; //接收端口 char achOutBuf[BUFSIZE]; //发送缓冲区
&stSrcAddr,sizeof(struct sockaddr)); if (nRet == SOCKET_ERROR)
printf("bind failed,Err:%d\n", WSAGetLastError());
15
四、组播接收程序分析
//⑦设置套接口工作方式,将IP数据报的TTL值设定为1 nRet = WSAIoctl(hSock,
实验七 基于UDP协议的组播通信程序设计
实验目的
1)了解以太网环境下,组播通信的基本原理。 2)掌握利用VC开发工具编写组播通信程序的方 法。 3)基于Winsock API编写、测试组播发送、接 收程序。
实验任务
1)完成GUI组播发送程序的编码 2)完成GUI组播接收程序的编码 3)在共享局域网环境下完成发送、接收程 序的测试
//⑥填写发送端套接口地址结构,并绑定到hSock上 stSrcAddr.sin_family = PF_INET; stSrcAddr.sin_port = htons(nDestPort); stSrcAddr.sin_addr.s_addr = INADDR_ANY; nRet = bind(hSock,(struct sockaddr FAR *)
failed,Err:%d\n",WSAGetLastError());
8
三、组播发送程序分析
//填写接收端套接口地址结构
stDestAddr.sin_family = PF_INET; nRet = WSAHtons(hSock,nDestPort,
&(stDestAddr.sin_port)); if (nRet == SOCKET_ERROR) printf("WSAHtons()failed,Err:%d\n",
failed,Err:%d\n",WSAGetLastError()); //设置套接口工作方式,允许内部回送 bFlag = TRUE; nRet = WSAIoctl(hSock,
printf("WSAStartup failed:%d\r\n",nRet);
exit(1);}
//④将组播接收地址224.1.1.1转换为INET地址结构
nRet = WSAStringToAddress(strDestMulti,
AF_INET,NULL,(LPSOCKADDR)&stDestAddr,&iLen);
Err:%d\n",strDestMulti,WSAGetLastError()); exit(1); }
13
四、组播接收程序分析
//④初始化UDP套接口
hSock = WSASocket(AF_INET, SOCK_DGRAM,IPPROTO_UDP, (LPWSAPROTOCOL_INFO)NULL,0, WSA_FLAG_OVERLAPPED| WSA_FLAG_MULTIPOINT_C_LEAF| WSA_FLAG_MULTIPOINT_D_LEAF);
stSrcAddr.sin_addr.s_addr = INADDR_来自百度文库NY;
nRet = bind(hSock,(struct sockaddr FAR *)
&stSrcAddr,sizeof(struct sockaddr));
if (nRet == SOCKET_ERROR)
printf("bind failed,
Err:%d\n",WSAGetLastError()); exit(1); }
6
三、组播发送程序分析
//⑥设置套接口选项,允许重用本地地址和端口
bFlag = TRUE;
nRet = setsockopt(hSock,SOL_SOCKET,
SO_REUSEADDR,(char *)&bFlag,sizeof(bFlag));
if (nRet) {printf("WSAstringToAddress(%s)failed,
Err:%d\n",strDestMulti,WSAGetLastError());
exit(1);
}
5
三、组播发送程序分析
//⑤初始化一个组播发送套接口 hSock = WSASocket(AF_INET,
SIO_MULTIPOINT_LOOPBACK, &bFlag,sizeof(bFlag),NULL,0,
if (nRet) printf("WSAioctl(SIO_MULTICAST_SCOPE)failed,
Err:%d\n",WSAGetLastError()); //设置套接口工作方式,允许内部回送 bFlag = TRUE; nRet = WSAIoctl(hSock,
SIO_MULTIPOINT_LOOPBACK, &bFlag,sizeof(bFlag),NULL,0,&cbRet,NULL,NULL); if (nRet) printf("WSAioctl(SIO_MULTIPOINT_LOOPBACK)
u_short nDestPort = 6666;
char achInBuf[BUFSIZE];
12
四、组播接收程序分析
nRet = CheckWinsockVersion(); //②检查Winsock版本 if (nRet) {
printf("WSAStartup failed:%d\r\n",nRet); exit(1); } //③将字符串组播地址“224.1.1.1” 转换为套接口地址 nRet = WSAStringToAddress(strDestMulti, AF_INET,NULL,(LPSOCKADDR)&stDestAddr,&iLen); if (nRet) { printf("WSAstringToAddress(%s)failed,
if (hSock == INVALID_SOCKET) { printf("WSASocket()failed,Err:%d\n",WSAGetLastError()); exit(1);}
//⑤设置套接口选项,允许重用本地地址和端口
bFlag = TRUE; nRet = setsockopt(hSock,SOL_SOCKET,
1
一、组播发送程序
编程步骤
① 创建对话框工程multicastsend; ② 编辑对话框,添加编辑框控件类变量: m_SendMsg ③ 添加包含文件和Winsock动态链接库 ④ 添加按钮函数OnOK() ⑤ 编译、运行和测试
2
二、组播接收程序
编程步骤
① 创建对话框工程multicastRecv; ② 编辑对话框,添加编辑框控件类变量: m_RecvMsg ③ 添加包含文件和Winsock动态链接库 ④ 添加按钮函数OnOK() ⑤ 编译、运行和测试
if (nRet == SOCKET_ERROR)
printf("setsockopt()SO_REUSEADDR failed,
Err:%d\n",WSAGetLastError());
//⑦填写发送端套接口地址结构
stSrcAddr.sin_family = PF_INET;
stSrcAddr.sin_port = htons(nDestPort);
SOCK_DGRAM,IPPROTO_UDP, (LPWSAPROTOCOL_INFO)NULL, 0,WSA_FLAG_OVERLAPPED |WSA_FLAG_MULTIPOINT_C_LEAF| WSA_FLAG_MULTIPOINT_D_LEAF); if (hSock == INVALID_SOCKET) { printf("WSASocket()failed,
SO_REUSEADDR,(char *)&bFlag,sizeof(bFlag)); if (nRet == SOCKET_ERROR)
printf("setsockopt()SO_REUSEADDR failed, Err:%d\n",WSAGetLastError());
14
四、组播接收程序分析
11
四、组播接收程序分析
void CMulticastRecvDlg::OnButton1()
{
UdpReceiver(); //hwp 增加初始化代码
}
int CMulticastRecvDlg::UdpReceiver(VOID)
{
//① 变量声明
int nRet,i;
int nIP_TTL = 1;
}
//关闭套接口,释放内存
closesocket(hSock); WSACleanup(); }
10
三、组播发送程序分析
int CMulticastsendDlg::CheckWinsockVersion(VOID) {
WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(2,2); err = WSAStartup(wVersionRequested,&wsaData); if (err == 0) {if ((LOBYTE(wsaData.wVersion) == 2)
Err:%d\n",WSAGetLastError());
7
三、组播发送程序分析
//⑧ 设置套接口工作方式,将IP数据报的TTL值设定为1 nRet = WSAIoctl(hSock,
SIO_MULTICAST_SCOPE, &nIP_TTL,sizeof(nIP_TTL), NULL,0,&cbRet,NULL,NULL);
4
三、组播发送程序分析
//②读取发送字符串,并复制到发送缓冲区
achOutBuf UpdateData(TRUE);
strcpy(achOutBuf,m_SendMsg.GetBuffer(0));
nRet = CheckWinsockVersion();//③检查Winsock版本
if (nRet) {
&& (HIBYTE(wsaData.wVersion) == 2)) return 0; WSACleanup(); err = WSAVERNOTSUPPORTED;} printf("Winsock DLL does not support requested API version.\n"); return err;}
WSAGetLastError());
9
三、组播发送程序分析
//⑨循环发送数据
for (i=0;i<LOOPCOUNT;i++) { //组播发送 nRet = sendto(hSock,achOutBuf,1024,0, (SOCKADDR *)&stDestAddr,sizeof(stDestAddr)); if (nRet == SOCKET_ERROR) printf("WSASendTo()failed, Err:%d\n",WSAGetLastError());
BOOL bFlag;
DWORD dFlag,cbRet;
int iLen = MAXADDRSTR;
char strDestMulti[MAXADDRSTR] = "224.1.1.1";
SOCKADDR_IN stSrcAddr,stDestAddr;
SOCKET hSock,hNewSock;
3
三、组播发送程序分析
void CMulticastsendDlg::OnOK() { //① 变量声明
int nRet,i; int nIP_TTL = 1; //IP数据报的TTL值 BOOL bFlag; DWORD cbRet; int iLen = MAXADDRSTR; //定义组播组地址 char strDestMulti[MAXADDRSTR] = "224.1.1.1"; //发送、接收端地址结构 SOCKADDR_IN stSrcAddr,stDestAddr; SOCKET hSock; //消息发送、接收套接口 u_short nDestPort = 6666; //接收端口 char achOutBuf[BUFSIZE]; //发送缓冲区
&stSrcAddr,sizeof(struct sockaddr)); if (nRet == SOCKET_ERROR)
printf("bind failed,Err:%d\n", WSAGetLastError());
15
四、组播接收程序分析
//⑦设置套接口工作方式,将IP数据报的TTL值设定为1 nRet = WSAIoctl(hSock,
实验七 基于UDP协议的组播通信程序设计
实验目的
1)了解以太网环境下,组播通信的基本原理。 2)掌握利用VC开发工具编写组播通信程序的方 法。 3)基于Winsock API编写、测试组播发送、接 收程序。
实验任务
1)完成GUI组播发送程序的编码 2)完成GUI组播接收程序的编码 3)在共享局域网环境下完成发送、接收程 序的测试
//⑥填写发送端套接口地址结构,并绑定到hSock上 stSrcAddr.sin_family = PF_INET; stSrcAddr.sin_port = htons(nDestPort); stSrcAddr.sin_addr.s_addr = INADDR_ANY; nRet = bind(hSock,(struct sockaddr FAR *)
failed,Err:%d\n",WSAGetLastError());
8
三、组播发送程序分析
//填写接收端套接口地址结构
stDestAddr.sin_family = PF_INET; nRet = WSAHtons(hSock,nDestPort,
&(stDestAddr.sin_port)); if (nRet == SOCKET_ERROR) printf("WSAHtons()failed,Err:%d\n",