10 years ago.

Non-blocking for received method

Good morning,

Is there a non-blocking solution for the receive method? I'm trying to control a small LED matrix where I have to switch all the rows in a specific moment. If nothing is sent over the network, the matrix will stop displaying.

int main (void) {
    pc.baud(9600);
    pc.printf("Started\r\n");
    
    EthernetInterface eth;
    eth.init(); // Use DHCP
    eth.connect();
    pc.printf("IP Address %s\r\n", eth.getIPAddress());
    
    TCPSocketServer server;
    TCPSocketConnection sock;
 
    server.bind(SERVICE_PORT);
    server.listen();
 
    server.set_blocking(false, 500); // SET NON-BLOCKING
    pc.printf("Service binded to port %d\r\n", SERVICE_PORT);
    
    while (true) {
        pc.printf("Waiting for connection\r\n");
        TCPSocketConnection client;
        server.accept(client);
        //client.set_blocking(false, 500); // Timeout 500ms
        
        
        pc.printf("Connection from %s\r\n", client.get_address());
        char buffer[100];
        while (true) {
            led1 = true;
            
            int n = client.receive(buffer, sizeof(buffer));
            
            /* Parsing received buffer for my matrix display */
            
            // Returning message to client
            client.send_all("*ok;", 4);
            if (n <= 0) break;
            
            /* Controlling all the lines of my matrix */
            display();
        }
        
        led1 = false;
        client.close();
    }
}

Regards

Question relating to:

1 Answer

5 years, 9 months ago.

For those of you still searching for the answer yes, there is a non-blocking mode. The mode .set_blocking is inherited by Socket.h.

server.set_blocking(false, 10); Timeout after (10)ms

Apply this to INSTANCE of TCPSocketServer, TCPSocketConnection, etc.

e.g. sock.set_blocking(false, 10) or client.set_blocking(false, 10);

Hope this helps.