6 years, 11 months ago.

Can I increase the number of active TCP connections from 3 to 5 with the latest mbed-os?

I modified the example program (below) from the mbed site and I am running this on K64F board. I am using the latest mbed-os. I can get 3 active connections. How do I increase the maximum number of active connections to 5?

include the mbed-os library with this snippet

#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPServer.h"
#include "TCPSocket.h"

#define MAXCLIENTS  5

Serial pc(USBTX, USBRX);

int main()
{
    pc.baud(115200);
    pc.printf("TCP server example\r\n");
    
    EthernetInterface eth;
    eth.connect();
    
    pc.printf("The Server IP address is '%s'\r\n", eth.get_ip_address());
        
    TCPServer srv;
    TCPSocket client_sock[MAXCLIENTS];
    SocketAddress client_addr[MAXCLIENTS];
    
    int connectionStatus[MAXCLIENTS];
    
    char *buffer = new char[256];
    
    /* Open the server on ethernet stack */
    srv.open(&eth);
    
    /* Bind the HTTP port (TCP 80) to the server */
    srv.bind(eth.get_ip_address(), 80);
    
    /* Can handle x simultaneous connections */
    srv.listen(MAXCLIENTS);
    srv.set_blocking(false);

    int numClient = 0;
    int rcount = 0;
    int scount = 0;
    
    // initialize connection Status for client
    for(int i=0;i<MAXCLIENTS;i++)
        connectionStatus[i] = 0;
    
    while(1)
    {
        for(int i=0;i < MAXCLIENTS;i++)
        {
            // if not connected accept connection
            if (connectionStatus[i] == 0)
            {
                if(srv.accept(&client_sock[i], &client_addr[i]) == 0)
                {  
                    connectionStatus[i] = 1;
                    client_sock[i].set_blocking(false);          
                    numClient++;                    
                    pc.printf("Client Accepted %s:%d,%d\r\n", client_addr[i].get_ip_address(),client_addr[i].get_port(),numClient);
                }
            }
            
            // if connected        
            if (connectionStatus[i] == 1)
            {                                             
                rcount = client_sock[i].recv(buffer, sizeof(buffer));
                if(rcount > 0)
                {
                    sprintf(buffer, "Hello Client %s:%d\r\n", client_addr[i].get_ip_address(),client_addr[i].get_port());
                    scount = client_sock[i].send(buffer, strlen(buffer));
                }
                if(rcount == 0) // client disconnected
                {
                    connectionStatus[i] = 0;
                    pc.printf("Client Disconnected %s:%d\r\n", client_addr[i].get_ip_address(),client_addr[i].get_port());
                    client_sock[i].close();
                    numClient--;                
                }                                                        
                if(scount < 0) // unable to send data
                {
                    connectionStatus[i] = 0;
                    pc.printf("Connection lost %s:%d\r\n", client_addr[i].get_ip_address(),client_addr[i].get_port());
                    client_sock[i].close();
                    numClient--;                
                }                                                        
            }
        }

        wait(0.5f);
    }

}
Be the first to answer this question.