This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3200 as a TCP server - multiple client receive

Hi,

I am trying to work on TCP Socket example code and trying to implement a TCP server to which 4 TCP client will connect simultaneously.

These TCP client need to connect on single TCP socket.

I am not sure how to do this, tried few ways as shown in below code snippet which shows 2 TCP clients getting connected, but data reception from client is not consistent. Data from the first TCP client which gets connected, is received consistently but sometimes eventhough data from 2nd TCP client is not sent, I get a debug message showing previous data from 2nd TCP Client.

Data from 2nd TCP client is inconsistent with above scenario.

Please let me know if the handling of TCP server with multiple client is correct or is there any other methodology to work with multiple TCP clients getting connected to TCP server.

Thanks,

Ashutosh.

Code snippet -

            // creating a TCP socket


            iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
            if( iSockID < 0 )
            {
                // error
                ASSERT_ON_ERROR(TCP_SERVER_FAILED);
            }

            iAddrSize = sizeof(SlSockAddrIn_t);

            // binding the TCP socket to the TCP server address


            iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize);
            if( iStatus < 0 )
            {
                // error
                ASSERT_ON_ERROR(sl_Close(iSockID));
                ASSERT_ON_ERROR(TCP_SERVER_FAILED);
            }



            // putting the socket for listening to the incoming TCP connection


            iStatus = sl_Listen(iSockID, 0);
            if( iStatus < 0 )
            {
                ASSERT_ON_ERROR(sl_Close(iSockID));
                ASSERT_ON_ERROR(TCP_SERVER_FAILED);
            }

            // setting socket option to make the socket as non blocking


            iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING,
                                    &lNonBlocking, sizeof(lNonBlocking));
            if( iStatus < 0 )
            {
                ASSERT_ON_ERROR(sl_Close(iSockID));
                ASSERT_ON_ERROR(TCP_SERVER_FAILED);
            }

            iNewSockID = SL_EAGAIN;
            iNewSockID2 = SL_EAGAIN;

            // waiting for an incoming TCP connection for TCP client 1


            while( iNewSockID < 0 )
            {
                // accepts a connection form a TCP client, if there is any
                // otherwise returns SL_EAGAIN
                iNewSockID = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr,
                                        (SlSocklen_t*)&iAddrSize);
                if( iNewSockID == SL_EAGAIN )
                {
                   MAP_UtilsDelay(10000);
                }
                else if( iNewSockID < 0 )
                {
                    // error
                    ASSERT_ON_ERROR(sl_Close(iNewSockID));
                    ASSERT_ON_ERROR(sl_Close(iSockID));
                    ASSERT_ON_ERROR(TCP_SERVER_FAILED);
                }
            }

            Report("Client 1 connected!");



            // waiting for an incoming TCP connection for TCP client 2


            while( iNewSockID2 < 0 )
            {
                // accepts a connection form a TCP client, if there is any
                // otherwise returns SL_EAGAIN
                iNewSockID2 = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr,
                                        (SlSocklen_t*)&iAddrSize);
                if( iNewSockID2 == SL_EAGAIN )
                {
                   MAP_UtilsDelay(10000);
                }
                else if( iNewSockID2 < 0 )
                {
                    // error
                    ASSERT_ON_ERROR(sl_Close(iNewSockID2));
                    ASSERT_ON_ERROR(sl_Close(iSockID));
                    ASSERT_ON_ERROR(TCP_SERVER_FAILED);
                }
            }

            Report("Client 2 connected!");

        }

