写一段使用WinSock网络编程接口规范及VC++编程语言,基于TCP协议的流式套接字,采用多线程、客户/服务器模式,完成一个控制台的 “石头、剪刀、布” 人机对战游戏程序,其中服务器端代表机器选手
时间: 2023-06-26 10:09:49 浏览: 152
很高兴能够为您提供这个问题的解答。以下是基于TCP协议的流式套接字,采用多线程、客户/服务器模式,完成一个控制台的 “石头、剪刀、布” 人机对战游戏程序的示例代码:
服务器端:
```c++
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
#define PORT 8888 // 端口号
int main()
{
// 初始化 Winsock 库
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != NO_ERROR) {
cerr << "WSAStartup failed: " << result << endl;
return 1;
}
// 创建 socket
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
cerr << "socket failed with error: " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 绑定 socket
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = INADDR_ANY;
result = bind(serverSocket, (sockaddr*)&serverAddr, sizeof(serverAddr));
if (result == SOCKET_ERROR) {
cerr << "bind failed with error: " << WSAGetLastError() << endl;
closesocket(serverSocket);
WSACleanup();
return 1;
}
// 监听 socket
result = listen(serverSocket, SOMAXCONN);
if (result == SOCKET_ERROR) {
cerr << "listen failed with error: " << WSAGetLastError() << endl;
closesocket(serverSocket);
WSACleanup();
return 1;
}
cout << "Server is running..." << endl;
while (true) {
// 接受客户端连接
SOCKET clientSocket = accept(serverSocket, NULL, NULL);
if (clientSocket == INVALID_SOCKET) {
cerr << "accept failed with error: " << WSAGetLastError() << endl;
closesocket(serverSocket);
WSACleanup();
return 1;
}
cout << "Client connected." << endl;
// 处理客户端请求
while (true) {
char buffer[1024] = { 0 };
result = recv(clientSocket, buffer, sizeof(buffer), 0);
if (result > 0) {
// 解析客户端请求
int choice = atoi(buffer);
srand(time(NULL));
int computerChoice = rand() % 3;
// 发送响应给客户端
char response[1024] = { 0 };
if (choice == computerChoice) {
sprintf(response, "Tie!");
}
else if ((choice == 0 && computerChoice == 1) ||
(choice == 1 && computerChoice == 2) ||
(choice == 2 && computerChoice == 0)) {
sprintf(response, "You win!");
}
else {
sprintf(response, "Computer wins!");
}
send(clientSocket, response, strlen(response), 0);
}
else if (result == 0) {
cout << "Client disconnected." << endl;
closesocket(clientSocket);
break;
}
else {
cerr << "recv failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
}
// 关闭 socket
closesocket(serverSocket);
// 清理 Winsock 库
WSACleanup();
return 0;
}
```
客户端:
```c++
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
#define SERVER_ADDRESS "127.0.0.1" // 服务器地址
#define PORT 8888 // 端口号
int main()
{
// 初始化 Winsock 库
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != NO_ERROR) {
cerr << "WSAStartup failed: " << result << endl;
return 1;
}
// 创建 socket
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
cerr << "socket failed with error: " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 连接服务器
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
inet_pton(AF_INET, SERVER_ADDRESS, &serverAddr.sin_addr);
result = connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr));
if (result == SOCKET_ERROR) {
cerr << "connect failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return 1;
}
while (true) {
// 获取用户输入
int choice = 0;
cout << "Please enter your choice (0 for rock, 1 for scissors, 2 for paper): ";
cin >> choice;
// 发送请求给服务器
char request[1024] = { 0 };
sprintf(request, "%d", choice);
send(clientSocket, request, strlen(request), 0);
// 接受服务器响应
char response[1024] = { 0 };
result = recv(clientSocket, response, sizeof(response), 0);
if (result > 0) {
cout << "Result: " << response << endl;
}
else if (result == 0) {
cout << "Server disconnected." << endl;
break;
}
else {
cerr << "recv failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
// 关闭 socket
closesocket(clientSocket);
// 清理 Winsock 库
WSACleanup();
return 0;
}
```
在 Visual Studio 中,您可以使用 Visual C++ 集成开发环境来编译和运行这个程序。在运行之前,您需要在项目属性中添加 `ws2_32.lib` 作为附加依赖项。如果您使用的是其他编译器或开发环境,请相应地进行配置。
阅读全文