6 years, 1 month ago.

How can I get the TCPSocketserver to work?

Hi i can't seem to get a simple socket server to work:

This is my code:

#include "mbed.h"
#include "EthernetInterface.h"
 
#define SERVER_PORT   80
  
EthernetInterface iface;
TCPSocket server;
Socket *client;
 
int main (void) {
    iface.connect();
    printf("\nServer IP Address is %s\n\r", iface.get_ip_address());
    
    server.open(&iface);
    server.bind(SERVER_PORT);
    server.listen();
    
    while (true) {
        printf("Server bound and listening\n");

        while (true) {
            client = server.accept();
    
            printf("Client connected, stack at 0x%08lX\n", client);
        
            char buffer[1024];
            int n; //= client->recv(buffer, sizeof(buffer));
            printf("Received %u bytes from remote host\n", n);
            
            // print received message to terminal
            // buffer[n] = '\0';
            // printf("Received message from Client :'%s'\n",buffer);
    
            //client->close();
        }
    }
}

The server does accept a connection from the remote host but the pointer to the client is a NULL pointer. I've been pulling my few remaining hears for about a day now ... Can someone please give me some pointers?

Kind regards,

Koen.

I also have this problem. Did you find a solution?

posted by Nikolai Zimfer 10 Nov 2018

If you update the mbed os at least the accept () function works. But last time I checked close() caused a run time error.

posted by Koen Kempeneers 10 Nov 2018

1 Answer

6 years, 1 month ago.

Hello Koen,

I hope that the following code will save your hair:

#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPServer.h"
#include "TCPSocket.h"

#define SERVER_PORT 80

EthernetInterface   iface;
TCPServer           server;
TCPSocket           client;
SocketAddress       clientAddress;

int main(void)
{
    iface.connect();
    printf("\nServer IP Address is %s\n\r", iface.get_ip_address());

    server.open(&iface);
    server.bind(iface.get_ip_address(), SERVER_PORT);
    server.listen(5);
    
    while (true) {
        printf("Server bound and listening\n");

        while (true) {
            server.accept(&client, &clientAddress);
            printf("Connection succeeded!\n\rClient's IP address: %s\n\r", clientAddress.get_ip_address());

            char    buffer[1024];
            int     n = client.recv(buffer, sizeof(buffer));

            printf("Received %u bytes from remote host\n", n);  //print received message to terminal
            buffer[n] = '\0';
            printf("Received message from Client :'%s'\n", buffer);

            client.close();
        }
    }
}

Accepted Answer

Thanks ! That works, but I get two warning messages from the compiler (mbed online compiler using mbed-os);

Warning: Function "TCPServer::TCPServer()" (declared at line 39 of "/extras/mbed-os.lib/features/netsocket/TCPServer.h") was declared "deprecated" in "TCPSocketServer.cpp", Line: 9, Col: 12
Warning: Function "TCPServer::accept(TCPSocket *, SocketAddress *)" (declared at line 81 of "/extras/mbed-os.lib/features/netsocket/TCPServer.h") was declared "deprecated" in "TCPSocketServer.cpp", Line: 23, Col: 21

Can you point me the correct manual pages ? I found this: obviously wrong article. https://os.mbed.com/docs/v5.10/mbed-os-api-doxy/class_t_c_p_socket.html#ac7d02ef109d2d3a48b6dcaa38869de80

Also ... How do you paste the code legible in the forum? The <<code>> <</code>> tags do not seem to work.

Thanks for your help.

posted by Koen Kempeneers 30 Sep 2018
  • It seems that something is wrong with the new network API :(
    Luckily the depricated one still works well :-)
  • To make code formattig work try to put the <<code>> <</code>> tags on their own separate lines:
<<code>>
Text of your code
<</code>>
posted by Zoltan Hudak 30 Sep 2018