第一次迭代完美无缺,我怎么能让它工作到循环结束?我试图追踪问题,最初在这个for
循环中,服务器由于某种原因没有等待客户端响应,所以我认为可能是因为send()
和recv()
是在void
函数中。所以,我尝试添加一个send()
和recv()
对,这应该等待客户端回答,但它不会以这种方式工作。
我注意到在第一次迭代中,它运行良好,继续下一个,它已经毁了。我认为它确实尝试使用之前的message
,因此我将其更改为一个空数组,但它仍无法使用。
这是for
循环:
for (int i = 0; i < std::atoi(max_round); i++)
{
bet(client1, message, to_client, size);
feldolgoz(message, game_obj, client1);
bet(client2, message, to_client, size);
feldolgoz(message, game_obj, client2);
game_obj.RoundResult();
char next_round[size] = "Irjon valamit kovetkezo korbe lepesert!";
char answer[size];
send(client1, next_round, sizeof(next_round), 0);
if (recv(client1, &answer, size, 0) < 0)
{
Receive_Error obj;
throw obj;
}
send(client2, next_round, sizeof(next_round), 0);
if (recv(client2, &answer, size, 0) < 0)
{
Receive_Error obj;
throw obj;
}
bzero(answer, sizeof(answer));
}
这是bet
函数:
void bet(int client, char *message, char *to_client, const int size)
{
int stuff = send(client, to_client, sizeof(to_client), 0);
if (stuff < 0)
{
Send_Error obj;
throw obj;
}
if (recv(client, message, sizeof(message), 0) < 0)
{
Receive_Error obj;
throw obj;
}
这是feldolgoz
函数(主要处理信息):
void feldolgoz(char *message, Roulette &obj, int client)
{
char *temp;
char *word;
char color = 0;
int count = 0;
int money = 0;
int number = -1;
temp = strtok(message, " ");
while (temp != NULL)
{
word = temp;
switch (count)
{
case 0:
color = word[0];
break;
case 1:
number = (int)word[0];
break;
case 2:
color = std::atoi(word);
break;
}
count++;
temp = strtok(NULL, " ");
}
obj.getBet(color, number, money, client);
bzero(message, sizeof(message));
}
obj.getBet()
函数基本上向客户端发送一条消息,无论他赢了还是输了一轮,但我高度怀疑这是罪魁祸首。有趣的是,第一次迭代完美无瑕地工作,我怎么能让它工作到循环结束?