基于winsocket的UDP的数据单向通信过程
2012-11-25 16:52
405 查看
主机A(接收):
1. 先设置winsocket的版本:WSADATA WSAData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD( 2, 0 );
//使用WinSocket2.0
2. 利用WSAStartup(wVersionRequested,&WSAData);
启动网络资源
3. 申请本地的SOCKET:(用于后面与IP和端口的绑定,用这个SOCKET,来标记sockaddr_in(IP和端口))
SOCKET LocalSocket=socket(AF_INET,SOCK_DGRAM,0);
////创建客户端socket,是UDP(SOCK_DGRAM数据流)
4. 设置本地套接字sockaddr_in:
struct sockaddr_in sockaddr_local;
sockaddr_local.sin_family = AF_INET;
sockaddr_local.sin_port= htons(LocalPORT); //port number
sockaddr_local.sin_addr= *((struct in_addr *)Localhost->h_addr);//htonl(INADDR_ANY);//address
memset(&(sockaddr_local.sin_zero),0,8);
5. 绑定本地套接字sockaddr_in到本地套接口SOCKET:
bind(LocalSocket, (struct sockaddrfar *)&sockaddr_local, sizeof(sockaddr_local))
6. 设置远程套接字sockaddr_in:
struct sockaddr_in sockaddr_remote;
sockaddr_remote.sin_family= PF_INET;
sockaddr_remote.sin_port= htons(RemotePORT); //port number
sockaddr_remote.sin_addr= *((struct in_addr *)Remotehost->h_addr);//htonl(INADDR_ANY);//address
memset(&(sockaddr_remote.sin_zero),0,8);
7. 接收信息:(recvfrom和Sendto的套接口SOCKET都为本地的socket_local,后面sockaddr
*为远程套接字和套接字长度)
recvfrom(socket_local,ReceiveMassage,MAXMASSAGESIZE, 0,(sockaddr*)&sockaddr_remote,&sockaddr_inLenght) ;(阻塞接收)
8. 结束通信:
发送结束包,待定改
9. 释放本地的套接口和网络资源:
closesocket(LocalSocket);
WSACleanup();
主机B(发送):
1. 先设置winsocket的版本:WSADATA WSAData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD( 2, 0 );
//使用WinSocket2.0
2. 利用WSAStartup(wVersionRequested,&WSAData);
启动网络资源
3. 申请本地的SOCKET:(用于后面与IP和端口的绑定,用这个SOCKET,来标记sockaddr_in(IP和端口))
SOCKET LocalSocket=socket(AF_INET,SOCK_DGRAM,0);
////创建客户端socket,是UDP(SOCK_DGRAM数据流)
4. 设置本地套接字sockaddr_in:
struct sockaddr_in sockaddr_local;
sockaddr_local.sin_family = AF_INET;
sockaddr_local.sin_port= htons(LocalPORT); //port number
sockaddr_local.sin_addr= *((struct in_addr *)Localhost->h_addr);//htonl(INADDR_ANY);//address
memset(&(sockaddr_local.sin_zero),0,8);
5. 绑定本地套接字sockaddr_in到本地套接口SOCKET:
bind(LocalSocket, (struct sockaddrfar *)&sockaddr_local, sizeof(sockaddr_local))
6. 设置远程套接字sockaddr_in:
structsockaddr_in sockaddr_remote;
sockaddr_remote.sin_family= PF_INET;
sockaddr_remote.sin_port= htons(RemotePORT); //port number
sockaddr_remote.sin_addr= *((struct in_addr *)Remotehost->h_addr);//htonl(INADDR_ANY);//address
memset(&(sockaddr_remote.sin_zero),0,8);
7. 发送信息:(recvfrom和Sendto的套接口SOCKET都为本地的socket_local,后面sockaddr
*为远程套接字和套接字长度)
sendto(socket_local,Massage,sizeof(Massage),0,(sockaddr *)&sockaddr_remote,sizeof(sockaddr_remote));(不停发送)
8. 结束通信:
发送结束包,待定改
9. 释放本地的套接口和网络资源:
closesocket(LocalSocket);
WSACleanup();
代码如下:
简单的阻塞UDP通信(服务器).cpp:
#include
<stdio.h>
#include
<winsock2.h>
#include<fstream>
#include<iostream>
#pragma
comment( lib,
"WS2_32.lib" )
using namespace std;
#defineMAXMASSAGESIZE 512
#defineLocalIP
"127.0.0.1"
#defineRemoteIP
"127.0.0.1"
#defineLocalPORT 8888
#defineRemotePORT 9999
intERROTNUM=0;
voidShowErrorInfo(int ErrorNUM)
{
printf("ERROTNUM=%d\n",ErrorNUM);
printf("Inputany key to exit!!\n");
scanf("%d");
exit(1);
}
voidmain()
{
//启动通信的工作间和建立client套接字
WSADATA WSAData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD( 2, 0 );//使用winSocket2.0
ERROTNUM=WSAStartup(wVersionRequested,&WSAData);
//进行WSAStartup函数调用
if(ERROTNUM==0)
// 如果成功
{
printf("Socket inital OK !!\n");
}
else
{
ERROTNUM=-1;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
//////////////////////////////////////////////////本地参数设置
SOCKET LocalSocket=socket(AF_INET,SOCK_DGRAM,0);////创建客户端socket,是UDP(SOCK_DGRAM数据流)
if(LocalSocket==-1)
{
ERROTNUM=-2;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
HOSTENT *Localhost;
if((Localhost=gethostbyname(LocalIP))==NULL)
{
printf("gethostbyname出错!");
ERROTNUM=-3;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
//设置本地参数
struct sockaddr_in sockaddr_local;
sockaddr_local.sin_family = AF_INET;
sockaddr_local.sin_port= htons(LocalPORT);
//port number
sockaddr_local.sin_addr= *((struct in_addr *)Localhost->h_addr);//htonl(INADDR_ANY);//address
memset(&(sockaddr_local.sin_zero),0,8);
//绑定本地套接字
if(bind(LocalSocket, (structsockaddr far *)&sockaddr_local,
sizeof(sockaddr_local)))
{
ERROTNUM=-5;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
//////////////////////////////////////////////////远程参数设置
HOSTENT*Remotehost;
if((Remotehost=gethostbyname(RemoteIP))==NULL)
{
printf("gethostbyname出错!");
ERROTNUM=-3;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
//设置远程套接字
struct sockaddr_in sockaddr_remote;
sockaddr_remote.sin_family = PF_INET;
sockaddr_remote.sin_port= htons(RemotePORT);
//port number
sockaddr_remote.sin_addr= *((struct in_addr *)Remotehost->h_addr);//htonl(INADDR_ANY);//address
memset(&(sockaddr_remote.sin_zero),0,8);
//通信
voidSendANDReceiveMainloop(SOCKET socket_local,sockaddr_in sockaddr_remote );
SendANDReceiveMainloop( LocalSocket,sockaddr_remote );
//关闭本地的套接口和释放资源
closesocket(LocalSocket);
WSACleanup();
}
voidSendANDReceiveMainloop(SOCKET socket_local,sockaddr_in sockaddr_remote )
{
int ReceiveMassageLenght=0;
char ReceiveMassage[MAXMASSAGESIZE]={0,0,0};
int sockaddr_inLenght=sizeof(sockaddr_in);
do
{
//(阻塞接收)
ReceiveMassageLenght=recvfrom(socket_local,ReceiveMassage,MAXMASSAGESIZE, 0,(sockaddr*)&sockaddr_remote,&sockaddr_inLenght) ;
if(ReceiveMassageLenght==0)//对方关闭连接
break;
printf("MassageLenght=%d,ReceiveMassage:%s\n",ReceiveMassageLenght,ReceiveMassage);
}
while(ReceiveMassageLenght!=-1) ;
}
简单的阻塞UDP通信(客户机).cpp:
#include
<stdio.h>
#include
<winsock2.h>
#include<fstream>
#include<iostream>
#pragma
comment( lib,
"WS2_32.lib" )
using namespace std;
#defineMAXMASSAGESIZE 512
#defineLocalIP
"127.0.0.1"
#defineRemoteIP
"127.0.0.1"
#defineLocalPORT 9999
#defineRemotePORT 8888
intERROTNUM=0;
voidShowErrorInfo(int ErrorNUM)
{
printf("ERROTNUM=%d\n",ErrorNUM);
printf("Inputany key to exit!!\n");
scanf("%d\n");
exit(1);
}
voidmain()
{
//启动通信的工作间和建立client套接字
WSADATA WSAData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD( 2, 0 );//使用winSocket2.0
ERROTNUM=WSAStartup(wVersionRequested,&WSAData);
//进行WSAStartup函数调用
if(ERROTNUM==0)
// 如果成功
{
printf("Socket inital OK !!\n");
}
else
{
ERROTNUM=-1;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
//////////////////////////////////////////////////本地参数设置
SOCKET LocalSocket=socket(AF_INET,SOCK_DGRAM,0);////创建客户端socket,是TCP(SOCK_STREAM字节流)
if(LocalSocket==-1)
{
ERROTNUM=-2;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
HOSTENT *Localhost;
if((Localhost=gethostbyname(LocalIP))==NULL)
{
printf("gethostbyname出错!");
ERROTNUM=-3;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
//设置本地参数
struct sockaddr_in sockaddr_local;
sockaddr_local.sin_family = AF_INET;
sockaddr_local.sin_port= htons(LocalPORT);
//port number
sockaddr_local.sin_addr= *((struct in_addr *)Localhost->h_addr);//htonl(INADDR_ANY);//address
memset(&(sockaddr_local.sin_zero),0,8);
//绑定本地套接字
if(bind(LocalSocket, (structsockaddr far *)&sockaddr_local,
sizeof(sockaddr_local)))
{
ERROTNUM=-5;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
//////////////////////////////////////////////////远程参数设置
HOSTENT*Remotehost;
if((Remotehost=gethostbyname(RemoteIP))==NULL)
{
printf("gethostbyname出错!");
ERROTNUM=-3;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
//绑定远程套接字
struct sockaddr_in sockaddr_remote;
sockaddr_remote.sin_family = PF_INET;
sockaddr_remote.sin_port= htons(RemotePORT);
//port number
sockaddr_remote.sin_addr= *((struct in_addr *)Remotehost->h_addr);//htonl(INADDR_ANY);//address
memset(&(sockaddr_remote.sin_zero),0,8);
//通信
voidSendANDReceiveMainloop(SOCKET socket_local,sockaddr_in sockaddr_remote );
SendANDReceiveMainloop( LocalSocket,sockaddr_remote );
//关闭本地的套接口和释放资源
closesocket(LocalSocket);
WSACleanup();
}
voidSendANDReceiveMainloop(SOCKET socket_local,sockaddr_in sockaddr_remote )
{
for(inti=0;i<100000;i++)
{
charMassage[MAXMASSAGESIZE]={0,0,0};
printf("i=%d\n",i);
sprintf(Massage,"%d",i);
intSendReturnLenght=0;
//不停地发送
SendReturnLenght=sendto(socket_local,Massage,sizeof(Massage), 0,(sockaddr *)&sockaddr_remote,sizeof(sockaddr_remote));
if ( SendReturnLenght== -1)
{
ERROTNUM=-9;
ShowErrorInfo(ERROTNUM);//错误提示 ///*/
}
}
}
相关文章推荐
- 关于Qt中Udp通信过程中的接收数据部分注意
- Node.js实现基于TCP与UDP的数据通信
- Node.js权威指南 (7) - 实现基于TCP与UDP的数据通信
- 使用dgram模块实现基于UDP的数据通信
- 基于TCP和UDP的socket通信(C++实现)
- .Net开发笔记(十四) 基于“泵”的UDP通信(接上篇)
- 基于UDP协议下的客户端与服务器之间的通信
- UDT协议-基于UDP的可靠数据传输协议
- 基于DTMF音调的数据通信(二)——android平台
- iOS 一个在UDP/TCP通信时,对16进制、10进制、NSData等数据之间的转换发送等处理的工具类库,一行代码将16进制字符串轻松转换成UDP需要的指令数据
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (二) 发送自定义数据
- 与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室
- 基于UDP 实现客户端之间通信【2】
- Java--UDP,接收方接收发送方的数据(单向)
- tcp/udp建立连接并通信的过程
- UDT协议-基于UDP的可靠数据传输协议(转)
- 基于WinSocket的网络通信实现
- UDP协议做的windows窗口聊天程序---此处是单向的"同步"多线程对话框通信
- TCP UDP多机通信无法接收到数据的问题
- 转:UDT协议-基于UDP的可靠数据传输协议