Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 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.
1 Answer
7 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();
}
}
}
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 30 Sep 2018
I also have this problem. Did you find a solution?
posted by Nikolai Zimfer 10 Nov 2018If 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