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 TCP server issues after 3-4 connections

Other Parts Discussed in Thread: CC3200

Hello,

I used the TCP server connection example and changed it so  that I can use it for non-blocking applications. The CC3200 is a server in my application and I want to connect my computer with the CC3200. My solution works only for 3-4 times, then a connection is not possible.

This is the function to create a Listening Socket. After getting a Listening Socket the function createConnectingSocket looks for new connections.

int createListeningTCPsocket(unsigned short PortNumber, SlSockAddrIn_t* sLocalAddr, SlSockAddrIn_t* sAddr)
{
    int             iAddrSize;
    int             iStatus;
    int             iSockID;
    long            lNonBlocking = 1;

    //filling the TCP server socket address
    (*sLocalAddr).sin_family = SL_AF_INET;
    (*sLocalAddr).sin_port = sl_Htons((unsigned short)PortNumber);
    (*sLocalAddr).sin_addr.s_addr = 0;

    // creating a TCP socket
	MAP_UtilsDelay(80000);
    iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);


    if( iSockID < 0 )
    {
        // error
        return iStatus;
    }

    iAddrSize = sizeof(SlSockAddrIn_t);

    // binding the TCP socket to the TCP server address

	MAP_UtilsDelay(80000);
    iStatus = sl_Bind(iSockID,(SlSockAddr_t *)sLocalAddr, iAddrSize);
    if( iStatus < 0 )
    {
        // error
    	MAP_UtilsDelay(80000);
        sl_Close(iSockID);
        return iStatus;
    }

    // putting the socket for listening to the incoming TCP connection
	MAP_UtilsDelay(80000);
    iStatus = sl_Listen(iSockID, 0);

    if( iStatus < 0 )
    {
    	MAP_UtilsDelay(80000);
        sl_Close(iSockID);
        return iStatus;
    }

    // setting socket option to make the socket as non blocking
	MAP_UtilsDelay(80000);
    iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking));

    if( iStatus < 0 )
    {
    	MAP_UtilsDelay(80000);
        sl_Close(iSockID);
        return iStatus;
    }

    return iSockID;
}
int createConnectingTCPsocket(unsigned short PortNumber, int* iSockID ,SlSockAddrIn_t* sLocalAddr, SlSockAddrIn_t* sAddr)
{
    int             iAddrSize;
    int             iNewSockID;
    long            lNonBlocking = 1;

    iNewSockID = SL_EAGAIN;

    iAddrSize = sizeof(SlSockAddrIn_t);
	// accepts a connection form a TCP client, if there is any
	// otherwise returns SL_EAGAIN
	MAP_UtilsDelay(80000);
	iNewSockID = sl_Accept(*iSockID,(struct SlSockAddr_t *)sAddr,(SlSocklen_t*)&iAddrSize);


	if( iNewSockID < 0 )
	{
		// error
		MAP_UtilsDelay(80000);
		sl_Close(iNewSockID);
		return iNewSockID;
	}

    int iStatus;
	// setting socket option to make the socket as non blocking
	MAP_UtilsDelay(80000);
    iStatus = sl_SetSockOpt(iNewSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking));


	return iNewSockID;
}

