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.
6 years, 4 months ago.
How to correct TCPServer and accept is depreciated
Mbed-OS-example gives depreciated warning on 'TCPServer' and 'accept' (lines 15 and 29)
https://os.mbed.com/teams/mbed_example/code/mbed-os-example-tcp-server/file/ddb5698aa782/main.cpp/
mbed-os-example-tcp-server
#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPServer.h"
#include "TCPSocket.h"
int main()
{
printf("TCP server example\n");
EthernetInterface eth;
eth.connect();
printf("The Server IP address is '%s'\n", eth.get_ip_address());
TCPServer srv;
TCPSocket client_sock;
SocketAddress client_addr;
char *buffer = new char[256];
/* Open the server on ethernet stack */
srv.open(ð);
/* Bind the HTTP port (TCP 80) to the server */
srv.bind(eth.get_ip_address(), 23);
/* Can handle x simultaneous connections */
srv.listen(1);
srv.accept(&client_sock, &client_addr);
printf("Accepted %s:%d\n", client_addr.get_ip_address(),
client_addr.get_port());
strcpy(buffer, "Hello \n\r");
client_sock.send(buffer, strlen(buffer));
client_sock.recv(buffer, 256);
client_sock.close();
delete[] buffer;
}
Although it does still work atm, I would like to use the correct code, can someone give me an example to to do this?
Even better could the example be updated on Mbed.
1 Answer
6 years, 4 months ago.
Hi Paul,
About the TCPServer I wrote in the comments under my answer in last question about TCP https://os.mbed.com/questions/86423/mbed-os-tcp-server-example-with-Nucleo-F/, where you wrote too.
TCPServer
#include "mbed.h"
#include "EthernetInterface.h"
//#include "TCPServer.h" // not needed anymore
//#include "TCPSocket.h" // not needed anymore
int main()
{
printf("TCP server example\n");
EthernetInterface eth;
eth.connect();
printf("The Server IP address is '%s'\n", eth.get_ip_address());
//TCPServer srv; //TCPServer was migrate to TCPSocket
TCPSocket srv;
TCPSocket *client_sock; // srv.accept() will return pointer to socket
SocketAddress client_addr;
char *buffer = new char[256];
/* Open the server on ethernet stack */
srv.open(ð);
/* Bind the HTTP port (TCP 80) to the server */
srv.bind(eth.get_ip_address(), 23);
/* Can handle x simultaneous connections */
srv.listen(1);
client_sock = srv.accept(); //return pointer of a client socket
client_sock->getpeername(&client_addr); //this will fill address of client to the SocketAddress object
printf("Accepted %s:%d\n", client_addr.get_ip_address(), client_addr.get_port());
strcpy(buffer, "Hello \n\r");
client_sock->send(buffer, strlen(buffer));
client_sock->recv(buffer, 256);
printf("Received Msg: %s\n", buffer); //this was missing in original example.
client_sock->close();
delete[] buffer;
}
Request about update of some example is better place it on github or email or pull request, maybe. However you are right, too many examples are out of date and a Mbed newbie or a beginner with an Api (I not say "You" :) ) can be confused when example and documentation say different things.
Best regards J.