SimpleSocket 1.0 examples

Dependencies:   EthernetNetIf SimpleSocket 1.0 mbed

Revision:
3:464dd710e6f6
Parent:
2:304672a01127
Child:
4:59056313fbfa
--- a/main.cpp	Tue Aug 16 06:56:48 2011 +0000
+++ b/main.cpp	Tue Aug 16 08:37:12 2011 +0000
@@ -2,113 +2,10 @@
 #include "EthernetNetIf.h"
 #include "TCPSocket.h"
 #include "SocketUtil.h"
-
-class ClientSocket {
-public:
-    ClientSocket(TCPSocket *socket) : socket(socket) {
-        socket->setOnEvent(this, &ClientSocket::onClientSocketEvent);
-    }
-
-    bool connected() {
-        if (socket)
-            Net::poll();
-        return socket != 0;
-    }
-
-    void onClientSocketEvent(TCPSocketEvent e) {
-        SocketEvent event = e;
-        printf(event);
-
-        switch (event) {
-            case TCPSOCKET_READABLE:
-                char buf[128];
-                while (int len = socket->recv(buf, 128)) {
-                    // And send straight back out again
-                    socket->send(buf, len);
-                    buf[len] = 0; // make terminater
-                    printf("Received & Wrote: %s\n",buf);
-                }
-                break;
-            case TCPSOCKET_DISCONNECTED:
-                socket->close();
-                delete socket;
-                socket = 0;
-                break;
-        }
-    }
-
-private:
-    TCPSocket *socket;
-};
-
-class ServerSocket {
-public:
-    ServerSocket(int port) : port(port), ssocketAcceptEvent(false) {}
-
-    void init() {
-        EthernetNetIf eth;
-        EthernetErr ethErr = eth.setup();
-        if (ethErr) error("Error %d in setup.\n", ethErr);
-
-        ssocket.setOnEvent(this, &ServerSocket::onTCPSocketEvent);
-
-        SocketError bindErr = ssocket.bind(Host(IpAddr(), port));
-        printf("Init bind... %s", bindErr.toString());
+#include "ClientSocket.h"
+#include "ServerSocket.h"
 
-        SocketError listenErr = ssocket.listen();
-        printf("Init listen... %s", listenErr.toString());
-    }
-
-
-    void onTCPSocketEvent(TCPSocketEvent e) {
-        SocketEvent event = e;
-        printf(event);
-
-        switch (event) {
-            case TCPSOCKET_ACCEPT:
-                ssocketAcceptEvent = true;
-                //if (SocketError err = ssocket.accept(&client, &link)) {
-                //    printf("accept:%s", err.toString());
-                //} else {
-                //    link->setOnEvent(this, &ServerSocket::onLinkSocketEvent);
-                //    IpAddr ip = client.getIp();
-                //    printf("Incoming TCP connection from %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
-                //}
-                break;
-
-            case TCPSOCKET_DISCONNECTED:
-                ssocket.close();
-                break;
-        }
-    }
-
-    ClientSocket *accept() {
-        Net::poll();
-
-        if (ssocketAcceptEvent) {
-            ssocketAcceptEvent = false;
-            TCPSocket* socket;
-            Host host;
-            SocketError err = ssocket.accept(&host, &socket);
-
-            if (!err) {
-                ClientSocket *clientSocket = new ClientSocket(socket);
-                IpAddr ip = host.getIp();
-                printf("TCP connection from %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
-                return clientSocket;
-            } else {
-                printf("accept:%s", err.toString());
-            }
-        }
-
-        return NULL;
-    }
-
-private:
-    int port;
-    TCPSocket ssocket;   //The listening port where requests are queued
-    bool ssocketAcceptEvent;
-};
+DigitalOut led1(LED1);
 
 int main() {
     DigitalOut led1(LED1);
@@ -122,6 +19,10 @@
     Net::poll();
         if (ClientSocket *clientSocket = server.accept()) {
             while (clientSocket->connected()) {
+            int c = clientSocket->read();
+                if (c != -1)
+                    clientSocket->write((char) c);
+            
                 if (timer.read() > 10) {
                     timer.reset();
                     timer.start();