Help with the TCPSocket

09 Feb 2011

Hello, I´ve programmed a TCP Socket interface using the TCPSocket and the EthernetNetIf libraries, but I think that I don´t understand very well the onTCPSocketEvent function. I´m able to make a "send" once but I don´t know how and where can I do more "send". I mean, for example, in the "main" I make he "connect", then , in the " TCPSOCKET_CONNECTED" event, I make the "send" and in the "TCPSOCKET_READABLE" I make the "recv". If I want to send more than one packet, do I need to make another "connect" or how can I do?

I´m new working with mbed and C++ so I need some help.

Thanks & Regards,

María.

11 Feb 2011

I am new to the mbed as well and have also been trying out the TCP Socket interface. You can send again when the onTCPSocketEvent function is called with the "TCPSOCKET_WRITEABLE" event. You could try changing your code to test for both events, like this:

  switch(e)
    {
    case TCPSOCKET_CONNECTED:
    case TCPSOCKET_WRITEABLE:
        tcp.send( ... );
        break;

Incidentally, I have discovered I can send packets at up to 1 KHz using a UDP socket, but when I try the same thing with a TCP socket the maxmium rate drops to just 2 Hz. Is this expected from a TCP socket or is there a secret to programming them efficiently that I am missing?

13 Feb 2011

If you want to handle a simple line-based protocol, have a look at the TcpLineStream library. It also can serve as an example of how to program with the TCPSocket class.

Steven, you can should be able to send TCP packets faster than just 2 per seconds. Maybe you have some wait()s between your Net::Poll() calls?

14 Feb 2011

Thanks for your help. I changed my code in the way Steven said but now I have a problem with the "recv". For example, I do this:

switch (e) {
        case TCPSOCKET_CONNECTED: 
        case TCPSOCKET_WRITEABLE: {
            int long_comando=socket.send(read_mode, sizeof(read_mode));
            int long_comando2=socket.send(clear_buffer, sizeof(clear_buffer));
                             }
        break;
        
        case TCPSOCKET_READABLE: {
            char respuesta[256];
            int long_respuesta= socket.recv(respuesta, 256);
                        
            }
        }
        break;

But I only receive the response to the first "send" (and it never stops send the commands...)

Thanks!