Chinaunix首页 | 论坛 | 博客
  • 博客访问: 659338
  • 博文数量: 175
  • 博客积分: 2457
  • 博客等级: 大尉
  • 技术积分: 1488
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-13 20:31
文章分类

全部博文(175)

文章存档

2012年(22)

2011年(153)

分类: C/C++

2011-09-02 22:11:30

  1. 对于UDP协议~因为不像TCP协议那样事先有connect~所以调用recvfrom前必须先用
    sendto, WSASendTo, or WSAJoinLeaf.来明确服务器端在哪里~(UDP没有bind 没有connect)
    MSDN上说:recvfrom的第一个参数(即套接字)必须是绑定的~(Descriptor identifying a bound socket.)

    (the socket can become bound implicitly to a local address through sendto, WSASendTo, or WSAJoinLeaf.)

    所以得recvfrom的第一个参数必须是已经调用过sendto的套接字myclient,用没调用过sendto的套接字sockrecv是错的~~~错误10022

  2. //Server

  3. #include <WinSock2.h>
  4. #include <iostream>
  5. #pragma comment(lib,"ws2_32.lib")
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif

  9. // The one and only application object

  10. using namespace std;

  11. int main()
  12. {
  13.         WSADATA wsaData;
  14.         int err;
  15.         err = WSAStartup(MAKEWORD(2,2), &wsaData );
  16.         SOCKET Srv=socket(AF_INET,SOCK_DGRAM,0);
  17.         SOCKADDR_IN addSrv;
  18.         addSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
  19.         addSrv.sin_family=AF_INET;
  20.         addSrv.sin_port=htons(4567);

  21.         bind(Srv,(SOCKADDR*)&addSrv,sizeof(SOCKADDR));
  22.         char buf[100];
  23.         char buf2[100]="hello 客服端,我是你的服务器";
  24.         SOCKADDR_IN Client;
  25.         int len=sizeof(SOCKADDR);
  26.         recvfrom(Srv,buf,100,0,(SOCKADDR*)&Client,&len);
  27.         printf("%s\n%s\n",buf,inet_ntoa(Client.sin_addr));
  28.         while(TRUE)
  29.         {
  30.                 Sleep(1000);
  31.                 if(sendto(Srv,buf2,strlen("hello 客服端,我是你的服务器")+1,0,(SOCKADDR*)&Client,len)==SOCKET_ERROR )//把addSrv改成Client 这样才能发送成功
  32.                 {
  33.                         printf("send failed\n");
  34.                 }
  35.         }

  36.         closesocket(Srv);
  37.         WSACleanup( );
  38.         return 0;
  39. }




  1. //client

  2. #include <WinSock2.h>
  3. #include <iostream>
  4. #pragma comment(lib,"ws2_32.lib")
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #endif


  8. // The one and only application object

  9. using namespace std;

  10. int main()
  11. {
  12.         WORD wVersionRequested;
  13.         WSADATA wsaData;
  14.         int err;
  15.         wVersionRequested = MAKEWORD( 2, 2 );
  16.         err = WSAStartup(wVersionRequested,&wsaData );
  17.         if ( err != 0 )
  18.         {
  19.                 return 0;
  20.         }
  21.         if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
  22.         {
  23.                         WSACleanup( );
  24.                         return 0;
  25.         }
  26.         SOCKET myclient=socket(AF_INET,SOCK_DGRAM,0);
  27.         SOCKADDR_IN addrcl;
  28.         addrcl.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
  29.         addrcl.sin_family=AF_INET;
  30.         addrcl.sin_port=htons(4567);
  31.         char buf[100]="hello 服务器,我是你的客服端";
  32.         int len=sizeof(SOCKADDR);
  33.         sendto(myclient,buf,strlen("hello 服务器,我是你的客服端")+1,0,(SOCKADDR*)&addrcl,len);
  34.         //把这句sendto注释掉也会出现跟下面myclient错写成sockrecv一样的错误10022 //说明recvfrom是跟sendto关联??//为什么???直接recvfrom为什么不行??
  35.         memset(&addrcl,0,sizeof(addrcl));
  36.         char buf2[100]=" ";
  37.         while(TRUE)
  38.         {
  39.                 if(recvfrom(myclient,buf2,100,0,(SOCKADDR*)&addrcl,&len)==SOCKET_ERROR)
  40.                 {
  41.                         printf("recvfrom failed\n");
  42.                         printf("%d\n",WSAGetLastError());
  43.                 }//为什么这里没办法接收到服务器端发来的字符串?//懂了sockrecv是错的,得改成 myclient
  44.                 //sockrecv与 myclient的唯一差别是sockrecv没有sendto二myclient有 //为什么这样??
  45.                 else
  46.                 {
  47.                         printf("%s\n%s\n",buf2,inet_ntoa(addrcl.sin_addr));
  48.                 }
  49.         }
  50.         closesocket(myclient);
  51.         WSACleanup( );
  52.         return 1;
  53. }
阅读(1303) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~