02 Mar 2011


i need to make a tcp connection with mbed and pc. but i don't know what i do wrong. i already can receive tcp messages but i got a problem with the sender can anybody help?

  1. include "mbed.h"
  2. include "EthernetNetIf.h"
  3. include "TCPSocket.h"

EthernetNetIf eth; TCPSocket tcp; Host server(IpAddr(10,101,8,229), 13000, "server");

struct { int connected; int writeable; }flags;

void onTCPSocketEvent(TCPSocketEvent e) { printf("New TCPSocketEvent: %d",e); switch(e) { case TCPSOCKET_CONNECTED: flags.connected = 1;break;

case TCPSOCKET_WRITEABLE: flags.writeable = 1;break;

case TCPSOCKET_CONTIMEOUT: case TCPSOCKET_CONRST: case TCPSOCKET_CONABRT: case TCPSOCKET_ERROR: case TCPSOCKET_DISCONNECTED: { tcp.close(); flags.connected = 0; }break; } }

void sendMessage(Host host, char* message, int msgLen) { printf("sending\r\n"); TCPSocketErr err = tcp.connect(host); printf("Connectection...\r\n"); if(err) { printf("Error connecting to host %d\r\n", (int) err);} else { printf("connectection to host OK %d\r\n",(int) err);}

if(flags.connected && flags.writeable) { printf("Sending data\r\n"); flags.writeable = 0; int transLen = tcp.send(message,msgLen); if(transLen != msgLen) {printf("Error in transmission\r\n");} else {printf("transmission OK\r\n");} printf("end sending data \r\n");

} else { printf("sending data fail\r\n"); printf("connected: %d, writeable %d\r\n",flags.connected , flags.writeable); } printf("tcp close\r\n"); tcp.close(); }

int main() { DigitalOut led4(LED4, "led4"); led4=!led4; EthernetErr ethErr = eth.setup(); if(ethErr) printf("Ethernet Error %d\r\n", ethErr);

else printf("mbed is online...\r\n");


Timer tmr; tmr.start(); while(true) { Net::poll(); if( > 0.2) sec { led4=!led4; Show that we are alive tmr.reset();

printf("send Message \r\n"); sendMessage(server,"Hello Server\n",13); } } }

i can connect to the host but the flags connected and writeble doesn't go high and i don't know why. does anybody know or does anyon has working cpp-code for the mbed?

thnx marnix you also can mail to

02 Mar 2011

When you do a connect() call, you need to

  • do a Net::poll() call
  • wait a for the connection afterwards.

The way your program works: you try to connect, check immediately afterwards for a connection, which hasn't happened at this moment, and close the socket.

03 Mar 2011

thnx for the reply.

to what you tell is that i need to wait until the connection flag is high? and than wait until the writeable flag is high.

i'm really new in working with the mbed and tcp messages. hope you can help me to fix things cause i don't find a solution and i need it for school and i don't know what i do wrong of what i forget



03 Mar 2011

You need to wait for the connected flag, yes. As for the writable flag - I think it should be that way, but AFAICS it only goes true when a write has happened. So in my code, I wait for the connected flag, and then do the write, ignoring the flag.

If you want to handle a line-based protocol, you can try my TCPLineStream class.

03 Mar 2011

thnx i think i know what i need to do now thnx.