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; } ...