11 years ago.

problem2 with example program: TCPSocket_Hello world

Hello, I run the example program TCP socket Hello world on the mbed.

#include "mbed.h"
#include "EthernetInterface.h"

int main() {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());
    
    TCPSocketConnection sock;
    sock.connect("mbed.org", 80);
    
    char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
    sock.send_all(http_cmd, sizeof(http_cmd)-1);
    
    char buffer[300];
    int ret;
    while (true) {
        ret = sock.receive(buffer, sizeof(buffer)-1);
        if (ret <= 0)
            break;
        buffer[ret] = '\0';
        printf("Received %d chars from server:\n%s\n", ret, buffer);
    }
      
    sock.close();
    
    eth.disconnect();
    
    while(1) {}
}

The strange thing is: sometimes i get an IP adres, sometimes i dont. This doesnt seems normal to me :) This also takes several minutes before i see something happening in hyperterminal?? If i get an IP adres, al the other information doesnt come. So it says

IP Address is 192.168.0.149

if i dont get an IP adres i get this:

IP Address is
              Received 299 chars from server:
                                             HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Sat, 04 May 2013 15:45:47 GMT
Content-Type: text/plain
Connection: close
Last-Modified: Fri, 27 Jul 2012 13:30:34 GMT
Cache-Control: max-age=36000
Expires: Sat, 04 May 2013 22:24:35 GMT
Vary: Accept-Encoding
X-Mystery-Header: 1861457452
X-be: web
         Received 40 chars from server:
                                       0_prod_sjc
Age: 12072

Hello world!

Anybody has an idea how this is possible? THnx in advance!!!

1 Answer

11 years ago.

Hi Adriaan, can you post an image of your schematic connections (the connection of the rj-45 plug to your mbed)

Greetings

Accepted Answer

Hey Ney, already thnx for the quick response. I got it working. I always get an IP adres now. But i have some next issues :) im totally new with the ethernet/socket programming. but it seems nice to learn. I posted them again in a new question. :) if you got some extra time, it would be nice, otherwise thnx for the alreaydy offered help greetings

posted by Adriaan Van Steendam 05 May 2013

again im there, i got my program running and working, but now im writing down everything i learned about sockets and TCP connections so if i lay this down for some time i can pick up my handbook. i know in theory what has to happen but if i compare this to the code there are still some gaps. would you mind looking at my other question (if you feel to and have time) and check if my assumptions are right/wrong and fill in the gaps. thnx and enjoy! cheers

posted by Adriaan Van Steendam 13 May 2013

Hi again adriaan, can you send me the link of the question that you want me to check?

Greetings

posted by Ney Palma 15 May 2013