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

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

看到此处,相信你已经对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")      //引入动态链接库:如果这里报错,请在Settings -> Compilier -> Linker settings ->add "libws2_32.a" 库(不带引号)
 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 == 0)
27     {
28         printf("bind successfully!\n");
29     }
30     else
31     {
32         printf("bind error : fail to bind! \n");
33     }
34 
35     /*listen*/
36     listen(s_server, 5);//max=5
37     printf("listening ... \n");
38 
39     SOCKADDR_IN addr_client;            //存储client地址信息
40     int len = sizeof(SOCKADDR);
41     int send_status;                    //send 状态
42     int count = 0;                        //统计客户数目
43 
44     SOCKET s_client;                //连接的socket
45     char buf[30];
46     while(1)
47     {
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("\nAccept successfully!\n");
60             printf(" 客户 %d \n", count);
61             printf(" Port:%d\n", ntohs(addr_client.sin_port));
62             printf(" IP:%s\n", inet_ntoa(addr_client.sin_addr));//inet_ntoa(SOCKADDR.in_addr)网络地址转换为IP
63 
64             send_status = send(s_client, "Hello, I am Server!", 40, 0);
65             if(send_status == SOCKET_ERROR)
66             {
67                 printf("Send failed!");
68             }
69         }
70         closesocket(s_client);
71     }
72 
73     closesocket(s_server);      //关闭socket
74     WSACleanup();
75 
76     return 0;
77 }

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

 1 #include <winsock2.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <windows.h>
 5 #pragma comment(lib, "ws2_32.lib")      //引入动态链接库
 6 #define SERVER_IP "192.168.31.102"        //客户端IP
 7 
 8 int main()
 9 {
10     WORD ws_version = MAKEWORD(2, 2);   //指定Winsock version
11     WSADATA wsaData;                    //WSA 函数的参数
12 
13     /*初始化winsock*/
14     WSAStartup(ws_version, &wsaData);
15 
16     /*socket*/
17     SOCKET s_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
18 
19     SOCKADDR_IN addr_server;
20     addr_server.sin_family = AF_INET;   //协议
21     addr_server.sin_port = htons(5050); //端口
22     addr_server.sin_addr.s_addr = inet_addr(SERVER_IP);    //IP:任意IP
23 
24     char buf[30];
25 
26     /*Connect*/
27     Sleep(2000);
28     int cnct_status = connect(s_client, (SOCKADDR*)&addr_server, sizeof(SOCKADDR));
29     if(cnct_status == 0)                //连接成功
30     {
31         printf("Connecting... done \n");
32     
33         int recv_status = recv(s_client, buf, 30, 0);//接受服务端消息
34         if(recv_status == SOCKET_ERROR)        
35         {
36                 printf("Recv : fail to recv! \n");
37         }
38         else
39         {
40             Sleep(1000);
41             printf("Recving... done \n");
42             printf("Message : %s \n", buf);
43             printf("Access done!\n\n");
44         }
45     }
46     else
47     {
48         printf("Connect : fail to connect server! \n");
49     }
50 
51     closesocket(s_client);
52     WSACleanup();
53 
54     return 0;
55 }

猜你喜欢

转载自www.cnblogs.com/yocichen/p/9965710.html
今日推荐