我正在编写一个客户端服务器程序,以使用简单的多人数学游戏开发聊天室。队列中只能侦听5个客户端。 3个客户端连接到服务器后,应创建它以启动游戏。直到连接了3个客户端,服务器才能启动游戏。当第4个客户端和第5个客户端与服务器连接时,他们应转到聊天室。 - - - 我的问题 - - - - 我的服务器无法使用“在3个客户端被接受之后”的条件。我不知道如何证明该条件。我想在3个客户端连接后开始游戏,并且每个客户端都可以在条件满足后才能玩游戏。我尝试了一些代码,请帮助
3个客户端连接后,我进入了聊天室。我把简单的问题称为“样本答案?”只是为了运行代码。
--------这是主要功能下的代码--------
//这是主要代码
int main(int argc, char **argv){
if(argc != 2){
printf("Usage: %s <port>\n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
//int port2 = 3333;
int option = 1;
int listenfd = 0, connfd = 0; //listenfd2 = 0, connfd2 = 0 ;
if(setsockopt(listenfd, SOL_SOCKET,(SO_REUSEPORT | SO_REUSEADDR),
(char*)&option,sizeof(option)) < 0){
perror("ERROR: setsockopt failed");
return EXIT_FAILURE;
}
if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR: Socket binding failed");
return EXIT_FAILURE;
}
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
pthread_t tid;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(ip);
serv_addr.sin_port = htons(port);
/* Ignore pipe signals */
signal(SIGPIPE, SIG_IGN);
/* Listen */
if (listen(listenfd, 10) < 0) {
perror("ERROR: Socket listening failed");
return EXIT_FAILURE;
}
printf("=== WELCOME TO THE GAME & CHAT ROOM ===\n");
while(1){
socklen_t clilen = sizeof(cli_addr);
connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &clilen);
/* Check if max clients is reached */
if((cli_count + 1) > 3){
printf("Max clients reached. Rejected: ");
print_client_addr(cli_addr);
printf(":%d\n", cli_addr.sin_port);
client2_t *cli2 = (client2_t *)malloc(sizeof(client2_t));
cli2->address = cli_addr;
cli2->sockfd_chat = connfd;
cli2->uid_chat = uid2++;
queue2_add(cli2);
pthread_create(&tid, NULL, &handle2_client, (void*)cli2);
sleep(1);
//}
}
else {
/* Client settings */
client_t *cli = (client_t *)malloc(sizeof(client_t));
cli->address = cli_addr;
cli->sockfd = connfd;
cli->uid = uid++;
/* Add client to the queue and fork thread */
queue_add(cli);
//pthread_create(&tid, NULL, &handle_client, (void*)cli);
wait_count();
cli_count++;
printf("client count: %d\n" , cli_count);
pthread_create(&tid, NULL, handle_client, (void*)cli);
/* Reduce CPU usage */
sleep(1);
}
}
return EXIT_SUCCESS;
}
----------这是“处理客户端”功能--------------------
void *handle_client(void *arg){
if( cli_count == 3){
char buff_out[BUFFER_SZ];
char game_buff[BUFFER_SZ];
char name[32];
//int leave_flag = 0;
client_t *cli = (client_t *)arg;
// Name
if(recv(cli->sockfd, name, 32, 0) <= 0 || strlen(name) < 2 || strlen(name) >= 32-1){
printf("Didn't enter the name.\n");
//leave_flag = 1;
} else{
strcpy(cli->name, name);
sprintf(buff_out, "%s has joined to the Game\n", cli->name);
printf("%s", buff_out);
send_message(buff_out, cli->uid);
}
bzero(buff_out, BUFFER_SZ);
printf("===The game is started=== \n");
strcpy( game_buff , "===The game is started=== \n");
game_message(game_buff);
bzero(game_buff, BUFFER_SZ);
if( cli_count == 3 ){
printf ("sample game answer ? \n");
strcpy( game_buff , "sample game answer ? \n");
game_message(game_buff);
bzero(game_buff, BUFFER_SZ);
int receive_answer = recv(cli->sockfd, buff_out, BUFFER_SZ, 0);
if ( receive_answer > 0 ){
if(strlen(buff_out) > 0){
if(strcmp(buff_out, "15") == 0){
sprintf(game_buff,"Correct answer given by %s !! 10 points given \n" , cli->name);
game_message(game_buff);
score_store(cli->uid);
}
}
}
else {
strcpy(game_buff, "Wrong answer!! \n");
game_message(game_buff);
close(cli->sockfd);
queue_remove(cli->uid);
free(cli);
cli_count--;
pthread_detach(pthread_self());
}
}
bzero(buff_out, BUFFER_SZ);
bzero(game_buff, BUFFER_SZ);
}
printf("returning!!!\n");
return NULL;
}
我需要在3个客户端连接后开始游戏。条件满足后,所有这些已连接的客户端都应该玩游戏。