Example program with HTTPServer and sensor data streaming over TCPSockets, using Donatien Garnier's Net APIs and services code on top of LWIP. Files StreamServer.h and .cpp encapsulate streaming over TCPSockets. Broadcast is done by sendToAll(), and all incoming data is echoed back to the client. Echo code can be replaced with some remote control of the streaming interface. See main() that shows how to periodically send some data to all subscribed clients. To subscribe, a client should open a socket at <mbed_ip> port 123. I used few lines in TCL code to set up a quick sink for the data. HTTP files are served on port 80 concurrently to the streaming.

Dependencies:   mbed

Revision:
1:3ee499525aa5
Parent:
0:e614f7875b60
--- a/if/lwip/lwipNetTcpSocket.cpp	Sat Jun 12 06:01:50 2010 +0000
+++ b/if/lwip/lwipNetTcpSocket.cpp	Mon Jun 14 03:24:33 2010 +0000
@@ -33,7 +33,7 @@
 LwipNetTcpSocket::LwipNetTcpSocket(tcp_pcb* pPcb /*= NULL*/) : m_pPcb(pPcb), m_lpInNetTcpSocket(), //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
 m_pReadPbuf(NULL)
 {
-  DBG("\r\nNew NetTcpSocket %p\r\n", (void*)this);
+  DBG("New NetTcpSocket %p\r\n", (void*)this);
   if(!m_pPcb)
     m_pPcb = tcp_new();
   if(m_pPcb)
@@ -46,7 +46,7 @@
     tcp_err( (tcp_pcb*) m_pPcb, LwipNetTcpSocket::sErrCb );
     //Connected callback is defined in connect()
     //Accept callback is defined in listen()
-    DBG("\r\nNetTcpSocket created.\r\n");
+    DBG("NetTcpSocket created.\r\n");
   }
 }
 
@@ -156,7 +156,7 @@
   
 /*  if(*ppNewNetTcpSocket == NULL)
   {
-    DBG("\r\nNot enough mem, socket dropped in LwipNetTcpSocket::accept.\r\n");
+    DBG("Not enough mem, socket dropped in LwipNetTcpSocket::accept.\r\n");
     tcp_abort(pInPcb);
   }*/
   
@@ -253,7 +253,7 @@
 
 NetTcpSocketErr LwipNetTcpSocket::close()
 {
-  //DBG("\r\nLwipNetTcpSocket::close() : Closing...\r\n");
+  //DBG("Closing...\r\n");
 
   if(m_closed)
     return NETTCPSOCKET_OK; //Already being closed
@@ -267,13 +267,13 @@
  
   if( !!tcp_close( (tcp_pcb*) m_pPcb) )
   {
-    DBG("\r\nLwipNetTcpSocket::close() could not close properly, abort.\r\n");
+    DBG("could not close properly, abort.\r\n");
     tcp_abort( (tcp_pcb*) m_pPcb);
     m_pPcb = NULL;
     return NETTCPSOCKET_MEM;
   }
   
-  DBG("\r\nLwipNetTcpSocket::close() : connection closed successfully.\r\n");
+  DBG("connection closed successfully.\r\n");
   
   m_pPcb = NULL;
   return NETTCPSOCKET_OK;
@@ -291,7 +291,7 @@
 {
   if(err)
   {
-    DBG("\r\nError %d in LwipNetTcpSocket::acceptCb.\r\n", err);
+    DBG("Error %d in LwipNetTcpSocket::acceptCb.\r\n", err);
     return err;
   }
   //FIXME: MEM Errs
@@ -300,7 +300,7 @@
   
   if(pNewNetTcpSocket == NULL)
   {
-    DBG("\r\nNot enough mem, socket dropped in LwipNetTcpSocket::acceptCb.\r\n");
+    DBG("Not enough mem, socket dropped in LwipNetTcpSocket::acceptCb.\r\n");
     tcp_abort(newpcb);
     return ERR_ABRT;
   }
@@ -322,7 +322,7 @@
 
 void LwipNetTcpSocket::errCb(err_t err)
 {
-  DBG("\r\nNetTcpSocket %p - Error %d in LwipNetTcpSocket::errCb.\r\n", (void*)this, err);
+  DBG("NetTcpSocket %p - Error %d in LwipNetTcpSocket::errCb.\r\n", (void*)this, err);
   //WARN: At this point, m_pPcb has been freed by lwIP
   m_pPcb = NULL;
   //These errors are fatal, discard all events queued before so that the errors are handled first
@@ -337,7 +337,7 @@
   
 err_t LwipNetTcpSocket::sentCb(tcp_pcb* tpcb, u16_t len)
 {
-//  DBG("\r\n%d bytes ACKed by host.\r\n", len);
+//  DBG("%d bytes ACKed by host.\r\n", len);
   queueEvent(NETTCPSOCKET_WRITEABLE);
   return ERR_OK;
 }
@@ -345,7 +345,7 @@
 err_t LwipNetTcpSocket::recvCb(tcp_pcb* tpcb, pbuf *p, err_t err)
 {
   //Store pbuf ptr
- // DBG("\r\nReceive CB with err = %d & len = %d.\r\n", err, p->tot_len);
+ // DBG("Receive CB with err = %d & len = %d.\r\n", err, p->tot_len);
 //  tcp_recved( (tcp_pcb*) m_pPcb, p->tot_len); //Acknowledge the reception
   
   if(err)
@@ -355,7 +355,7 @@
   }
   else if(!p)
   {
-    DBG("\r\nNetTcpSocket %p - Connection closed by remote host (LwipNetTcpSocket::recvCb).\r\n", (void*)this);
+    DBG("NetTcpSocket %p - Connection closed by remote host (LwipNetTcpSocket::recvCb).\r\n", (void*)this);
     //Buf is NULL, that means that the connection has been closed by remote host
     
     //FIX: 27/05/2010: We do not want to deallocate the socket while some data might still be readable
@@ -398,7 +398,7 @@
   
   if( m_pReadPbuf )
   {
-    DBG("\r\nDeallocating unread data.\r\n");
+    DBG("Deallocating unread data.\r\n");
     pbuf_free((pbuf*)m_pReadPbuf); //Free all unread data
     m_pReadPbuf = NULL;
     recv(NULL,0); //Update recv ptr position
@@ -423,7 +423,7 @@
 {
   if( !arg )
   {
-    DBG("\r\nNetTcpSocket - Error %d in LwipNetTcpSocket::sErrCb.\r\n", err);
+    DBG("NetTcpSocket - Error %d in LwipNetTcpSocket::sErrCb.\r\n", err);
     return; //The socket has been destroyed, discard error
   }
   LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;