gpt4 book ai didi

无法计算 C 中插槽的回合数

转载 作者:行者123 更新时间:2023-11-30 17:06:43 25 4
gpt4 key购买 nike

我正在尝试向 2 个客户端发送两轮提示,每轮的提示都不同。所以我写了一个简单的客户端和服务器代码。

这是服务器代码:

#include <stdio.h>
#include <string.h> //strlen
#include <stdlib.h> //strlen
#include <stdbool.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h> //write
#include <pthread.h> //for threading , link with lpthread
#define N 2 // player numbers
#define CardUpperLimit 10;

// Game setting
int n = 1000;
int flag_phase1 = 1;
pthread_t sniffer_thread[N];
char message[256] , client_message[2000];
int PHASE=0;
int player_index=0;

void *test(void *);
void error(char *msg)
{
perror(msg);
exit(1);
}

int main(int argc , char *argv[])
{
// Socket setting
int i,j;
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;
int whoesTurn;

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("bind failed. Error");
return 1;
}
puts("Bind done");

//Listen
listen(socket_desc , 3);
c = sizeof(struct sockaddr_in);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

/* Prepare section.. */
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");
new_sock = malloc(1);
*new_sock = client_sock;

if( pthread_create( &sniffer_thread[player_index] , NULL , test , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}

player_index+=1;

// break if all players entry..
if ( player_index== N ) break;
}
printf("All players are in the game!\n");

/* Phase 1 ... */
PHASE=1;
for ( whoesTurn = 0; whoesTurn < N; whoesTurn++)
{
/* Communication! */
bzero(client_message, 256);
n=whoesTurn+1;
while(flag_phase1); // busy waiting until this thread function finish..
flag_phase1=1;
printf("Finish Communication!!\n");

}
n=0; // reset checking index
printf("First Phase done..\n");

/* Phase 2 .. */
PHASE+=1;
for (whoesTurn=0;whoesTurn < N; whoesTurn++)
{
/* Communication Part! */
bzero(client_message, 256);
n=whoesTurn+1;
while(flag_phase1) {usleep(100);} // busy waiting until this thread function finish..
flag_phase1=1;
printf("Finish Communication!!\n");

}
n=0;

PHASE=0;
while(1)
sleep(1);

if (client_sock < 0)
{
perror("accept failed");
return 1;
}

return 0;
}

void * test(void *socket_desc)
{
int i, j;
int sock = *(int*)socket_desc;
int read_size;
int player_index_handler = player_index;

while(1)
{
/* Communication */
if(n==player_index_handler)
{
printf("I am player:%d\n", player_index_handler);
if (PHASE==1)
*message = '1';
else if (PHASE==2)
*message = '2';
write(sock, message, strlen(message));
printf("[%d],Phase msg sent:%d\n", player_index_handler,atoi(message));

read_size = recv(sock , client_message , 2000 , 0);
printf("the client_message is:%s, from player %d\n", client_message, player_index_handler);
bzero(message,256);

// Finish works..
flag_phase1=0;
}

}
}

这是我的客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>

void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sockfd, portno, n, k;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];
portno = 8888;
/* Create Socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
/* Catch Server IP */
server = gethostbyname("localhost");
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");

while(1)
{

int read_size =recv(sockfd , buffer , 256 , 0);
if (read_size>0)
{
// show hint in client..
printf("%s\n", buffer);
if (atoi(buffer)==1)
printf("FIRST HINT\n");
else if (atoi(buffer)==2)
printf("SECOND HINT\n");

bzero(buffer,256);
// Enter something..
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
// bzero(buffer,256);
if (n < 0) error("ERROR writing to socket");
}
else ;
}
return 0;
}

我遇到的问题是“请输入消息”提示显示的次数过多,导致输出显示的内容超出了我的预期。如何让所有客户端完成输入字符串时显示此提示消息?

最佳答案

服务器的行为未定义有几个原因,最重要的是:

  • 您只为单个字节分配空间,而不是 sizeof(*new_sock)。 – 一些程序员家伙
  • main (player_index+=1;) 和 test (intplayer_index_handler =player_index;)。思考:您无法预测 main 中的增量是发生在 test 中的赋值之前还是之后。

要解决这两个问题,您可以进行以下更改:

  • 为套接字定义全局数组

    int sockets[N];

    并在main中进行更改

            new_sock = malloc(1);

            new_sock = sockets+player_index;
  • 测试中更改

        int player_index_handler = player_index;

        int player_index_handler = (int *)socket_desc - sockets + 1;    // 1 to N

然后您可以解决其他问题,例如 int n = 1000; 处缺少 volatile

关于无法计算 C 中插槽的回合数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com