An Echo server as described in RFC862. Written as a learning exercise for using Donatien's network stack. Hopefully of some use to others to get started with socket programming.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EchoServer.cpp Source File

EchoServer.cpp

00001 #include "EchoServer.h"
00002 
00003 EchoServer::EchoServer() {
00004     // Create the sockets and set the callbacks
00005     tcpSock = new TCPSocket;
00006     tcpSock->setOnEvent(this, &EchoServer::onNetTcpSocketEvent);
00007     udpSock = new UDPSocket;
00008     udpSock->setOnEvent(this, &EchoServer::onNetUdpSocketEvent);
00009 }
00010 
00011 EchoServer::~EchoServer() {
00012     // Delete the sockets on destruction
00013     delete tcpSock;
00014     delete udpSock;
00015 }
00016 
00017 void EchoServer::bind(int tcpPort, int udpPort) {
00018     // bind and listen on TCP
00019     tcpSock->bind(Host(IP_ADDR_ANY, tcpPort));
00020     tcpSock->listen();
00021     // bind UDP
00022     udpSock->bind(Host(IP_ADDR_ANY, udpPort));
00023 }
00024 
00025 void EchoServer::onNetTcpSocketEvent(TCPSocketEvent e) {
00026     // We're only interested in the ACCEPT event where we need to accept
00027     // the incoming connection
00028     if ( e == TCPSOCKET_ACCEPT ) {
00029         TCPSocket* tcpClientSocket;
00030         Host client;
00031         if ( tcpSock->accept(&client, &tcpClientSocket) ) {
00032             printf("onNetTcpSocketEvent : Could not accept connection.\r\n");
00033             return; //Error in accept, discard connection
00034         }
00035         // We can find out from where the connection is coming by looking at the
00036         // Host parameter of the accept() method
00037         IpAddr clientIp = client.getIp();
00038         printf("Incoming TCP connection from %d.%d.%d.%d\r\n", clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
00039         // Create TCPEchoHandler and pass client socket
00040         TCPEchoHandler* tcpHandler = new TCPEchoHandler(tcpClientSocket); //TCPSocket ownership is passed to handler
00041         // The handler object will destroy itself when done, or will be destroyed on Server destruction
00042     }
00043 }
00044 
00045 void EchoServer::onNetUdpSocketEvent(UDPSocketEvent e) {
00046     // We're only interested in the READABLE event (it's the only one)
00047     if ( e == UDPSOCKET_READABLE ) {
00048         // No need for a handler for UDP - set up a buffer and check client
00049         char buff[128];
00050         Host client;
00051         IpAddr clientIp = client.getIp();
00052         printf("Incoming UDP connection from %d.%d.%d.%d\r\n", clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
00053         // Keep reading while there's data to be read
00054         while ( int len = udpSock->recvfrom(buff, 128, &client) ) {
00055             if ( len > 0 )
00056                 // If there's data, send it straight back out
00057                 udpSock->sendto(buff, len, &client);
00058         }
00059     }
00060 }