tcp connection Problems

27 Apr 2012

Hi.

It is the first time for me working with TCP connection. I want to connect over TCP to a console application running on my pc.

My Problem: I could not connect. In my oppinion the configuration is correct.

#include "mbed.h"
#include "EthernetNetIf.h"
#include "TCPSocket.h"
#include "host.h"

EthernetNetIf eth(
  IpAddr(10,10,33,233), //IP Address
  IpAddr(255,255,252,0), //Network Mask
  IpAddr(10,10,35,254), //Gateway
  IpAddr(10,10,2,32)  //DNS
);
DigitalOut myled(LED1);
IpAddr hostIP(10,10,33,81);
Host host(hostIP,10024, "H2O-NB-1367");
TCPSocket tcp;
Serial pc(USBTX, USBRX);
EthernetErr ethErr;

int main() {
    IpAddr IP;
    IpAddr HIp;

    pc.baud(115200);

   EthernetErr ethErr = eth.setup();
    if (ethErr) {
        printf("Error %d in setup.\n", ethErr);
        return -1;
    }
    

    TCPSocketErr tcpBin = tcp.bind(host);
    if (tcpBin) {
        printf("Error %d in connect.\n", ethErr);
        return -1;
    }

    TCPSocketErr tcpErr = tcp.connect(host);
    if (tcpErr) {
        printf("Error %d in connect.\n", ethErr);
        return -1;
    }

    IP = eth.getIp();
    HIp = host.getIp();
    pc.printf("Die aktuelle IpAdresse von mBED ist %d.%d.%d.%d\n", IP[0],IP[1],IP[2],IP[3]);
    pc.printf("Die aktuelle IP von Host ist %d.%d.%d.%d\n",HIp[0],HIp[1],HIp[2],HIp[3]);
    pc.printf("Der aktuelle Port von Host ist %d\n", host.getPort());
    pc.printf("Der aktuelle Name von Host ist %s\n", host.getName());

    while (1) {
        myled = 1;
        tcp.send("test\n",5);
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}
27 Apr 2012

Is your ethernet hardware, wiring etc OK. Note that RJ45 magjack sockets all look the same but the actual wiring may be very different.

Can you get any of the ethernet example programs to run. For example http://mbed.org/users/4180_1/notebook/internet-lcd-clock/

27 Apr 2012

The connection is ok. I tried this http Client example with the wiring. I also get a IP adress over DHCP.

27 Apr 2012

I would start by removing the calls to wait() from your main loop since the networking stack isn't running while you are in a wait() call. It is possible that tcp.send() polls the network while you are in that call (I haven't used that function to know how it is implemented) but if you still have problems after removing the wait() calls, then add a call to Net::poll() in your main loop and call it as often as possible.

You can check out this post I made recently with an example main() loop that keeps the network stack properly primed and still sends data at the user's desired interval http://mbed.org/forum/mbed/topic/3457/?page=1#comment-17401 The example uses UDP but it should be very easy for you to insert your TCP code instead.