Here is the part of the application where I use this functions. The network controller is freezing often if send two commands without a delay between them.

			...

                        if(tcpStatus == TCP_IP_NO_TCP_LISTENING_SOCKET)
			{
				//Create listening socket
				listeningSocket = createListeningTCPsocket(30000, &slocalAdress, &sAddress);
				if(listeningSocket < 0)
				{
					return;
				}
				else
				{
					tcpStatus = TCP_IP_NO_CONNECTION;
				}
			}
			else if(tcpStatus == TCP_IP_NO_CONNECTION)
			{
				//Look for incomming TCP connection from a computer
				connectedSocket = createConnectingTCPsocket(30000,&listeningSocket, &slocalAdress, &sAddress);

				if(connectedSocket == -11)
				{
				//No connection request
					return;
				}
				else if((connectedSocket < 0) && (connectedSocket != -11))
				{
					//connection is lost
					MAP_UtilsDelay(80000);
					sl_Close(connectedSocket);
					MAP_UtilsDelay(80000);
					sl_Close(listeningSocket);
					tcpStatus = TCP_IP_NO_TCP_LISTENING_SOCKET;

					timeStampNewSocket = mSecondsCounter;
					return;
				}
				else
				{
					MAP_UtilsDelay(80000);
					tcpStatus = TCP_COMPUTER_CONNECTED;
				}
			}
			else if(tcpStatus == TCP_COMPUTER_CONNECTED)
			{
				//
				//handle requests from computer
				//

				MAP_UtilsDelay(80000);
				iStatus = sl_Recv(connectedSocket, communicationBuffer, 1460, 0);

				if(iStatus == 0)
				{
					//connection is lost
					MAP_UtilsDelay(80000);
					sl_Close(connectedSocket);
					tcpStatus = TCP_IP_NO_CONNECTION;
					return;
				}

                                ...

  • Hi,

    I am not sure, because I slightly lost in your code, but it looks that you not close socket properly. You can easily check this by printing number of socket (handle) into console.


    Jan
  • Hi,

    You are right, I close the the socket after receiving a 0 from sl_receiv(). I want that the computer close the socket first. Can you name the function, that you mean?

    Emru

  • Hi,

    Closing client socket sounds reasonable when you get 0 or negative code from sl_receiv(). Only when you receive SL_EAGAIN (-11) that make sense sleep task for moment and call sl_Recv() again. Also I am slightly confused from you closing server socket...

    Sorry, I don't understand your question.

    Jan
  • Sorry, I thought there is a function to check if the tcp connection is still available. I close the socket only if I get a negative value from sl_receive wich is not equal to -11. The computer disconnects first.

    I only can do 3-4 new tcp connections. Then it does not work.
  • Hi,

    It is not necessary close server socket. But if you properly open server socket again then it is not error. Please try do something like:

    listeningSocket = createListeningTCPsocket(30000, &slocalAdress, &sAddress);
    UART_PRINT("server-sock-nr: %d\n\r", listeningSocket);
    ...
    ...
    sl_Close(listeningSocket);
    UART_PRINT("server-sock-close-nr: %d\n\r", listeningSocket);
    ..
    ..
    sl_Close(connectedSocket);
    UART_PRINT("client-sock-close-nr: %d\n\r", ,connectedSocket);
    ...
    ...
    connectedSocket = createConnectingTCPsocket(30000,&listeningSocket, &slocalAdress, &sAddress);
    UART_PRINT("cliet-sock-nr: %d\n\r",connectedSocket);

    If you will see continuously increasing number of socket, then you probably did not call sl_Close() in some case.


    Jan

  • After closing listeningSocket, did I not need another one?

    Emru
  • Hi,

    Yes, you have one listening socket and always new socket for each new TCP connection. If you will close properly client socket, then new client socket will have same socket handle (number). But if you not close socket properly then number will be increased. When you will have occupied all 8 socket, new connection will not be possible. I expect that this is your problem, but it can be something else...

    Jan
  • I checked, if the connectedSocket is closed after the end of a connection with sl_close(connectedSocket). After that a new connection get a increasing socket number. I always get a 0 = succes after sl_close(). I don't understand why the socket is not closed properly.
  • Now I changed the code a little bit. The computer sends a command that the CC3200 disconnects first.

    if(DISCONNECT_SOCKET == communicationCMD)
    {
    MAP_UtilsDelay(80000);
    iStatus = sl_Close(connectedSocket);
    tcpStatus = TCP_IP_NO_CONNECTION;
    }

    return;

    But every new connection gets an increasing number... I am sure the sl_Close is used before using createConnectingTCPsocket().
  • Hi,

    Can you share handles (numbers) of sockets during TCP connections?

    Jan
  • I hope I understand your question right. Do you asked for socket numbers in my example?

    ListeningSocket: 16
    Connecting Sockets: 17 -> 18 -> 19 -> 20
  • Hi,

    I am almost sure, that you not properly close client sockets. Api calls sl_Socket() and sl_Close() are pair functions. Please add debug printf after sl_Socket() and sl_Close(). Probably you will see that you call more sl_Socket() than sl_Close() or you not set correct socket handle into sl_Close().

    Jan
  • Hi,

    I found the problem. In connectCommunicationSocket I changed this part to:

    ...
    iNewSockID = sl_Accept(*iSockID,(struct SlSockAddr_t *)sAddr,(SlSocklen_t*)&iAddrSize);


    if( iNewSockID < 0 )
    {
    // error
    MAP_UtilsDelay(80000);
    //iStatus = sl_Close(iNewSockID);
    return iNewSockID;
    }
    ...

    I find out that I get -1 if I close the the socket with the number -11. Thank you for your support, Jan!
  • OK, If question is answered please mark it as Answered.

    Thanks