TCPSocketEvent for my laptop application

24 Mar 2011

Hi,

I've set up a TCP socket connection between my linux laptop and mbed. On the mbed, you can use the SetOnEvent routine to deal with received data. I'm trying to find a similar code to make the same routine on my laptop application so it can receive data asynchronously, but I can't seem to find anything that suits my (simple) needs.

I've tried something like:

bytes_received=recv(sock,recv_data,1024,0);
while(bytes_received != 0) {
    	//printf("Number of bytes received = %d\n",bytes_received); //Print the amount of bytes received
    	recv_data[bytes_received] = '\0';
    	printf("Received data = %s\n" , recv_data); //Print the data.
    	bytes_received=recv(sock,recv_data,1024,0);
    	sleep(0.1);
}

But as soon as there's nothing received, it gets stuck on the recv(); with Thread [1] (Running : Step)

bytes_received is 256 on the first go, 128 on the second, and then crashes on the third.

With while(bytes_received > 0) it doesn't print all data.

But anyway, I want to get out of the while loop since I want the reception to be asynchronous. So does anyone know if there's anything like the SetOnEvent, for C in linux?

Thanks a bunch!

Melchior