0

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?

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Gameerik
  • 5
  • 4
  • You need to check the return value of `recv` and `send` for more than just error conditions. The only way to know you wrote or read the number of bytes you need is to check the return value. `recv` is the most noticeable. Good reading on that here: https://stackoverflow.com/questions/2295737/case-when-blocking-recv-returns-less-than-requested-bytes – user4581301 May 08 '18 at 17:15
  • @user4581301 seems helpful, so I should change every recv in my loop to this custom function if I don’t misunderstand ? Thank You ! – Gameerik May 08 '18 at 17:19
  • A function could help, but at the very least you need to make sure if you want `size` bytes you keep reading until you have size `bytes` If it is supported, and it usually is these days, [setting the `MSG_WAITALL` flag in calls to `recv`](http://man7.org/linux/man-pages/man2/recvmsg.2.html) is a quick hack fix. Generally if you have other stuff to do, you read what you've got and come back later. If the program can't continue until you have `size` bytes, you might as well use `MSG_WAITALL` to block until you have `size` bytes (or disconnection or error). – user4581301 May 08 '18 at 17:29

0 Answers0