TCP Server application which accepts single TCP telnet client connection

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "TCPServer.h"
00004 #include "TCPSocket.h"
00005 
00006 int main()
00007 {
00008     printf("TCP server example\n");
00009     
00010     EthernetInterface eth;
00011     eth.connect();
00012     
00013     printf("The Server IP address is '%s'\n", eth.get_ip_address());
00014     
00015     TCPServer srv;
00016     TCPSocket client_sock;
00017     SocketAddress client_addr;
00018     char *buffer = new char[256];
00019     
00020     /* Open the server on ethernet stack */
00021     srv.open(&eth);
00022     
00023     /* Bind the HTTP port (TCP 80) to the server */
00024     srv.bind(eth.get_ip_address(), 23);
00025     
00026     /* Can handle x simultaneous connections */
00027     srv.listen(1);
00028 
00029     srv.accept(&client_sock, &client_addr);
00030     printf("Accepted %s:%d\n", client_addr.get_ip_address(), 
00031                     client_addr.get_port());
00032     strcpy(buffer, "Hello \n\r");
00033     client_sock.send(buffer, strlen(buffer));
00034     client_sock.recv(buffer, 256);
00035     
00036     client_sock.close();
00037     delete[] buffer;
00038 }