// data receive from TCP Client 1



        SL_FD_SET(iNewSockID, &ReadFds);

        ActiveReadFds = ReadFds;
        iStatus = sl_Select(iNewSockID + 1, &ActiveReadFds, NULL, \
                                   NULL, &timeout);

        if((iStatus > 0))// && (FD_ISSET(iSockID, &ReadFds))) // Checks if current socket descriptor is set
        {
            iStatus = sl_Recv(iNewSockID, g_cBsdBuf, iTestBufLen, 0);

            UART_PRINT("Data 1 is - %x\n\r",g_cBsdBuf[0]);
          
        }

        
        
        

        // data receive from TCP Client 2



        SL_FD_SET(iNewSockID2, &ReadFds);

        ActiveReadFds = ReadFds;
        iStatus = sl_Select(iNewSockID2 + 1, &ActiveReadFds, NULL, \
                                   NULL, &timeout);

        if((iStatus > 0) && (FD_ISSET(iNewSockID2, &ReadFds))) // Checks if current socket descriptor is set
        {
            iStatus = sl_Recv(iNewSockID2, g_cBsdBuf2, iTestBufLen, 0);

            UART_PRINT("Data 2 is - %x\n\r",g_cBsdBuf2[0]);
          
        }

  • Hi Ashutosh,


    Do you mean you are receiving duplicate packets in case of 2nd TCP client? Can you please cross verify the total number of bytes received is same as total number of bytes sent in case of 2nd TCP socket.


    Regards,
    Aashish
  • Hi Aashish,

    I think I am receiving duplicate packets in case of 2nd TCP client but its not consistent. Sometimes when I send packet from TCP client 1, i get debug message with old data from TCP client 2, in this case data is correctly received from TCP client 1.

    At times, i did not receive any data from TCP client 2, but when I send data from TCP client 1, I receive previous data from TCP client 2.

    I am currently sending only 1 byte.

    Is the firmware implementation for receiving data from multiple TCP client correct?

    Thanks,

    Ashutosh.

  • Hi Ashutosh,


    We have never seen such issue for multiple TCP client. For each TCP connection, socket is different so client should not affected by other clients.

    Also we can see in your code snippet your are printing buffer without checking iStatus value. You should print only in case of iStatus > 0.




    Regards,
    Aashish
  • Hi Aashish,

    Yes rectified the problem of not checking iStatus before printing the data, now the data from TCP client 2 is not visible randomly.

    But still we are facing the problem where when data from the 1st TCP client is received its getting received properly, but when data from 2nd TCP client receive is not consistent. Further sometimes when 1st TCP client data is received, the data from 2nd TCP client is printed.

    In short -

    1.  Data from TCP client 1 - received OK

    2. Data from TCP client 2 - Not received

    3. Data from TCP client 1 - received OK, Data from TCP client 2 - received OK

    So we are not sure about our firmware implementation, will you please share pseudo code for connecting multiple TCP clients and receving data from them? That will help is understanding how to handle multiple clients.

    Thanks,

    Ashutosh.

  • Hi Ashutosh,

    Please find below code.

    int multiClientTest(unsigned short usPort)
    {
    	SlSockAddrIn_t  cliAddr;
    	SlSockAddrIn_t  sLocalAddr;
    	int             iAddrSize;
    	int             iSockID;
    	int             tempSock;
    	int             clientSock[7];
    	int             status;
    	unsigned char   buffer[1468];
    	SlFdSet_t       readFds;
    	SlFdSet_t       masterFds;
    	int             maxSock;
    	int             i;
    	int             numOfClients = 0;
    
    	iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
    	if( iSockID < 0 )
    	{
    		// error
    		ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
    	}
    
    	//filling the TCP server socket address
    	sLocalAddr.sin_family = SL_AF_INET;
    	sLocalAddr.sin_port = sl_Htons((unsigned short)usPort);
    	sLocalAddr.sin_addr.s_addr = 0;
    
    	iAddrSize = sizeof(SlSockAddrIn_t);
    
    	status = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize);
    	if( status < 0 )
    	{
    		// error
    		sl_Close(iSockID);
    		ASSERT_ON_ERROR(BIND_ERROR);
    	}
    
    	// putting the socket for listening to the incoming TCP connection
    	status = sl_Listen(iSockID, 0);
    	if(status < 0)
    	{
    		sl_Close(iSockID);
    		ASSERT_ON_ERROR(LISTEN_ERROR);
    	}
    
    	SL_FD_SET(iSockID, &masterFds);
    	maxSock= iSockID;
    
    	while (1)
    	{
    		readFds = masterFds;
    		if (7 == numOfClients)
    		{
    			SL_FD_CLR(iSockID,&readFds);
    		}
    
    		status = sl_Select(maxSock,&readFds,0,0,0);
    		if (status <= 0)
    		{
    			sl_Close(iSockID);
    			ASSERT_ON_ERROR(SELECT_ERROR);
    		}
    
    		if (SL_FD_ISSET(iSockID,&readFds))
    		{
    			// New Accept
    			tempSock = sl_Accept(iSockID, (SlSockAddr_t*)&cliAddr, (SlSocklen_t*) &iAddrSize);
    			if(tempSock < 0)
    			{
    				break;
    			}
    			else
    			{
    				UART_PRINT("Client connected Addr=%d.%d.%d.%d\n\r",
    						SL_IPV4_BYTE(cliAddr.sin_addr.s_addr,3),
    						SL_IPV4_BYTE(cliAddr.sin_addr.s_addr,2),
    						SL_IPV4_BYTE(cliAddr.sin_addr.s_addr,1),
    						SL_IPV4_BYTE(cliAddr.sin_addr.s_addr,0));
    				for (i=0 ; i<7 ; i++)
    				{
    					if (clientSock[i] < 0)
    					{
    						numOfClients++;
    						clientSock[i] = tempSock;
    						SL_FD_SET(tempSock,&masterFds);
    						if (maxSock < tempSock)
    						{
    							maxSock = tempSock;
    						}
    						break;
    					}
    				}
    			}
    		}
    
    		for (i=0 ; i<7 ; i++)
    		{
    			if (clientSock[i] >=0)
    			{
    				if (SL_FD_ISSET(clientSock[i],&readFds))
    				{
    					status = sl_Recv(clientSock[i], buffer, 1468, 0);
    					if( status < 0 )
    					{
    						// error
    						sl_Close(clientSock[i]);
    						clientSock[i] = -1;
    						numOfClients--;
    						continue;
    					}
    				}
    			}
    		}
    	}
    
    	return 0;
    }

    Regards,

    Aashish

  • Hi Aashish,

    Thanks for the code, I will check and revert back.

    Ashutosh.

  • Hi Aashish,
    Thanks for the code, it worked perfectly.

    Just to ask one more thing, how to setup fixed TCP server IP?

    Thanks,
    Ashutosh.
  • Hi Ashutosh,


    Set static IP to your device.


    Regards,
    Aashish
  • Hi Ashutosh


    We are closing this thread, for follow up queries please open a new thread and add a link to this one for reference.


    Regards,
    Aashish