Simple Telnet Client

25 Jul 2010

I'm trying to get my mbed to connect to a telnet server and exchange some data.  I've searched the forums and read the source code, and can't find a reason why this shouldn't work:

EthernetNetIf *eth;
TCPSocket *tcpsock; 

int main() {

    printf("Init\n");

    printf("\r\nSetting up...\r\n");
    eth = new EthernetNetIf();
    EthernetErr ethErr = eth->setup();
    if(ethErr)
    {
        printf("Error %d in setup.\n", ethErr);
        return -1;
    }
    printf("\r\nSetup OK\r\n");

    // the magic happens here
    
    tcpsock = new TCPSocket();
    
    // make a host
    Host host( IpAddr( 192,168,0,100 ), 6667, "server" );
    
    // connect the event callback
    tcpsock->setOnEvent( sockEvent );
    
    TCPSocketErr err = tcpsock->connect( host );
    if( err )
    {
        prettyTCPError( err );
        printf( "error connecting to host (%d )\r\n", (int) err );
        return -1;
    }
 
    return 0;
}
connect() will always return TCPSOCKET_MEM ( -65531 ).

In TCPSocket.h, this is commented as "Not enough mem".  Somehow that seems very unlikely, considering there is nothing else to this program!

26 Jul 2010

Hi Ed,

Could you publish your program so that I can have a look and test it?

Thanks,

Donatien

26 Jul 2010

Here it is:

http://mbed.org/users/eparadis/programs/ethernet_test/latest

26 Jul 2010 . Edited: 26 Jul 2010

Ok thanks,

I've checked that and there was a bug in the stack (the error is wrongly returned).

For now the best for you is to ignore it, this will be fixed this week in the next stack release.

BTW, is there a reason why you're using dynamic initialization for the Ethernet interface?

Good luck with the project, a Telnet client would add up great functionalities to the mbed.

Cheers

Donatien

26 Jul 2010

Thanks for your help, Donatien!

Ed

27 Jul 2010 . Edited: 27 Jul 2010

Well, I tried ignoring the returned error and assuming the socket connect()s properly.  It didn't work.   send() reports that the correct number of bytes are sent, but I see no ethernet activity on the switch port the mbed is connect to and nothing on my server.

I checked the HTTPclient example and after sorting through its 3000 layers of abstraction found that it was doing the same thing I am.  So I'm stumped.

For reference, I'm currently trying to connect to a Linux box running "netcat" using `nc -v -v -l -p 6668`.  Netcat never reports any connection attempt.  Am I missing something such as an off-by-one on how the ports are numbered?  Is there some trick to specifying the IP address of the host I'm trying to connect to?

Host host( IpAddr( 192,168,0,100 ), 6668, NULL );

Is there anyone else who has tried to use TCPSocket?  Thanks for any help that comes my way.

Regarding the dynamic instialization of the Ethernet interface; I'm just following the examples.  I don't have any documentation so I'm copying as much from the examples as possible.  If by 'dynamic' you mean DHCP, then 'yes' because I want DHCP support.  Eventually I'll use the DNS query stuff too, but I was trying to remove possible causes of error.  If there is a simpler way of doing what I'm trying to do, I'll try it.  If you mean not declaring them as globals and using the `new` keyword, it was because that was the way the examples did it, so I assumed they knew something about the compiler that I didn't (which is a lot: I know very little about what the compiler is doing in this case).

"Connect to a host and send a few bytes" should be about 6 lines of code.  I think it'd make a great example.  Right now the examples are all very specific to Twitter, SQL, etc.

27 Jul 2010

Hi Ed,

Do you wait for the "TCPSOCKET_CONNECTED" event to happen before starting to send data?

There is no trick on how to specify the port or IP address.

I meant dynamic allocation. Afaik the examples all use static allocation for the EthernetNetIf instance (doesn't have to be global).

I'm aware that this sockets API needs a lot more documentation, I'll try to fix that soon.

Donatien

PS: 3000 layers:)?

27 Jul 2010

I did some documentation here: http://mbed.org/cookbook/Sockets-API.

Donatien

28 Jul 2010

Thanks for your help again, Donatien!

I'll be working on the mbed project later tonight but a quick look through that documentation shows that I'll be well on my way.

Of course it looks like I was using TCPSocket incorrectly. When I get a demo, I'll make sure to post it back here so that other (ahem) "inexperienced users" can get their feet on the ground.

Thanks for supporting me and everyone else here on the mbed forums.

Regarding dynamic allocation:  I wouldn't think that there would be much difference in C++ in this case, since the object will be initialized early in the program and never destructed.  I'm eager to learn more.

Ed

PS: (re: 3000 layers) :) It was pretty late and I was getting frustrated! Its some great code but it'd be easier to browse through with some object property browsing built into the editor. :)