最简单的Windows套接字(Socket)例子(源码,实例)

时间:2022-05-07 03:18:49

最简单的Windows套接字(Socket)例子(源码,实例) 佟强(http://blog.csdn.net/microtong) 2008年11月21日

    Server.exe PortNumber,例如Server 8000

    Client.exe IPAddress PortNumber,例如Client 127.0.0.1 8000

    然后在客户端的命令行输入字符串并回车,客户端将会把消息发送到服务器,服务器再把消息传回客户端。

服务器端,Server.cpp

  1. //Server.cpp
  2. #include <winsock2.h>
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #pragma comment(lib,"ws2_32.lib")
  6. int main(int argc, char* argv[]){
  7.     //判断是否输入了端口号
  8.     if(argc!=2){
  9.         printf("Usage: %s PortNumber/n",argv[0]);
  10.         exit(-1);
  11.     }
  12.     //把端口号转化成整数
  13.     short port;
  14.     if((port = atoi(argv[1]))==0){
  15.         printf("端口号有误!");
  16.         exit(-1);
  17.     }
  18.     WSADATA wsa;
  19.     //初始化套接字DLL
  20.     if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){
  21.         printf("套接字初始化失败!");
  22.         exit(-1);
  23.     }
  24.     //创建套接字
  25.     SOCKET serverSocket;
  26.     if((serverSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){
  27.         printf("创建套接字失败!");
  28.         exit(-1);
  29.     }
  30.     struct sockaddr_in serverAddress;
  31.     memset(&serverAddress,0,sizeof(sockaddr_in));
  32.     serverAddress.sin_family=AF_INET;
  33.     serverAddress.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
  34.     serverAddress.sin_port = htons(port);
  35.     //绑定
  36.     if(bind(serverSocket,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){
  37.         printf("套接字绑定到端口失败!端口: %d/n",port);
  38.         exit(-1);
  39.     }
  40.     //进入侦听状态
  41.     if(listen(serverSocket,SOMAXCONN)==SOCKET_ERROR){
  42.         printf("侦听失败!");
  43.         exit(-1);
  44.     }
  45.     printf("Server %d is listening....../n",port);
  46.     SOCKET clientSocket;//用来和客户端通信的套接字
  47.     struct sockaddr_in clientAddress;//用来和客户端通信的套接字地址
  48.     memset(&clientAddress,0,sizeof(clientAddress));
  49.     int addrlen = sizeof(clientAddress);
  50.     //接受连接
  51.     if((clientSocket=accept(serverSocket,(sockaddr*)&clientAddress,&addrlen))==INVALID_SOCKET){
  52.         printf("接受客户端连接失败!");
  53.         exit(-1);
  54.     }
  55.     printf("Accept connection from %s/n",inet_ntoa(clientAddress.sin_addr));
  56.     char buf[4096];
  57.     while(1){
  58.         //接收数据
  59.         int bytes;
  60.         if((bytes=recv(clientSocket,buf,sizeof(buf),0))==SOCKET_ERROR){
  61.             printf("接收数据失败!/n");
  62.             exit(-1);
  63.         }
  64.         buf[bytes]='/0';
  65.         printf("Message from %s: %s/n",inet_ntoa(clientAddress.sin_addr),buf);
  66.         if(send(clientSocket,buf,bytes,0)==SOCKET_ERROR){
  67.             printf("发送数据失败!");
  68.             exit(-1);
  69.         }
  70.     }
  71.     //清理套接字占用的资源
  72.     WSACleanup();
  73.     return 0;
  74. }

客户端,Client.cpp

 

  1. //Client.cpp
  2. #include <winsock2.h>
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #pragma comment(lib,"ws2_32.lib")
  6. int main(int argc, char* argv[]){
  7.     //判断是否输入了IP地址和端口号
  8.     if(argc!=3){
  9.         printf("Usage: %s IPAddress PortNumber/n",argv[0]);
  10.         exit(-1);
  11.     }
  12.     //把字符串的IP地址转化为u_long
  13.     unsigned long ip;
  14.     if((ip=inet_addr(argv[1]))==INADDR_NONE){
  15.         printf("不合法的IP地址:%s",argv[1]);
  16.         exit(-1);
  17.     }
  18.     //把端口号转化成整数
  19.     short port;
  20.     if((port = atoi(argv[2]))==0){
  21.         printf("端口号有误!");
  22.         exit(-1);
  23.     }
  24.     printf("Connecting to %s:%d....../n",inet_ntoa(*(in_addr*)&ip),port);
  25.     WSADATA wsa;
  26.     //初始化套接字DLL
  27.     if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){
  28.         printf("套接字初始化失败!");
  29.         exit(-1);
  30.     }
  31.     //创建套接字
  32.     SOCKET sock;
  33.     if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){
  34.         printf("创建套接字失败!");
  35.         exit(-1);
  36.     }
  37.     struct sockaddr_in serverAddress;
  38.     memset(&serverAddress,0,sizeof(sockaddr_in));
  39.     serverAddress.sin_family=AF_INET;
  40.     serverAddress.sin_addr.S_un.S_addr = ip;
  41.     serverAddress.sin_port = htons(port);
  42.     //建立和服务器的连接
  43.     if(connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){
  44.         printf("建立连接失败!");
  45.         exit(-1);
  46.     }
  47.     char buf[4096];
  48.     while(1){
  49.         printf(">");
  50.         //从控制台读取一行数据
  51.         gets(buf);
  52.         //发送给服务器
  53.        if(send(sock,buf,strlen(buf),0)==SOCKET_ERROR){
  54.             printf("发送数据失败!");
  55.             exit(-1);
  56.         }
  57.        int bytes;
  58.         if((bytes=recv(sock,buf,sizeof(buf),0))==SOCKET_ERROR){
  59.             printf("接收数据失败!/n");
  60.             exit(-1);
  61.         }
  62.         buf[bytes]='/0';
  63.         printf("Message from %s: %s/n",inet_ntoa(serverAddress.sin_addr),buf);
  64.     }
  65.     //清理套接字占用的资源
  66.     WSACleanup();
  67.     return 0;
  68. }