Ethernet interface for W5500 with bug fixed in socket::close()

Fork of EthernetInterfaceW5500 by W5500-Ethernet-Interface Makers

Files at this revision

API Documentation at this revision

Comitter:
Bongjun
Date:
Mon Jul 21 05:12:11 2014 +0000
Parent:
11:274fda7476d0
Child:
13:6f469f76a76a
Commit message:
modify accept() function for multiful connections.

Changed in this revision

Socket/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.h Show annotated file Show diff for this revision Revisions of this file
--- a/Socket/TCPSocketServer.cpp	Thu Jul 17 07:13:27 2014 +0000
+++ b/Socket/TCPSocketServer.cpp	Mon Jul 21 05:12:11 2014 +0000
@@ -23,6 +23,7 @@
 // Server initialization
 int TCPSocketServer::bind(int port)
 {
+    listen_port = port;
     if (_sock_fd < 0) {
         _sock_fd = eth->new_socket();
         if (_sock_fd < 0) {
@@ -71,8 +72,21 @@
     char host[16];
     snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
     uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
+
+    // change this server socket to connection socket.
     connection._sock_fd = _sock_fd;
     connection._is_connected = true;
     connection.set_address(host, port);
+
+    // and then, for the next connection, server socket should be assigned new one.
+    _sock_fd = -1; // want to assign new available _sock_fd.
+    if(bind(listen_port) < 0) {
+        printf("No more server socket");
+    } else {
+        //return -1;
+        if(listen(1) < 0) {
+            printf("No more server socket");
+        }
+    }
     return 0;
 }
--- a/Socket/TCPSocketServer.h	Thu Jul 17 07:13:27 2014 +0000
+++ b/Socket/TCPSocketServer.h	Mon Jul 21 05:12:11 2014 +0000
@@ -23,30 +23,34 @@
 
 /** TCP Server.
   */
-class TCPSocketServer : public Socket {
-  public:
+class TCPSocketServer : public Socket
+{
+public:
     /** Instantiate a TCP Server.
     */
     TCPSocketServer();
-    
+
     /** Bind a socket to a specific port.
     \param port The port to listen for incoming connections on.
     \return 0 on success, -1 on failure.
     */
     int bind(int port);
-    
+
     /** Start listening for incoming connections.
     \param backlog number of pending connections that can be queued up at any
                    one time [Default: 1].
     \return 0 on success, -1 on failure.
     */
     int listen(int backlog=1);
-    
+
     /** Accept a new connection.
     \param connection A TCPSocketConnection instance that will handle the incoming connection.
     \return 0 on success, -1 on failure.
     */
     int accept(TCPSocketConnection& connection);
+private :
+    int listen_port;
+
 };
 
 #endif