Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Waiting for an incoming message for a certain time.  (Read 2727 times)

0 Members and 1 Guest are viewing this topic.

megani

  • Newbie
  • *
  • Posts: 2
    • View Profile
Waiting for an incoming message for a certain time.
« on: February 21, 2019, 05:26:48 pm »
Is it possible to wait for a certain time for an incoming message. And if none comes, to get a signal?

I'm using Tcp Sockets and the receive function.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Waiting for an incoming message for a certain time.
« Reply #1 on: February 21, 2019, 05:41:18 pm »
You can set the socket to non-blocking and implement your own logic, probably best done with sf::sleep(). Retry to receive data after a certain time and if nothing gets sent, the proceed to doing something else.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

megani

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Waiting for an incoming message for a certain time.
« Reply #2 on: February 21, 2019, 06:06:49 pm »
I wrote this, but im not sure, if it will work. (Its a running thread!).

void getPoints(TcpSocket* client, int *points) {
        char rec[100];
        size_t received;
        int i;

        client->setBlocking(false);

        while (client->receive(rec, 100, received) == Socket::NotReady && i < 5000) {
                sleep(milliseconds(10));
                i++;
        }

        if (strlen(rec) != 0) {
                *points = atoi(rec);
        }

}
« Last Edit: February 21, 2019, 06:09:43 pm by megani »

 

anything