A new object oriented network api that can be used to replace the one provided by the EthernetInterface library.

Dependents:   NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more

Object oriented network interface for the mbed platform

Currently implemented:

  • Address
  • Endpoint
  • UDP Socket
  • TCP Socket
  • Databuffer
  • Select API

It depends on the EthernetInterface for the lwip network stack.

Please do not hesitate to contact me with any remarks, improvements or questions.

The API is also available for unix at GitHub: LibNosa

Examples

Revision:
3:d30db8752485
Parent:
2:e283a0062097
Child:
6:847a0b218e22
--- a/udp/socket.cpp	Tue Jul 17 18:31:22 2012 +0000
+++ b/udp/socket.cpp	Wed Jul 18 11:22:37 2012 +0000
@@ -46,31 +46,6 @@
 }
 
 int
-Socket::bind(int port)
-{
-    // Check socket status
-    if (this->_status != Socket::Open) {
-        return -1;
-    }
-    
-    // Update local endpoint and create native
-    struct sockaddr_in native_endpoint;
-    this->_local_endpoint.setAddress(ip::Address(ip::Address::Any));
-    this->_local_endpoint.setPort(port);
-    this->_local_endpoint.toNative(&native_endpoint);
-    
-    // Bind socket to endpoint
-    if (::bind(this->_socket,
-        (const struct sockaddr *)&native_endpoint,
-        sizeof(native_endpoint)) < 0)
-    {
-        return -1;
-    }
-
-    return 0;
-}
-
-int
 Socket::send(void *data, size_t size, ip::Address &address, int port)
 {
     ip::Endpoint endpoint(address, port);
@@ -109,7 +84,13 @@
 }
 
 int
-Socket::reveive(void *data, size_t max_size, ip::Endpoint &endpoint)
+Socket::receive(void *data, size_t max_size)
+{
+    return this->receive(data, max_size, this->_remote_endpoint);
+}
+
+int
+Socket::receive(void *data, size_t max_size, ip::Endpoint &endpoint)
 {
     // Check data buffer and size
     if (data == NULL || max_size == 0) {
@@ -122,8 +103,8 @@
     }
 
     // Create native endpoint
-    int native_endpoint_size;
     struct sockaddr_in native_endpoint;
+    int native_endpoint_size = sizeof(native_endpoint);
     std::memset(&native_endpoint, 0, sizeof(native_endpoint));
     
     // Update status
@@ -141,11 +122,15 @@
         return -1;
     }
     
-    // Copy native endpoint
-    if (native_endpoint_size == sizeof(native_endpoint)) {
-        endpoint.fromNative(&native_endpoint);
+    // Check if we received the endpoint information correctly
+    if (native_endpoint_size != sizeof(native_endpoint)) {
+        printf("Warning: invalid endpoint size received\n\r");
+        return bytes_read;
     }
     
+    // Copy remote endpoint information
+    endpoint.fromNative(&native_endpoint);
+    
     // Return bytes read
     return bytes_read;
 }