C++实现Socket 实现五子棋小游戏
时间: 2023-07-11 12:03:11 浏览: 114
用C++实现五子棋小游戏
下面是一个简单的 C++ 实现的 Socket 五子棋小游戏:
服务端代码:
```cpp
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 1234
#define BACKLOG 5
#define MAXDATASIZE 100
using namespace std;
int main()
{
int sockfd, client_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
char buf[MAXDATASIZE];
int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
cout << "Create socket error!" << endl;
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
cout << "Bind error!" << endl;
exit(1);
}
if (listen(sockfd, BACKLOG) == -1)
{
cout << "Listen error!" << endl;
exit(1);
}
cout << "Server started!" << endl;
sin_size = sizeof(struct sockaddr_in);
if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, (socklen_t *)&sin_size)) == -1)
{
cout << "Accept error!" << endl;
exit(1);
}
cout << "Client connected!" << endl;
send(client_fd, "Welcome to the game!", MAXDATASIZE, 0);
while (1)
{
if ((recv(client_fd, buf, MAXDATASIZE, 0)) == -1)
{
cout << "Receive error!" << endl;
exit(1);
}
cout << "Received: " << buf << endl;
if (strcmp(buf, "exit") == 0)
{
break;
}
send(client_fd, "OK", MAXDATASIZE, 0);
}
close(client_fd);
close(sockfd);
cout << "Server stopped!" << endl;
return 0;
}
```
客户端代码:
```cpp
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 1234
#define MAXDATASIZE 100
using namespace std;
int main()
{
int sockfd;
char buf[MAXDATASIZE];
struct sockaddr_in server_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
cout << "Create socket error!" << endl;
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(server_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
cout << "Connect error!" << endl;
exit(1);
}
cout << "Connected to the server!" << endl;
if ((recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
cout << "Receive error!" << endl;
exit(1);
}
cout << "Received: " << buf << endl;
while (1)
{
cout << "Please enter your move: " << endl;
cin >> buf;
if ((send(sockfd, buf, strlen(buf), 0)) == -1)
{
cout << "Send error!" << endl;
exit(1);
}
if (strcmp(buf, "exit") == 0)
{
break;
}
if ((recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
cout << "Receive error!" << endl;
exit(1);
}
cout << "Received: " << buf << endl;
}
close(sockfd);
cout << "Disconnected from the server!" << endl;
return 0;
}
```
这个代码实现了一个简单的基于 Socket 的五子棋小游戏,可以在本地运行。服务端代码监听一个端口并等待客户端连接,一旦连接成功,就发送欢迎消息并开始循环接收来自客户端的消息,如果接收到 "exit" 消息就退出循环并关闭连接。客户端代码连接到服务端并接收欢迎消息,然后开始循环输入并发送消息给服务端,如果输入 "exit" 消息就退出循环并关闭连接。
阅读全文