TCPTest Server attempt

Dependencies:   EthernetNetIf mbed

Files at this revision

API Documentation at this revision

Comitter:
OTBsolar
Date:
Tue Mar 15 16:08:17 2011 +0000
Parent:
0:f8aba92e4c46
Commit message:
slightly less beta

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f8aba92e4c46 -r 7785624d8f35 main.cpp
--- a/main.cpp	Tue Mar 15 11:50:38 2011 +0000
+++ b/main.cpp	Tue Mar 15 16:08:17 2011 +0000
@@ -9,16 +9,80 @@
 TCPSocket tcp;  //The listening port where requests are queued
 TCPSocket* link; //The port where accepted requests can communicate
 
+Host local(IpAddr(130,144,2,83), 12345); //mbed IP
+Host client;
+
+TCPSocketErr accErr;
+
+void onLinkSocketEvent(TCPSocketEvent e)
+{
+   switch(e)
+    {
+    case TCPSOCKET_CONNECTED:
+        printf("TCP Socket Connected\r\n");
+        break;
+    case TCPSOCKET_WRITEABLE:
+      //Can now write some data...
+        printf("TCP Socket Writable\r\n");
+        break;
+    case TCPSOCKET_READABLE:
+      //Can now read dome data...
+        printf("TCP Socket Readable\r\n");
+       // Read in any available data into the buffer
+       char buff[128];
+       while ( int len = link->recv(buff, 128) ) {
+       // And send straight back out again
+           link->send(buff, len);
+           buff[len]=0; // make terminater
+           printf("Received&Wrote:%s\r\n",buff);
+       }
+       break;
+    case TCPSOCKET_CONTIMEOUT:
+        printf("TCP Socket Timeout\r\n");
+        break;
+    case TCPSOCKET_CONRST:
+        printf("TCP Socket CONRST\r\n");
+        break;
+    case TCPSOCKET_CONABRT:
+        printf("TCP Socket CONABRT\r\n");
+        break;
+    case TCPSOCKET_ERROR:
+        printf("TCP Socket Error\r\n");
+        break;
+    case TCPSOCKET_DISCONNECTED:
+    //Close socket...
+        printf("TCP Socket Disconnected\r\n");        
+        link->close();
+        break;
+    default:
+        printf("DEFAULT\r\n"); 
+      }
+}
+
 void onTCPSocketEvent(TCPSocketEvent e)
 {
-    switch(e)
-    {
+    switch(e) {
         case TCPSOCKET_CONNECTED:
             printf("Connected\n");
         break;
         
-        case TCPSOCKET_ACCEPT:
-            printf("Accepted\n");
+        case TCPSOCKET_ACCEPT: {
+            accErr = tcp.accept(&client,&link);
+            switch(accErr) {
+                case TCPSOCKET_SETUP:   printf("Err:Setup\n");      break;  //TCPSocket not properly configured.
+                case TCPSOCKET_TIMEOUT: printf("Err:Timeout\n");    break;  //Connection timed out.
+                case TCPSOCKET_IF:      printf("Err:Interface\n");  break;  //Interface has problems, does not exist or is not initialized.
+                case TCPSOCKET_MEM:     printf("Err:Memory\n");     break;  //Not enough mem.
+                case TCPSOCKET_INUSE:   printf("Err:In use\n");     break;  //Interface / Port is in use.
+                case TCPSOCKET_EMPTY:   printf("Err:Empty\n");      break;  //Connections queue is empty.
+                case TCPSOCKET_RST:     printf("Err:Reset\n");      break;  //Connection was reset by remote host.
+                case TCPSOCKET_OK:      printf("Accepted: ");       break;  //Success. 
+            }
+            link->setOnEvent(&onLinkSocketEvent);
+            IpAddr clientIp = client.getIp();
+            printf("Incoming TCP connection from %d.%d.%d.%d\r\n", 
+            clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
+        }
         break;
         
         case TCPSOCKET_READABLE:
@@ -64,13 +128,38 @@
     printf("Setup OK\n");
     //****End of basic setup*****
     
-    tcp.setOnEvent(&onTCPSocketEvent); //Generate method
-    Host local(IpAddr(130,144,3,9), 12345); //mbed IP
-    Host client;
-  
+    tcp.setOnEvent(&onTCPSocketEvent); //Generate method to deal with requests
+    
+    //Bind to local port
+    printf("Init bind..\n");
+    TCPSocketErr bindErr = tcp.bind(local);
+    switch(bindErr) {
+    case TCPSOCKET_SETUP:   printf("Err:Setup\n");      break;  //TCPSocket not properly configured.
+    case TCPSOCKET_TIMEOUT: printf("Err:Timeout\n");    break;  //Connection timed out.
+    case TCPSOCKET_IF:      printf("Err:Interface\n");  break;  //Interface has problems, does not exist or is not initialized.
+    case TCPSOCKET_MEM:     printf("Err:Memory\n");     break;  //Not enough mem.
+    case TCPSOCKET_INUSE:   printf("Err:In use\n");     break;  //Interface / Port is in use.
+    case TCPSOCKET_EMPTY:   printf("Err:Empty\n");      break;  //Connections queue is empty.
+    case TCPSOCKET_RST:     printf("Err:Reset\n");      break;  //Connection was reset by remote host.
+    case TCPSOCKET_OK:      printf("Bound to port\n");  break;  //Success. 
+    }
+            
+    //Listen to local port
+    printf("Init listen..\n");
+    TCPSocketErr listenErr = tcp.listen();
+    switch(listenErr) {
+    case TCPSOCKET_SETUP:   printf("Err:Setup\n");      break;  //TCPSocket not properly configured.
+    case TCPSOCKET_TIMEOUT: printf("Err:Timeout\n");    break;  //Connection timed out.
+    case TCPSOCKET_IF:      printf("Err:Interface\n");  break;  //Interface has problems, does not exist or is not initialized.
+    case TCPSOCKET_MEM:     printf("Err:Memory\n");     break;  //Not enough mem.
+    case TCPSOCKET_INUSE:   printf("Err:In use\n");     break;  //Interface / Port is in use.
+    case TCPSOCKET_EMPTY:   printf("Err:Empty\n");      break;  //Connections queue is empty.
+    case TCPSOCKET_RST:     printf("Err:Reset\n");      break;  //Connection was reset by remote host.
+    case TCPSOCKET_OK:      printf("Listening\n");      break;  //Success. 
+    }
     Timer tmr;
     tmr.start();
-    while(true)
+    while(1)
     {
         Net::poll();
         if(tmr.read() > 2)
@@ -78,35 +167,8 @@
             tmr.reset();
             led1=!led1; //Show that we are alive
             
-            //Bind to local port
-            printf("Init bind..\n");
-            TCPSocketErr bindErr = tcp.bind(local);
-            if(bindErr == 0) {
-                printf("Bound to port\n");
-            }
-            else {
-                printf("Error %i\n",bindErr);
-            }
-            
-            //Listen to local port
-            printf("Init listen..\n");
-            TCPSocketErr listenErr = tcp.listen();
-            if(listenErr == 0) {
-                printf("Listening to port");
-            }
-            else {
-                printf("Error %i\n",listenErr);
-            }
-            
             //Wait for a connection request
-            printf("waiting for client on port 12345");
-            TCPSocketErr accErr = tcp.accept(&local,&link);
-            if(accErr == 0) {
-                printf("Connection accepted\n");
-            }
-            else {
-                printf("Error opening connection\n");
-            }
+            printf("waiting for client on port 12345\n");
         }
     }
 }
\ No newline at end of file