基于winsocket的UDP的数据单向通信过程

来源:互联网 发布:淘宝上怎么换货啊 编辑:程序博客网 时间:2024/06/16 06:05

主机A(接收):

1.      先设置winsocket的版本:

  WSADATA WSAData;

  WORD  wVersionRequested;

  wVersionRequested = MAKEWORD( 2, 0 );

//使用WinSocket2.0

2.      利用WSAStartup(wVersionRequested,&WSAData);

启动网络资源

3.      申请本地的SOCKET:(用于后面与IP和端口的绑定,用这个SOCKET,来标记sockaddr_inIP和端口))

SOCKET LocalSocket=socket(AF_INET,SOCK_DGRAM,0);

////创建客户端socket,UDPSOCK_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.      接收信息:(recvfromSendto套接口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_inIP和端口))

SOCKET LocalSocket=socket(AF_INET,SOCK_DGRAM,0);

////创建客户端socket,UDPSOCK_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.      发送信息:(recvfromSendto套接口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);//错误提示  ///*/

    }

   

   

 

 }

 

}

 

 

 

原创粉丝点击