0
\$\begingroup\$
#include "mbed.h"
#include "EthernetInterface.h"

// Network interface
EthernetInterface net;

TCPSocket listener;
TCPSocket* client;

DigitalOut led1(LED1);

Serial pc(USBTX, USBRX);


// Socket demo
int main()
{

    int remaining;
    int rcount;
    char *p;
    char *buffer = new char[256];
    nsapi_size_or_error_t result;

    int n = net.set_network("192,168,10,101","255,255,255,0","192,168,10,10");
    pc.printf("\n Success? %d\n", n);
    net.connect();

    listener.open(&net);
    listener.bind(80);

    listener.listen(1);

    client = listener.accept(NULL);
    client->set_timeout(1000);

    led1 = 0;
    while(1) {

        int remaining = 256;
        int rcount = 0;
        p = buffer;

        while (remaining > 0 && 0 < (result = client->recv(buffer, remaining))) {
            p += result;
            rcount += result;
            remaining -= result;
        }

        if (result == 0) { //Peer has shutdown connection
            client->close(); //close socket;

            client = listener.accept(NULL); //block again until new connection comes through
            client->set_timeout(1000);
        }


        if (result == NSAPI_ERROR_TIMEOUT ) { //Assume connection has been lost
            client->close(); //close socket;

            client = listener.accept(NULL); //block again until new connection comes through
            client->set_timeout(1000);

        }






    }


}

I'm trying to establish a connection between my mbed and PC via TCP, using Teraterm.

The host PC has an Ethernet cable connected to the breakout board I've wired to my mbed. I've changed the localhost settings so that only IPv4 is selected, and chosen a random static IP, to disable DHCP. Subnet mask has been set to 255.255.255.0 and random IP (192.168.10.10) for Gateway.

When trying to connect to the mbed with Teraterm (Service: other, Protocol: IPv4, host: 80), an "Invalid host" error appears.

I'm new to networking, so don't really know where the problem lies, although I do have some suspicions.

  1. Are the IP addresses parsed in the correct format? The mbed documentation (https://os.mbed.com/docs/mbed-os/v5.8/reference/ethernet.html) isn't clear at all on this.

  2. Is the gateway something I can choose to set, or is this automatically assigned by the host PC?

  3. Do I need to disable wireless?

I'm using the Sparkfun Ethernet breakout board, if that makes a difference.

\$\endgroup\$
  • \$\begingroup\$ Do you need an Ethernet Crossover Cable?? en.wikipedia.org/wiki/Ethernet_crossover_cable \$\endgroup\$ – Scott Seidman Mar 26 at 11:49
  • \$\begingroup\$ @ScottSeidman, Just tried that and still no luck. \$\endgroup\$ – 19172281 Mar 26 at 11:54
  • \$\begingroup\$ Do you have an mbed board with Ethernet which you could try my code on? Not sure if the problem is with the mbed code or the configuration on the PC \$\endgroup\$ – 19172281 Mar 26 at 11:55
  • \$\begingroup\$ Nope, sorry, but I don't use that board. \$\endgroup\$ – Scott Seidman Mar 26 at 12:06
  • \$\begingroup\$ Do you have any comment on the IP format? \$\endgroup\$ – 19172281 Mar 26 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.