TCP socket waits for a char
Topic last updated
17 Oct 2012, by
Gerrit Pathuis.
3 replies
TCP,
tcp/ip,
TCPSocket
GerritPathuis
#
14 Oct 2012
Hi,
The program contains;
EthernetInterface eth;
eth.init();
eth.connect();
pc.printf("IP Address is %s\n\r", eth.getIPAddress());
TCPSocketServer server;
server.bind(ECHO_SERVER_PORT);
server.listen();
pc.puts("\r\nWait for new connection...\n\r");
TCPSocketConnection socket;
server.accept(socket);
socket.set_blocking(true);
int n = socket.receive(buffer, 1);
All works OK but the socket.receive waits until a char to arrive, this locks up my program.
Is there a way to know upfront that the buffer contains a character?? then i can prevent the locking up.
Thanks,
Gerrit
Replies
Hi Garrit -
Have you read about blocking and non-blocking on the Sockets page? There is also some explanation about this function in the comments on that page, specifically from Emilio on August 8, 2012.
https://mbed.org/handbook/Socket
You have blocking set to TRUE in your code above, so it will wait forever. You could set it to FALSE and give it a timeout value.
I hope that helps,
Mike
GerritPathuis
#
16 Oct 2012
Hi Mike,
Thanks for your info,I will give it a shot and see what happens.
Thanks again,
Gerrit
GerritPathuis
#
17 Oct 2012
Mike,
It works like a charm.
Thank you very very much
Gerrit
Please log in to post a reply.
Hi,
The program contains;
EthernetInterface eth; eth.init(); eth.connect(); pc.printf("IP Address is %s\n\r", eth.getIPAddress());
TCPSocketServer server; server.bind(ECHO_SERVER_PORT); server.listen(); pc.puts("\r\nWait for new connection...\n\r"); TCPSocketConnection socket; server.accept(socket); socket.set_blocking(true); int n = socket.receive(buffer, 1);
All works OK but the socket.receive waits until a char to arrive, this locks up my program.
Is there a way to know upfront that the buffer contains a character?? then i can prevent the locking up.
Thanks, Gerrit