The first iteration works flawlessly, how could I make it work until the end of the loop? I tried to track down the problem , initially in this for
loop, the server was not waiting for the client response for some reason, so I thought it might be because send()
and recv()
are in a void
function. So, I tried adding one more send()
and recv()
pair, which should be waiting for the client to answer, but it doesn't work this way.
I noticed that during the very first iteration, it works great, moving on the next one, it's already ruined. I think it does try to use the previous message
, so I changed that to an empty array, which didn't work, because it still uses it.
This is the for
loop:
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));
}
this is the bet
function:
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;
}
and this is the feldolgoz
function (which mainly processes the information):
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));
}
The obj.getBet()
function basically sends a message to the client whether he won or lost the round, but I highly doubt that is the culprit.The interesting part is , that the first iteration works flawlessly, how could i make it work until the end of the loop?