在Linux下用c语言编写一个贪吃蛇小游戏的服务器,能够同时连接多个客户端
时间: 2024-05-09 16:15:58 浏览: 111
linux下用C编写的贪吃蛇游戏,编着玩的
好的,下面是一个简单的贪吃蛇游戏服务器的代码实现,仅供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
typedef struct {
int x;
int y;
} point;
typedef struct {
int id;
int score;
point pos[100];
int len;
} snake;
snake snakes[MAX_CLIENTS];
int num_clients = 0;
int main(int argc, char *argv[]) {
int server_fd, client_fds[MAX_CLIENTS];
struct sockaddr_in server_addr, client_addr;
char buffer[BUFFER_SIZE];
int i, j, k, n;
int max_fd, fd;
fd_set read_fds;
socklen_t client_len = sizeof(client_addr);
// 初始化蛇的状态
for (i = 0; i < MAX_CLIENTS; i++) {
snakes[i].id = i + 1;
snakes[i].score = 0;
snakes[i].len = 3;
for (j = 0; j < snakes[i].len; j++) {
snakes[i].pos[j].x = j + 1;
snakes[i].pos[j].y = 1;
}
}
// 创建服务器套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 绑定服务器地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
exit(1);
}
// 监听客户端连接
if (listen(server_fd, MAX_CLIENTS) == -1) {
perror("listen");
exit(1);
}
// 循环处理客户端请求
while (1) {
// 初始化文件描述符集合
FD_ZERO(&read_fds);
FD_SET(server_fd, &read_fds);
max_fd = server_fd;
for (i = 0; i < num_clients; i++) {
FD_SET(client_fds[i], &read_fds);
if (client_fds[i] > max_fd) {
max_fd = client_fds[i];
}
}
// 等待事件发生
if (select(max_fd + 1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select");
exit(1);
}
// 处理服务器套接字有新连接
if (FD_ISSET(server_fd, &read_fds)) {
if ((client_fds[num_clients] = accept(server_fd, (struct sockaddr *)&client_addr, &client_len)) == -1) {
perror("accept");
continue;
}
printf("New client connected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
num_clients++;
}
// 处理客户端有数据到达
for (i = 0; i < num_clients; i++) {
if (FD_ISSET(client_fds[i], &read_fds)) {
if ((n = read(client_fds[i], buffer, BUFFER_SIZE)) <= 0) {
// 客户端断开连接
printf("Client disconnected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
close(client_fds[i]);
for (j = i; j < num_clients - 1; j++) {
client_fds[j] = client_fds[j + 1];
}
num_clients--;
continue;
}
// 处理客户端发送的消息
buffer[n] = '\0';
printf("Received message from client %d: %s\n", i + 1, buffer);
// 更新蛇的位置
for (j = 0; j < MAX_CLIENTS; j++) {
if (snakes[j].id == i + 1) {
for (k = snakes[j].len - 1; k > 0; k--) {
snakes[j].pos[k].x = snakes[j].pos[k - 1].x;
snakes[j].pos[k].y = snakes[j].pos[k - 1].y;
}
if (buffer[0] == 'U') {
snakes[j].pos[0].y--;
} else if (buffer[0] == 'D') {
snakes[j].pos[0].y++;
} else if (buffer[0] == 'L') {
snakes[j].pos[0].x--;
} else if (buffer[0] == 'R') {
snakes[j].pos[0].x++;
}
break;
}
}
// 发送蛇的状态给所有客户端
for (j = 0; j < num_clients; j++) {
memset(buffer, 0, BUFFER_SIZE);
for (k = 0; k < MAX_CLIENTS; k++) {
if (snakes[k].id == j + 1) {
sprintf(buffer + strlen(buffer), "%d,%d,%d;", snakes[k].id, snakes[k].score, snakes[k].len);
for (n = 0; n < snakes[k].len; n++) {
sprintf(buffer + strlen(buffer), "%d,%d;", snakes[k].pos[n].x, snakes[k].pos[n].y);
}
break;
}
}
write(client_fds[j], buffer, strlen(buffer));
}
}
}
}
return 0;
}
```
主要思路是:创建一个服务器套接字,监听客户端连接。当有新客户端连接时,将其加入到文件描述符集合中,并为其创建一个蛇的状态。当客户端发送消息时,更新相应的蛇的位置,并将所有蛇的状态发送给所有客户端。当客户端断开连接时,将其从文件描述符集合中移除,并将其对应的蛇的状态从服务器中删除。
这个程序只是一个简单的示例,可能还需要进行一些改进和优化,比如处理客户端之间的碰撞、吃食物等逻辑。
阅读全文