基于Windows的服务器和客户端

server.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <winsock2.h>
 4 void ErrorHandling(char* message);
 5 
 6 int main(int argc, char* argv[])
 7 {
 8     WSADATA wsaData;
 9     SOCKET hServSock, hClntSock;
10     SOCKADDR_IN servAddr, clntAddr;
11 
12     int szClntAddr;
13     char message[] = "Hello World!";
14     if (argc != 2)
15     {
16         printf("Usage : %s ,port>\n", argv[0]);
17         exit(1);
18     }
19 
20     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
21         ErrorHandling("WSAStartup() error!");
22 
23     hServSock = socket(PF_INET, SOCK_STREAM, 0);
24     if (hServSock == INVALID_SOCKET)
25         ErrorHandling("socket() error");
26 
27     memset(&servAddr, 0, sizeof(servAddr));
28     servAddr.sin_family = AF_INET;
29     servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
30     servAddr.sin_port = htons(atoi(argv[1]));
31 
32     if (bind(hServSock, (SOCKADDR *)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
33         ErrorHandling("bind() error");
34 
35     if (listen(hServSock, 5) == SOCKET_ERROR)
36         ErrorHandling("listen() error");
37 
38     szClntAddr = sizeof(clntAddr);
39     hClntSock = accept(hServSock, (SOCKADDR *)&clntAddr, &szClntAddr);
40     if (hClntSock == INVALID_SOCKET)
41         ErrorHandling("accept() error");
42 
43     send(hClntSock, message, sizeof(message), 0);
44     closesocket(hClntSock);
45     closesocket(hClntSock);
46     WSACleanup();
47     return 0;
48 }
49 
50 void ErrorHandling(char* message)
51 {
52     fputs(message, stderr);
53     fputc('\n', stderr);
54     exit(1);
55 }

client.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <WinSock2.h>
 4 void ErrorHandling(char* message);
 5 
 6 int main(int argc, char* argv[])
 7 {
 8     WSADATA wsaData;
 9     SOCKET hSocket;
10     SOCKADDR_IN servAddr;
11 
12     char message[30];
13     int strLen;
14     if (argc != 3)
15     {
16         printf("Usage : %s <IP> <port>\n", argv[0]);
17         exit(1);
18     }
19 
20     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
21         ErrorHandling("WSAStartup() error!");
22 
23     hSocket = socket(PF_INET, SOCK_STREAM, 0);
24     if (hSocket == INVALID_SOCKET)
25         ErrorHandling("socket() error");
26 
27     memset(&servAddr, 0, sizeof(servAddr));
28     servAddr.sin_family = AF_INET;
29     servAddr.sin_addr.s_addr = inet_addr(argv[1]);
30     servAddr.sin_port = htons(atoi(argv[2]));
31 
32     if (connect(hSocket, (SOCKADDR *)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
33         ErrorHandling("connect() error!");
34 
35     strLen = recv(hSocket, message, sizeof(message) - 1, 0);
36     if (strLen == -1)
37         ErrorHandling("read() error!");
38     printf("Message from server: %s \n", message);
39 
40     closesocket(hSocket);
41     WSACleanup();
42     return 0;
43 }
44 
45 void ErrorHandling(char* message)
46 {
47     fputs(message, stderr);
48     fputc('\n', stderr);
49     exit(1);
50 }

注意,需要在工程属性中设置:链接器-输入-附加依赖项中添加:ws2_32.lib。

posted @ 2019-10-22 22:22  wisdomroc  阅读(332)  评论(0编辑  收藏  举报