Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1576543
  • 博文数量: 239
  • 博客积分: 1760
  • 博客等级: 上尉
  • 技术积分: 1595
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-08 23:53
文章分类

全部博文(239)

文章存档

2016年(1)

2015年(28)

2014年(53)

2013年(42)

2012年(50)

2011年(65)

分类: Windows平台

2014-10-17 23:20:54

gcc 编译:

gcc -o server server.c -lws2_32
gcc -o client client.c -lws2_32 

返回代码,查询地址 

          

       

server端代码

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <Winsock2.h>
  3.   
  4. WORD RPort =6666; //客户端 port
  5. char RIP[16]="127.0.0.1";
  6. WORD LPort =7777; //服务端 port
  7. char LIP[16]="127.0.0.1";
  8.   
  9. SOCKET S;
  10. struct sockaddr_in rAddr;
  11. struct sockaddr_in lAddr;
  12.   
  13. WSADATA WD; //winsock dll 消息
  14. int r ; // result
  15.   
  16. void ShowInfo(char *info)
  17. {
  18.     puts(info);
  19.     exit(1);
  20. }
  21. void SetSockAddr(struct sockaddr_in *A,WORD Port,char *IP)
  22. {
  23.     A->sin_family=AF_INET; //tcp/tp协议 win下PF_INET 与AF_INET 相同
  24.     A->sin_port=htons(Port); //端口 主机字节顺序转网络字节顺序
  25.     A->sin_addr.s_addr=inet_addr(IP); //IP地址
  26. }
  27.  int main(int argc,char *argv[])
  28.  {
  29.   
  30.      WORD V;
  31.      V=0x0202 ; //winsock 版本 2.2 ,可以用 MAKEWORD(2,2) 表示
  32.   
  33.     r=WSAStartup(V,(LPWSADATA)&WD); //载入适合的winsock动态链接库
  34.     if(r == SOCKET_ERROR) ShowInfo("Start Error"); // SOCKET_ERROR 等于 -1
  35.   
  36.     S=socket(AF_INET,SOCK_DGRAM,0); // create win socket
  37.                                                                                    //socket(int af,int type,int protocel)
  38.   
  39.                                                                                   //程序结束前使用closesocket(S)
  40.     if(S == SOCKET_ERROR) ShowInfo("socket create error");
  41.   
  42.     SetSockAddr(&lAddr,LPort,LIP);
  43.   
  44.     r=bind(S,(struct sockaddr far *)&lAddr,sizeof(lAddr));
  45.     if(r == SOCKET_ERROR) ShowInfo("bind error");
  46.   
  47.     SetSockAddr(&rAddr,RPort,RIP);
  48.   
  49.     char msg[80];
  50.   
  51.         do{
  52.             puts("wait client....");
  53.   
  54.             //接收client端信息
  55.             r=recv(S,msg,80,0);
  56.             if(r ==SOCKET_ERROR ) ShowInfo("receive error");
  57.               
  58.             msg[r]='\0';
  59.               
  60.               
  61.             if(!strcmp(msg,"exit")) {
  62.                 printf("client is exit\n");
  63.                 break;
  64.             }
  65.   
  66.             puts("client message :");
  67.             puts(msg);
  68.   
  69.             // inform client message is succeed receive
  70.             strcpy(msg,"ok!");
  71.             r=sendto(S,msg,strlen(msg),0,(struct sockaddr far *)&rAddr,sizeof(rAddr));
  72.             if( r == SOCKET_ERROR ) ShowInfo("send error");
  73.   
  74.         } while(1);
  75.         closesocket(S);
  76.         WSACleanup();
  77.   
  78.      return 0;
  79.  }
client端代码

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <winsock2.h>
  3.   
  4. WORD rport=7777;
  5. char rip[16]="127.0.0.1";
  6. WORD lport=6666;
  7. char lip[16]="127.0.0.1";
  8.   
  9. SOCKET S;
  10.   
  11. struct sockaddr_in raddr;
  12. struct sockaddr_in laddr;
  13.   
  14. WSADATA WD;
  15.   
  16. int r;
  17.   
  18. void ShowInfo(char *info){
  19.     puts(info);
  20.     printf("error code is %d\n",WSAGetLastError());
  21.     exit(1);
  22. }
  23.   
  24. void SetSockAddr(struct sockaddr_in *A,WORD Port ,char *IP)
  25. {
  26.     A->sin_family =AF_INET;
  27.     A->sin_port = htons(Port);
  28.     A->sin_addr.s_addr=inet_addr(IP);
  29.   
  30. }
  31.   
  32. int main(int argc ,char *argv[])
  33. {
  34.     // load winsock dll
  35.     r = WSAStartup(MAKEWORD(2,2),&WD);
  36.     if(r == SOCKET_ERROR) ShowInfo(" start error");
  37.   
  38.     //create win sokcet
  39.     S = socket(AF_INET,SOCK_DGRAM,0);
  40.     if(S == SOCKET_ERROR) ShowInfo( "socket error");
  41.   
  42.     //message
  43.     char msg[80];
  44.   
  45.     //set socket localhost
  46.     SetSockAddr(&laddr,lport,lip);
  47.   
  48.     //指定 Socket 的 Local 位址
  49.     r=bind(S,(struct sockaddr far *)&laddr,sizeof(laddr));
  50.     if(r == SOCKET_ERROR ) ShowInfo(" bind error");
  51.   
  52.     //set socket server
  53.     SetSockAddr(&raddr,rport,rip);
  54.   
  55.     //--------get message and send messge
  56.     do{
  57.         puts("send:");
  58.         gets(msg);
  59.   
  60.         //send to server
  61.         r=sendto(S,msg,strlen(msg),0,(struct sockaddr far *)&raddr,sizeof(raddr));
  62.         if( r == SOCKET_ERROR ) ShowInfo("send error");
  63.   
  64.         // exit
  65.         if(! strcmp( msg,"exit")) break;
  66.   
  67.         puts("send ok! wait server recieve");
  68.   
  69.         //get recieve
  70.         r=recv(S,msg,80,0);
  71.         if( r== SOCKET_ERROR ) ShowInfo("recieve error");
  72.         puts("server:");
  73.         msg[r]='\0';
  74.         puts(msg);
  75.   
  76.   
  77.     }while(1);
  78.   
  79.     closesocket(S); //close socket
  80.     WSACleanup(); //中止Windows Sockets DLL的使用
  81.   
  82.     return 0;
  83. }
转载自:


阅读(4162) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~