BSD Socket

我们知道BSD Socket是标准的套接字规范,那么怎么在windows使用他们呢?

    我们首先要引用<winsock2.h>和ws2_32.lib

    然后,执行WSAStartup

  1. #ifdef _WIN32  
  2.   WORD wVersionRequested;  
  3.   WSADATA wsaData;  
  4.   wVersionRequested = MAKEWORD(1, 1);  
  5.   iStatus = WSAStartup(wVersionRequested, &wsaData);  
  6.   if (iStatus != 0) {  
  7.     return 0;  
  8.   }  
  9.   if (LOBYTE(wsaData.wVersion) != 1 ||  
  10.     HIBYTE(wsaData.wVersion) != 1) {  
  11.     WSACleanup();  
  12.     return 0;  
  13.   }  
  14. #endif  
    WSAStartup函数
  1. int WSAStartup(  
  2.   _In_   WORD wVersionRequested,  
  3.   _Out_  LPWSADATA lpWSAData  
  4. );  


    最后,执行关闭socket、清理工作

  1. #ifdef _WIN32  
  2.   closesocket(sockfd);  
  3.   WSACleanup();  
  4. #endif  
   WSACleanup函数
  1. int WSACleanup(void);  


    以下程序在Win7 + VC10下编译通过
    客户端程序:
  1. // prjClt.cpp : Defines the entry point for the console application.  
  2. //  
  3. /** 
  4.  * Networking program is Win version with BSD Socket 
  5.  * Client side 
  6.  * 
  7.  * Author: xiaobin 
  8.  * Date: 2013-12-12 
  9.  */  
  10. #include "stdafx.h"  
  11.   
  12. #ifdef _WIN32  
  13. #include <winsock2.h>  
  14. #include <ws2tcpip.h>  
  15. #pragma comment(lib, "ws2_32.lib")  
  16. #else  
  17. #include <sys/socket.h>  
  18. #include <netinet/in.h>  
  19. #include <arpa/in.h>  
  20. #endif  
  21.   
  22. #define MAXLINE 254  
  23. #define DEFAULT_PORT 3293  
  24.   
  25. int main(int argc, char* argv[])  
  26. {  
  27.     int                  sockfd;  
  28.     struct  sockaddr_in  servaddr;  
  29.     int                  iStatus;  
  30.     char                *sendBuff = "this is test message!";  
  31.       
  32.     #ifdef _WIN32  
  33.       WORD wVersionRequested;  
  34.       WSADATA wsaData;  
  35.       wVersionRequested = MAKEWORD(1, 1);  
  36.       iStatus = WSAStartup(wVersionRequested, &wsaData);  
  37.       if (iStatus != 0) {  
  38.         return 0;  
  39.       }  
  40.       if (LOBYTE(wsaData.wVersion) != 1 ||  
  41.         HIBYTE(wsaData.wVersion) != 1) {  
  42.         WSACleanup();  
  43.         return 0;  
  44.       }  
  45.     #endif  
  46.   
  47.     if (argc != 2)  
  48.         printf("Usage: <IPaddress>>\n");  
  49.       
  50.     sockfd = socket(AF_INET, SOCK_STREAM, 0);  
  51.     if (sockfd < 0)  
  52.         printf("socket error\n");  
  53.       
  54.     /* check Server address */  
  55.     // inet_pton is win version - InetPton  
  56.     if (InetPton(AF_INET, argv[1], &servaddr.sin_addr) < 0)  
  57.         printf("inet_pton error for %s", argv[1]);  
  58.   
  59.     /* Set serveraddr */  
  60.     memset(&servaddr, 0, sizeof(servaddr));  
  61.     servaddr.sin_family = AF_INET;  
  62.     servaddr.sin_addr.S_un.S_addr = inet_addr(argv[1]);  
  63.     servaddr.sin_port   = htons(DEFAULT_PORT);  
  64.   
  65.     printf("%s%s%s\n""Connecting ", argv[1], " ...");  
  66.   
  67.     /* connect server */  
  68.     iStatus = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));  
  69.     if ( iStatus < 0 ) {  
  70.         closesocket(sockfd);  
  71.         printf("connect error\n");  
  72.     }  
  73.   
  74.     printf("%s\n""Writing...");  
  75.     /* Write data */  
  76.     iStatus = send(sockfd, sendBuff, (int)strlen(sendBuff), 0);  
  77.     if (iStatus < 0) {  
  78.         printf("%s\n""write data error");  
  79.     }  
  80.     printf("%s\n""Writed.");  
  81.           
  82.     #ifdef _WIN32  
  83.       closesocket(sockfd);  
  84.       WSACleanup();  
  85.     #endif  
  86.   
  87.     return 0;  
  88. }  
    注意:InetPton函数只能在Windows Version >=6上实现!

    

    服务器端程序:

  1. // prjSrv.cpp : Defines the entry point for the console application.  
  2. //  
  3. /** 
  4.  * Networking program is Win version with BSD Socket 
  5.  * Server side 
  6.  * 
  7.  * Author: xiaobin 
  8.  * Date: 2012-12-18 23:35 
  9.  */  
  10. #include "stdafx.h"  
  11.   
  12. #ifdef _WIN32  
  13. #include <winsock2.h>  
  14. #pragma comment(lib, "ws2_32.lib")  
  15. #else  
  16. #include <sys/socket.h>  
  17. #include <netinet/in.h>  
  18. #include <arpa/in.h>  
  19. #endif  
  20.   
  21. #define DEFAULT_BUFLEN 128  
  22. #define DEFAULT_PORT 3293  
  23.   
  24. int main(int argc, char* argv[])  
  25. {  
  26.     int                 srvSock, client;  
  27.     struct sockaddr_in  addrSrv;  
  28.     int                 iStatus;  
  29.     int                 len;  
  30.     char                buff[DEFAULT_BUFLEN];  
  31.   
  32.     #ifdef _WIN32  
  33.       WORD wVersionRequested;  
  34.       WSADATA wsaData;  
  35.       wVersionRequested = MAKEWORD(1, 1);  
  36.       iStatus = WSAStartup(wVersionRequested, &wsaData);  
  37.       if (iStatus != 0) {  
  38.         return 0;  
  39.       }  
  40.       if (LOBYTE(wsaData.wVersion) != 1 ||  
  41.         HIBYTE(wsaData.wVersion) != 1) {  
  42.         WSACleanup();  
  43.         return 0;  
  44.       }  
  45.     #endif  
  46.   
  47.     srvSock = socket(AF_INET, SOCK_STREAM, 0);  
  48.   
  49.     addrSrv.sin_family = AF_INET;  
  50.     addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);  
  51.     addrSrv.sin_port = htons(DEFAULT_PORT);  
  52.   
  53.     bind(srvSock, (struct sockaddr *)&addrSrv, sizeof(addrSrv));  
  54.     listen(srvSock, 5);  
  55.   
  56.     len = sizeof(struct sockaddr);  
  57.   
  58.     while(1) {  
  59.         client = accept(srvSock, (struct sockaddr *)&addrSrv, (int *)&len);  
  60.         iStatus = recv(client, buff, DEFAULT_BUFLEN, 0);  
  61.         if (iStatus > 0)  
  62.             printf("%s\n", buff);  
  63.     }  
  64.   
  65.   
  66.   
  67.   
  68.     #ifdef _WIN32  
  69.       closesocket(client);  
  70.       closesocket(srvSock);  
  71.       WSACleanup();  
  72.     #endif  
  73.   
  74.     return 0;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值