Nenad Jovicic / Mbed OS mbed-os-PMK2018_tcp_server
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #if !FEATURE_LWIP
00002     #error [NOT_SUPPORTED] LWIP not supported for this target
00003 #endif
00004 
00005 #include "mbed.h"
00006 #include "EthernetInterface.h"
00007 #include "TCPServer.h"
00008 #include "TCPSocket.h"
00009 
00010 int main()
00011 {
00012     printf("Basic TCP server example\n");
00013     
00014     EthernetInterface eth;
00015     eth.connect();
00016     
00017     printf("The target IP address is '%s'\n", eth.get_ip_address());
00018     
00019     TCPServer srv;
00020     TCPSocket clt_sock;
00021     SocketAddress clt_addr;
00022     
00023     /* Open the server on ethernet stack */
00024     srv.open(&eth);
00025     
00026     /* Bind the HTTP port (TCP 80) to the server */
00027     srv.bind(eth.get_ip_address(), 80);
00028     
00029     /* Can handle 5 simultaneous connections */
00030     srv.listen(5);
00031     
00032     while (true) {
00033         srv.accept(&clt_sock, &clt_addr);
00034         printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
00035         char rbuffer[64];
00036         int rcount = clt_sock.recv(rbuffer, sizeof rbuffer);
00037         printf("received data %s\n", rbuffer);
00038         clt_sock.send(rbuffer, rcount);
00039     }
00040 }