Visual Studio 2022 中可以运行的 UDP 客户端和服务器的示例代码。这个示例使用 C++ 和 Windows Socket API 来实现 UDP 通信。
服务器端代码(udp_server.cpp
)
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>#pragma comment(lib, "Ws2_32.lib")#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512int main() {WSADATA wsaData;int iResult;// 初始化 WinsockiResult = WSAStartup(MAKEWORD(2, 2), &wsaData);if (iResult != 0) {std::cout << "WSAStartup failed: " << iResult << std::endl;return 1;}struct addrinfo *result = NULL, *ptr = NULL, hints;ZeroMemory(&hints, sizeof(hints));hints.ai_family = AF_INET;hints.ai_socktype = SOCK_DGRAM;hints.ai_protocol = IPPROTO_UDP;hints.ai_flags = AI_PASSIVE;// 解析本地地址和端口iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);if (iResult != 0) {std::cout << "getaddrinfo failed: " << iResult << std::endl;WSACleanup();return 1;}SOCKET ListenSocket = INVALID_SOCKET;// 创建监听套接字ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);if (ListenSocket == INVALID_SOCKET) {std::cout << "socket failed: " << WSAGetLastError() << std::endl;freeaddrinfo(result);WSACleanup();return 1;}// 绑定套接字iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);if (iResult == SOCKET_ERROR) {std::cout << "bind failed: " << WSAGetLastError() << std::endl;freeaddrinfo(result);closesocket(ListenSocket);WSACleanup();return 1;}freeaddrinfo(result);char recvbuf[DEFAULT_BUFLEN];int recvbuflen = DEFAULT_BUFLEN;struct sockaddr_in clientAddr;int clientAddrLen = sizeof(clientAddr);std::cout << "UDP Server is listening on port " << DEFAULT_PORT << std::endl;while (true) {// 接收数据iResult = recvfrom(ListenSocket, recvbuf, recvbuflen, 0, (struct sockaddr*)&clientAddr, &clientAddrLen);if (iResult > 0) {recvbuf[iResult] = '\0';std::cout << "Received from client: " << recvbuf << std::endl;// 回显数据给客户端iResult = sendto(ListenSocket, recvbuf, iResult, 0, (struct sockaddr*)&clientAddr, clientAddrLen);if (iResult == SOCKET_ERROR) {std::cout << "sendto failed: " << WSAGetLastError() << std::endl;}}else if (iResult == 0) {std::cout << "Connection closing..." << std::endl;}else {std::cout << "recvfrom failed: " << WSAGetLastError() << std::endl;}}// 关闭套接字closesocket(ListenSocket);WSACleanup();return 0;
}
客户端代码(udp_client.cpp
)
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>#pragma comment(lib, "Ws2_32.lib")#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
#define DEFAULT_SERVER_IP "127.0.0.1"int main() {WSADATA wsaData;int iResult;// 初始化 WinsockiResult = WSAStartup(MAKEWORD(2, 2), &wsaData);if (iResult != 0) {std::cout << "WSAStartup failed: " << iResult << std::endl;return 1;}struct addrinfo *result = NULL, *ptr = NULL, hints;ZeroMemory(&hints, sizeof(hints));hints.ai_family = AF_INET;hints.ai_socktype = SOCK_DGRAM;hints.ai_protocol = IPPROTO_UDP;// 解析服务器地址和端口iResult = getaddrinfo(DEFAULT_SERVER_IP, DEFAULT_PORT, &hints, &result);if (iResult != 0) {std::cout << "getaddrinfo failed: " << iResult << std::endl;WSACleanup();return 1;}SOCKET ConnectSocket = INVALID_SOCKET;// 创建套接字ConnectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);if (ConnectSocket == INVALID_SOCKET) {std::cout << "socket failed: " << WSAGetLastError() << std::endl;freeaddrinfo(result);WSACleanup();return 1;}char sendbuf[DEFAULT_BUFLEN];char recvbuf[DEFAULT_BUFLEN];int recvbuflen = DEFAULT_BUFLEN;std::cout << "Enter a message to send to the server: ";std::cin.getline(sendbuf, DEFAULT_BUFLEN);// 发送数据到服务器iResult = sendto(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0, result->ai_addr, (int)result->ai_addrlen);if (iResult == SOCKET_ERROR) {std::cout << "sendto failed: " << WSAGetLastError() << std::endl;freeaddrinfo(result);closesocket(ConnectSocket);WSACleanup();return 1;}struct sockaddr_in serverAddr;int serverAddrLen = sizeof(serverAddr);// 接收服务器的响应iResult = recvfrom(ConnectSocket, recvbuf, recvbuflen, 0, (struct sockaddr*)&serverAddr, &serverAddrLen);if (iResult > 0) {recvbuf[iResult] = '\0';std::cout << "Received from server: " << recvbuf << std::endl;}else if (iResult == 0) {std::cout << "Connection closed by server." << std::endl;}else {std::cout << "recvfrom failed: " << WSAGetLastError() << std::endl;}// 清理资源freeaddrinfo(result);closesocket(ConnectSocket);WSACleanup();return 0;
}
代码解释
-
服务器端:
- 初始化 Winsock:调用
WSAStartup
函数初始化 Windows Sockets DLL。 - 解析地址和端口:使用
getaddrinfo
函数解析本地地址和端口。 - 创建和绑定套接字:使用
socket
函数创建 UDP 套接字,并使用bind
函数将套接字绑定到指定的地址和端口。 - 接收和发送数据:使用
recvfrom
函数接收客户端发送的数据,并使用sendto
函数将数据回显给客户端。
- 初始化 Winsock:调用
-
客户端:
- 初始化 Winsock:同样调用
WSAStartup
函数初始化 Windows Sockets DLL。 - 解析服务器地址和端口:使用
getaddrinfo
函数解析服务器的地址和端口。 - 创建套接字:使用
socket
函数创建 UDP 套接字。 - 发送和接收数据:使用
sendto
函数向服务器发送数据,并使用recvfrom
函数接收服务器的响应。
- 初始化 Winsock:同样调用
在 VS2022 中运行步骤
- 打开 Visual Studio 2022,创建一个新的控制台应用程序项目。
- 将上述服务器端代码复制到一个
.cpp
文件中,例如udp_server.cpp
;将客户端代码复制到另一个.cpp
文件中,例如udp_client.cpp
。 - 分别编译和运行这两个程序。先运行服务器端程序,然后运行客户端程序,在客户端输入要发送的消息,客户端将消息发送给服务器,服务器接收到消息后将其回显给客户端。