zoukankan      html  css  js  c++  java
  • winsock 编程(简单客户&服务端通信实现)

    winsock 编程(简单客户&服务端通信实现)

    双向通信:Client send message to Server, and if  Server receive the message, return a string "你好,客户端!我是服务器。"

    修改说明:If you want achieve the function that Server can also send message to Client, you only need to change the 70,71 and 72 line. That is, you need to add the scanf code and transform it to const char*, then send it to Client!

    看到此处,相信你已经对winsock编程有了一定的了解,下面给出 简单的实现。

    其中用到的  各个函数详细说明参考

    服务端:初始化winsock-->建立socket-->bind(端口&IP&协议)-->listen-->accept*(不断监听&接受连接)

     1 /*注意头文件顺序*/
     2 #include <winsock2.h>
     3 #include <stdio.h>
     4 #include <stdlib.h>
     5 #pragma comment(lib, "ws2_32.lib")      //引入动态链接库
     6 
     7 int main()
     8 {
     9     WORD ws_version = MAKEWORD(2, 2);   //指定Winsock version
    10     WSADATA wsaData;                    //WSA 函数的参数
    11 
    12     /*初始化winsock*/
    13     WSAStartup(ws_version, &wsaData);
    14 
    15     /*socket*/
    16     SOCKET s_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    17 
    18     SOCKADDR_IN addr_server;
    19     addr_server.sin_family = AF_INET;   //协议
    20     addr_server.sin_port = htons(5050); //端口
    21     addr_server.sin_addr.s_addr = htonl(INADDR_ANY);    //IP:任意IP
    22 
    23     /*bind*/
    24     int bind_status;
    25     bind_status = bind(s_server, (SOCKADDR*)&addr_server, sizeof(SOCKADDR));
    26     if(bind_status == SOCKET_ERROR)
    27     {
    28         printf("bind error : fail to bind! 
    "); 
    29     }
    30     else
    31     {
    32         printf("bind successfully!
    ");
    33     }
    34 
    35     /*listen*/
    36     listen(s_server, 5);//max=5
    37     printf("listening ... 
    ");
    38 
    39     SOCKADDR_IN addr_client;            //存储client地址信息
    40     int len = sizeof(SOCKADDR);
    41     int count = 0;                        //统计客户数目
    42     SOCKET s_client;                    //连接的socket
    43 
    44     char buf[1000];
    45     while(true)
    46     {
    47         printf("
    等待连接...
    ");
    48         /*accept*/
    49         s_client = accept(s_server, (SOCKADDR*)&addr_client, &len);
    50         if(s_client == INVALID_SOCKET)
    51         {
    52             printf("Accept error : fail to accept client! ");
    53         }
    54         else//连接成功
    55         {
    56             count++;
    57             printf("
    Accept successfully!
    ");
    58 
    59             printf(" 编号:%d 
    ", count);
    60             printf(" Port:%d
    ", ntohs(addr_client.sin_port));
    61             printf(" IP:%s
    ", inet_ntoa(addr_client.sin_addr));//inet_ntoa(SOCKADDR.in_addr)网络地址转换为IP
    62         
    63             int recv_status = recv(s_client, buf, 100, 0);
    64             if(recv_status > 0)
    65             {
    66                 printf("收到:");
    67                 buf[recv_status] = 0x00;//在字符串结尾填上 结束符 0x00 == /0 参考回答
    68                 printf(buf);
    69             }
    70             const char *sendData = "你好!客户端!我是服务器";
    71             send(s_client, sendData, strlen(sendData), 0);
    72             closesocket(s_client);
    73         }
    74     }
    75 
    76     closesocket(s_server);      //关闭socket
    77     WSACleanup();
    78 
    79     return 0;
    80 }

    客户端:初始化winsock-->建立socket-->connect-->通信(send & recv)

     1 #include <winsock2.h>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include <windows.h>
     5 #include <iostream>
     6 #pragma comment(lib, "ws2_32.lib")      //引入动态链接库
     7 #define SERVER_IP "192.168.43.100"        //客户端IP
     8 using namespace std;
     9 
    10 int main()
    11 {
    12     WORD ws_version = MAKEWORD(2, 2);   //指定Winsock version
    13     WSADATA wsaData;                    //WSA 函数的参数
    14 
    15     /*初始化winsock*/
    16     WSAStartup(ws_version, &wsaData);
    17 
    18     while(true)
    19     {
    20         /*socket*/
    21         SOCKET s_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    22 
    23         SOCKADDR_IN addr_server;
    24         addr_server.sin_family = AF_INET;   //协议
    25         addr_server.sin_port = htons(5050); //端口
    26         addr_server.sin_addr.s_addr = inet_addr(SERVER_IP);
    27 
    28         char buf[100];
    29         int send_status, recv_status;
    30 
    31         /*Connect*/
    32         int cnct_status = connect(s_client, (SOCKADDR*)&addr_server, sizeof(SOCKADDR));
    33         if(cnct_status == 0)//连接成功
    34         {
    35                 printf("
    Connecting... done
    ");
    36 
    37                 scanf("%s", buf);
    38                 //向服务端发送消息
    39                 send_status = send(s_client, buf, 10, 0);
    40                 if(send_status == SOCKET_ERROR)//发送失败
    41                 {
    42                     printf("send error!
    ");
    43                 }
    44                 else
    45                 {
    46                     printf("发送:%s
    ", buf);
    47                     //接受服务端消息
    48                     recv_status = recv(s_client, buf, 100, 0);
    49                     buf[recv_status] = 0x00;//在字符串结尾填上 结束符 0x00 == /0 参考回答
    50 printf("收到:%s ", buf); 51 } 52 } 53 else 54 { 55 printf("Test:fail to connect server! "); 56 } 57 closesocket(s_client); 58 } 59 60 WSACleanup(); 61 62 return 0; 63 }
  • 相关阅读:
    全栈必备Linux 基础
    Linux 的 Socket IO 模型
    Vim
    Linux 下使用 Sar 简介
    提高效率,推荐 5 款命令行工具
    Vim小技巧
    剑指Offer 矩形覆盖
    剑指Offer 变态跳台阶
    剑指Offer 跳台阶
    2016 网易校招内推C/C++第二场8.6
  • 原文地址:https://www.cnblogs.com/yocichen/p/9965710.html
Copyright © 2011-2022 走看看