我正在学习Linux / UNIX套接字,所以我写了一个非常简单的“游戏”,基于它,称为“21匹配”。有一个由21个匹配组成的堆,每个玩家从中获得一个,两个或三个匹配。参加最后一场比赛的人将输掉比赛。
显然,获胜的关键是补充你的对手最多4场比赛,所以他必须参加最后一场比赛(仅在第一回合时才有效)。因此,客户端连接到服务器并对服务器“播放”直到他输了。可以连接多个客户端,因此我限制了连接数,使我的主机拒绝任何其他客户端。
我唯一无法修复或解释的是,当有人被拒绝时,即使他没有做出任何转弯,第一个客户也会立即失去游戏 。如果我让新客户端触及任何客户端堆,可能会解释这一点,但我不这样做!我还跟踪了缓冲区数组,但它不会受到任何影响。
以下是代码:
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <poll.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void die (const char *s, int errcode);
int mkservsock();
void acceptclient();
void dropclient (int client);
void make_turn (int client);
enum
{
port = 3333,
buf_s = 100,
limit = 3
};
void die (const char *s, int errcode)
{
perror (s);
exit (errcode);
}
struct pollfd fds[limit + 1]; // client socket descriptors array
int left[limit + 1]; // how many matches left
int nfd = 1; // number of the next client
char buffer[buf_s]; // buffer for communicating
// creates a single socket to poll
int mkservsock()
{
int s = socket (AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons (port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
die ("Can't bind socket", 1);
if (listen (s, 1) == -1)
die ("Can't start listening", 1);
return s;
}
// adds new client to descriptors array or rejects it, if number of connections exceeds the limit
void acceptclient()
{
int c = accept (fds[0].fd, 0, 0);
fds[nfd].fd = c;
fds[nfd].events = POLLIN;
if (nfd == limit + 1)
{
sprintf (buffer, "Server is busy, try again later\n");
send (fds[nfd].fd, buffer, strlen (buffer), 0);
close (fds[nfd].fd);
return;
} else
{
left[nfd] = 21;
sprintf (buffer, "Matches available: %d\nTake 1, 2 or 3!\n", left[nfd]);
send (fds[nfd].fd, buffer, strlen (buffer), 0);
nfd++;
}
}
// disconnects a client in case of match ending or inappropriate data sent
void dropclient (int client)
{
int i, j;
close (fds[client].fd);
for (i = client; i < nfd - 1; i++)
{
fds[i] = fds[i + 1];
left[i] = left[i + 1];
}
nfd--;
}
void make_turn (int client)
{
int n = recv (fds[client].fd, buffer, buf_s, 0);
if (n == 0) {
dropclient (client);
return;
} else if (n > 3)
{
// input counts as incorrect if it contains more than 1 symbol,
// since we expect a single digit and nothing else
// (yep, we get two extra bytes when receiving a message)
sprintf (buffer, "I can break rules, too. Goodbye.\n");
send (fds[client].fd, buffer, strlen (buffer), 0);
dropclient (client);
return;
}
// way to extract a digit from the character
int received = buffer[0] - '0';
if (received > 3 || received <= 0)
{
sprintf (buffer, "You're allowed to take 1, 2 or 3 matches only\n");
send (fds[client].fd, buffer, strlen (buffer), 0);
return;
} else if (received > left[client])
{
sprintf (buffer, "You can't take more than %d\n", left[client]);
send (fds[client].fd, buffer, strlen (buffer), 0);
return;
} else
{
// obviously, it happens only when there's the only match,
// and the client has to take it
if (left[client] == received)
{
sprintf (buffer, "You lost!\n");
send (fds[client].fd, buffer, strlen (buffer), 0);
dropclient (client);
return;
} else
{
// sort of "keeping the game up"
left[client] -= 4;
sprintf (buffer, "Matches left: %d (I took %d)\n", left[client], 4 - received);
send (fds[client].fd, buffer, strlen (buffer), 0);
return;
}
}
}
int main (int argc, char *argv[])
{
fds[0].fd = mkservsock ();
fds[0].events = POLLIN;
for (;;)
{
int status, i;
status = poll (fds, nfd, -1);
if (status == -1)
die ("Error while polling", 1);
if (fds[0].revents & POLLIN)
acceptclient();
for (i = 1; i < nfd; i++)
{
if (fds[i].revents & POLLERR)
{
printf ("Got troubles on %d\n", i);
continue;
}
if (fds[i].revents & POLLIN)
make_turn (i);
}
}
}
这是达到最大值后第一个客户端发生的事情。连接数:
Matches available: 21
Take 1, 2 or 3!
(不要带任何东西,有人联系并被拒绝)
(在此之后,发布任何数字,它会说堆中只有一个匹配)
1
You lost!
请注意,如果输入等于剩余的匹配数,则会丢失if和 。那么,发生了什么?
答案 0 :(得分:0)
当您致电left[]
并且未检查acceptclient()
是否有空间来存储新连接时,您会覆盖fds
。
首先初始化新客户端fds
然后拒绝连接,但是为时已晚,因为您已经在数组之外编写了一个条目。除了数组之外,还有left[]
数组,现在存储了1或0。所以,无论第一个客户如何回应,他总是拿最后一个。