A http client sample program.

Dependencies:   NyFileSystems libMiMic mbed-rtos mbed

Fork of TcpSocketClientSamlpe by Ryo Iizuka

Revision:
25:1a4f620b7af6
Parent:
24:83a1d2bc8709
Child:
26:f58dc24e2c1b
--- a/main.cpp	Fri Sep 27 12:52:34 2013 +0000
+++ b/main.cpp	Wed Oct 02 08:34:02 2013 +0000
@@ -1,7 +1,8 @@
 /**
  * @file
- * Websocket server sample.<br/>
- * This program is websocket server template.
+ * TCP client socket sample.<br/>
+ * This program is to test of TCP client.
+ * Connect to a TCP server, and send back the received data as is.
  * 
  */
 #include "mbed.h"
@@ -12,69 +13,70 @@
 #include "fsdata.h"
 
 
-//local filesystem
-LocalFileSystem2 lf("local");
-
 NetConfig cfg; //create network configulation
 Net* net;
 
-/**
- * Httpd for UPnPService and presentation.
- */
-class WebSocketHttpd:public MiMic::Httpd
-{
-private:
-    ModLocalFileSystem modlocal;
-    ModWebSocket modwebsocket;
-    ModRomFiles modromfs; //ROM file module    
-public:
-    WebSocketHttpd(NetConfig& i_cfg):Httpd(i_cfg.getHttpPort())
-    {
-        //prepare fs data (presentation.html,icon,image.)
-        this->modromfs.setParam("rom",FSDATA,3);
-        this->modlocal.setParam("local");
-        //bind websocket module.
-        this->modwebsocket.setParam("ws");
-    }
-    virtual void onRequest(HttpdConnection& i_connection)
-    {
-        //try to ModRomFS module. for icon,images.
-        if(this->modromfs.execute(i_connection)){
-            return;
-        }
-        //try to ModLocalFileSystem
-        if(this->modlocal.execute(i_connection)){
-            return;
-        }
-        //try to Websocket service.
-        if(this->modwebsocket.execute(i_connection)){
-            //send AIN1 value every 10ms.
-            AnalogIn ain(p20);
-            do{
-                Thread::wait(20);
-            }while(this->modwebsocket.writeF("%d,",(int)(ain*4096)));
-            this->modwebsocket.close();
-            return;
-        }
-        //Otherwise, Send the redirect response to /rom/index.html
-        i_connection.sendHeader(302,
-            "text/html",
-            "Status: 302:Moved Temporarily\r\n"
-            "Location: /rom/index.html\r\n");     
-    }
-};
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
 
 
 int main()
 {
     net=new Net();//Net constructor must be created after started RTOS
+
     // manual setting
     cfg.setIpAddr(192,168,128,39);
     cfg.setNetMask(255,255,255,0);
     cfg.setGateway(192,168,128,254);    
 
-    WebSocketHttpd httpd(cfg); //create a httpd instance.
+
+    // Create tcp socket with 512 bytes RX buffer.
+    // Socket must create between "net.start" with "new Net()"
+    TcpSocket socket(512);
+    
+    //Start network
     net->start(cfg);
-    httpd.loop();  //start httpd loop.
+
+
+    led1=1;    
+    for(;;){
+        //connect to server
+        if(!socket.connect(IpAddr(192,168,128,195),1234)){
+            Thread::wait(1000);
+        }
+        //connected!
+        led2=1;
+        for(;;){
+            led4=0;
+            led3=1;
+            //wait for data...
+            const void* rx;
+            //get read pointer
+            int l=socket.precv(rx);
+            if(l<0){
+                break;
+            }
+            if(l==0){
+                //timeout
+            }else{
+                //ok,echo back data.
+                led4=1;
+                //send data
+                if(!socket.send(rx,l)){
+                    break;
+                }
+                //move read pointer.
+                socket.pseek(l);
+            }
+            led3=0;
+        }
+        led2=0;
+        led3=0;
+        led4=0;
+        socket.close(); //close the socket.
+    }
     return 0;
 }
+