c++下基于windows socket的服务器客户端程序(基于UDP协议)

前天写了一个基于tcp协议的服务器客户端程序,今天写了一个基于UDP协议的,代码如下:

服务器端:

 1 #include <iostream>
 2 #include "Ws2tcpip.h"
 3 #include "Winsock2.h"
 4 
 5 using std::cin;
 6 using std::cout;
 7 using std::cerr;
 8 using std::endl;
 9 
10 #pragma comment(lib, "ws2_32.lib")
11 
12 int SOCKADDR_IN_SIZE = sizeof(SOCKADDR_IN);
13 const u_short DEFAULT_PORT = 4000;
14 const size_t MSG_BUF_SIZE = 512;
15 const size_t IP_BUF_SIZE = 256;
16 
17 int main() {
18     WSADATA wsa_data;
19     SOCKET sock_serv = INVALID_SOCKET;
20     SOCKADDR_IN addr_serv, addr_clt;
21     char ip_buf[IP_BUF_SIZE];
22     char msg_buf[MSG_BUF_SIZE];
23     int ret_val = 0;
24     //
25     ret_val = WSAStartup(MAKEWORD(2, 2), &wsa_data);
26     if (ret_val != 0) {
27         cerr << "WSAStartup() function failed with error: " << WSAGetLastError() << "\n";
28         system("pause");
29         return 1;
30     }
31     //
32     //SecureZeroMemory(&addr_serv, SOCKADDR_IN_SIZE);
33     addr_serv.sin_family = AF_INET;
34     addr_serv.sin_port = htons(DEFAULT_PORT);
35     addr_serv.sin_addr.S_un.S_addr = ADDR_ANY;
36     //
37     sock_serv = socket(addr_serv.sin_family, SOCK_DGRAM, IPPROTO_UDP);
38     if (sock_serv == INVALID_SOCKET) {
39         cerr << "socket() function failed with error: " << WSAGetLastError() << "\n";
40         WSACleanup();
41         system("pause");
42         return 1;
43     }
44     //
45     ret_val = bind(sock_serv, (SOCKADDR*)&addr_serv, SOCKADDR_IN_SIZE);
46     if (ret_val != 0) {
47         cerr << "bind() function failed with error: " << WSAGetLastError() << "\n";
48         system("pause");
49         return 1;
50     }
51     cout << "A UDP server has started successfully..." << endl;
52     //
53     while (true) {
54         SecureZeroMemory(msg_buf, MSG_BUF_SIZE);
55         ret_val = recvfrom(sock_serv, msg_buf, MSG_BUF_SIZE, 0, (SOCKADDR*)&addr_clt, &SOCKADDR_IN_SIZE);
56         if (ret_val > 0) {
57             inet_ntop(addr_clt.sin_family, &addr_clt, ip_buf, IP_BUF_SIZE);
58             cout << "message from client " << ip_buf << ": " << msg_buf << endl;
59             cout << "enter the message to response: ";
60             cin.getline(msg_buf, MSG_BUF_SIZE);
61             sendto(sock_serv, msg_buf, strlen(msg_buf), 0, (SOCKADDR*)&addr_clt, SOCKADDR_IN_SIZE);
62         }
63         else if (ret_val == 0) {
64             cout << "connection is closed..." << endl;
65         }
66         else {
67             cerr << "recvfrom() function failed with error: " << WSAGetLastError() << "\n";
68             closesocket(sock_serv);
69             WSACleanup();
70             system("pause");
71             return 1;
72         }
73     }
74     ret_val = shutdown(sock_serv, SD_BOTH);
75     if (ret_val == SOCKET_ERROR) {
76         cerr << "shutdown() function failed with error: " << WSAGetLastError() << "\n";
77         closesocket(sock_serv);
78         WSACleanup();
79         system("pause");
80         return 1;
81     }
82     closesocket(sock_serv);
83     WSACleanup();
84     cout << "server shutdown..." << endl;
85     system("pause");
86     return 0;
87 }

客户端:

 1 #include <Winsock2.h>
 2 #include <WS2tcpip.h>
 3 #include <iostream>
 4 
 5 using std::cout;
 6 using std::cin;
 7 using std::cerr;
 8 using std::endl;
 9 
10 #pragma comment(lib, "ws2_32.lib")
11 
12 const int MSG_BUF_SIZE = 512;
13 int SOCKADDR_SIZE = sizeof(SOCKADDR);
14 const int SERV_PORT = 4000;
15 const char *SERV_IP = "127.0.0.1";
16 
17 int main() {
18     WSADATA wsa_data;
19     SOCKET sock_clt = INVALID_SOCKET;
20     SOCKADDR_IN addr_serv;
21     char msg_buf[MSG_BUF_SIZE];
22     int ret_val = 0;
23     //
24     ret_val = WSAStartup(MAKEWORD(2, 2), &wsa_data);
25     if (ret_val != 0) {
26         cerr << "WSAStartup() function failed with error: " << WSAGetLastError() << "\n";
27         system("pause");
28         return 1;
29     }
30     //
31     sock_clt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
32     if (sock_clt == INVALID_SOCKET) {
33         cerr << "socket() function failed with error: " << WSAGetLastError() << "\n";
34         WSACleanup();
35         system("pause");
36         return 1;
37     }
38     //
39     addr_serv.sin_family = AF_INET;
40     addr_serv.sin_port = htons(SERV_PORT);
41     ret_val = InetPton(addr_serv.sin_family, SERV_IP, &addr_serv.sin_addr);
42     if (ret_val != 1) {
43         cerr << "inet_pton() function failed with error: " << WSAGetLastError() << "\n";
44         closesocket(sock_clt);
45         WSACleanup();
46         system("pause");
47         return 1;
48     }
49     //
50     cout << "A UDP client has already started successfully..." << endl;
51     //
52     //SecureZeroMemory(msg_buf, MSG_BUF_SIZE);
53     while (true) {
54         cout << "enter the message you want to send: ";
55         cin.getline(msg_buf, MSG_BUF_SIZE);
56         ret_val = sendto(sock_clt, msg_buf, strlen(msg_buf), 0, (SOCKADDR*)&addr_serv, SOCKADDR_SIZE);
57         if (ret_val == SOCKET_ERROR) {
58             cerr << "sendto() function failed with error: " << WSAGetLastError() << "\n";
59             closesocket(sock_clt);
60             WSACleanup();
61             system("pause");
62             return 1;
63         }
64         SecureZeroMemory(msg_buf, MSG_BUF_SIZE);
65         ret_val = recvfrom(sock_clt, msg_buf, MSG_BUF_SIZE, 0, (SOCKADDR*)&addr_serv, &SOCKADDR_SIZE);
66         if (ret_val > 0) {
67             cout << "feedback from the server: " << msg_buf << endl;
68         }
69         else if (ret_val == 0) {
70             cout << "connection is closed..." << endl;
71         }
72         else {
73             cerr << "recvfrom() function failed with error: " << WSAGetLastError() << "\n";
74             closesocket(sock_clt);
75             WSACleanup();
76             system("pause");
77             return 1;
78         }
79     }
80     ret_val = shutdown(sock_clt, SD_BOTH);
81     if (ret_val == SOCKET_ERROR) {
82         cerr << "shutdown() function failed with error: " << WSAGetLastError() << "\n";
83         closesocket(sock_clt);
84         WSACleanup();
85         system("pause");
86         return 1;
87     }
88     closesocket(sock_clt);
89     WSACleanup();
90     system("pause");
91     return 0;
92 }

 

你可能感兴趣的:(c++下基于windows socket的服务器客户端程序(基于UDP协议))