Jump to content

[SOLVED] Socket call accept


photo

Recommended Posts

Posted

Hi there,

 

Is it right that the socket call "accept" is a blocking call? How could I interrupt it and get out of the lock?

 

Kind regards,

Renato

  • 2 weeks later...
Posted

Hello, you can call nonblock() on server socket after its creation.

In that case, accept() will immediately return int value, which will tell you whether client is connected or not.

 

Example:

Socket server = new Socket(SOCKET_STREAM,8080);
server.bind();
server.listen(10);
server.nonblock();

thread([](Socket server) {
    
    Socket client = new Socket(SOCKET_STREAM);
    
    while(!server.accept(client)) {
        wait;
    }
    
    client.puts("Hello");

    client.close();
    server.close();
    delete client;
    delete server;
    
},server);
×
×
  • Create New...