Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 12 months ago.
polling blocks the main loop
I use this testsample:
while (true) { pc.printf("\nWait for new connection...\n"); TCPSocketConnection client; server.accept(client); client.set_blocking(false, 1500); // Timeout after (1.5)s pc.printf("Connection from: %s\n", client.get_address()); char buffer[256]; while (true) { int n = client.receive(buffer, sizeof(buffer)); if (n <= 0) break; client.send_all(buffer, n); if (n <= 0) break; } client.close(); } }
In this line ( server.accept(client); , it waits until the client sends data. Serial output:
StartInitialized, MAC: 00:02:F7:F0:00:00 IP: 192.168.0.120, MASK: 255.255.255.0, GW: 192.168.0.200 Wait for new connection...
This is not usefull, because the cpu is still doing other things It would be better to query whether data are to be collected. This is the code template with the arduino lib with non blocking:
// listen for incoming clients EthernetClient client = server.available(); if (client) { //Serial.println("new client"); while (client.connected()) { if (client.available()) { char c = client.read(); ....