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

Committer:
darran
Date:
Sat Jun 12 19:05:52 2010 +0000
Revision:
0:c4e397ba6a9d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darran 0:c4e397ba6a9d 1 #ifndef TCP_ECHO_HANDLER_H
darran 0:c4e397ba6a9d 2 #define TCP_ECHO_HANDLER_H
darran 0:c4e397ba6a9d 3
darran 0:c4e397ba6a9d 4 #include "mbed.h"
darran 0:c4e397ba6a9d 5 #include "TCPSocket.h"
darran 0:c4e397ba6a9d 6
darran 0:c4e397ba6a9d 7 // Constant: ECHO_TIMOUT
darran 0:c4e397ba6a9d 8 // The timeout period for inactivity in milliseconds
darran 0:c4e397ba6a9d 9 #define ECHO_TIMEOUT 5000
darran 0:c4e397ba6a9d 10
darran 0:c4e397ba6a9d 11 /*
darran 0:c4e397ba6a9d 12 Class: TCPEchoHandler
darran 0:c4e397ba6a9d 13 A class instantiated to handle the incoming TCP client connection
darran 0:c4e397ba6a9d 14 Extends NetService to hook into the TCP/IP stack's polling
darran 0:c4e397ba6a9d 15 and destruction service
darran 0:c4e397ba6a9d 16 */
darran 0:c4e397ba6a9d 17 class TCPEchoHandler : public NetService {
darran 0:c4e397ba6a9d 18 public:
darran 0:c4e397ba6a9d 19 // Constructor: TCPEchoHandler
darran 0:c4e397ba6a9d 20 // Setup and handle the incoming connection
darran 0:c4e397ba6a9d 21 TCPEchoHandler(TCPSocket*);
darran 0:c4e397ba6a9d 22 virtual ~TCPEchoHandler();
darran 0:c4e397ba6a9d 23 private:
darran 0:c4e397ba6a9d 24 // Variable: clientSocket
darran 0:c4e397ba6a9d 25 // The incoming TCP socket from the client
darran 0:c4e397ba6a9d 26 TCPSocket* clientSocket;
darran 0:c4e397ba6a9d 27 // Variable: closed
darran 0:c4e397ba6a9d 28 // A marker to say whether this socket is already closed
darran 0:c4e397ba6a9d 29 int closed;
darran 0:c4e397ba6a9d 30 // Variable: timeoutWatchdog
darran 0:c4e397ba6a9d 31 // A timer to countdown from during inactivity and close
darran 0:c4e397ba6a9d 32 // dormant connections
darran 0:c4e397ba6a9d 33 Timeout timeoutWatchdog;
darran 0:c4e397ba6a9d 34
darran 0:c4e397ba6a9d 35 // Function: onNetTcpSocketEvent
darran 0:c4e397ba6a9d 36 // The callback function called by the network stack whenever an
darran 0:c4e397ba6a9d 37 // event occurs on the TCP socket
darran 0:c4e397ba6a9d 38 // Parameter: e - The event that has occurred
darran 0:c4e397ba6a9d 39 void onTCPSocketEvent(TCPSocketEvent e);
darran 0:c4e397ba6a9d 40 // Function: close
darran 0:c4e397ba6a9d 41 // Closes the client socket and marks this class as done with
darran 0:c4e397ba6a9d 42 // for the TCP/IP stack to destroy
darran 0:c4e397ba6a9d 43 virtual void close();
darran 0:c4e397ba6a9d 44 // Function: setTimeout
darran 0:c4e397ba6a9d 45 // Parameter: timeout - The length of time to wait for more activity in milliseconds
darran 0:c4e397ba6a9d 46 void setTimeout(unsigned int timeout);
darran 0:c4e397ba6a9d 47 // Function: onTimeout
darran 0:c4e397ba6a9d 48 // The handler called by the timeout watchdog to close the connection when timed out
darran 0:c4e397ba6a9d 49 void onTimeout();
darran 0:c4e397ba6a9d 50 };
darran 0:c4e397ba6a9d 51
darran 0:c4e397ba6a9d 52 #endif