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.

Socket Returns invalid_socket

Other Parts Discussed in Thread: OMAPL138

I'm trying to set up a message parse on a OMAPL138 with ARM9 and DSP processors.

Using

NDK_2.24.1.18

SYS/BIOS 6.41.0.26

TI-RTOS 2.0.1.23

In the Networking Hooks I have the Network open hook calling the following, but it fails when it trys to make the socket:

int InitTCPListener()
{
	SOCKET   stcp = INVALID_SOCKET;
		struct   sockaddr_in sin1;
		SOCKET   stcpactive = INVALID_SOCKET;
		int      size,tmp;
		SOCKET   stcpbusy;
		struct   timeval timeout;

		char recvbuf[100];
		char sendbuf[100];

		fdOpenSession(TaskSelf());

		stcp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if( stcp != INVALID_SOCKET )
			goto leave;

		bzero( &sin1, sizeof(struct sockaddr_in) );
		sin1.sin_family = AF_INET;
		sin1.sin_port   = htons(27015);

		if ( bind( stcp, (PSA) &sin1, sizeof(sin1) ) < 0 )
		        goto leave;

		if ( listen( stcp, 1) < 0 )
		        goto leave;

		DbgPrintf(DBG_INFO, "Listening for connection\n");

		while(1)
		{
			fd_set ibits, obits, xbits;
			int    cnt;

			FD_ZERO(&ibits);
			FD_ZERO(&obits);
		    FD_ZERO(&xbits);

		    FD_SET(stcp, &ibits);

		    if( stcpactive == INVALID_SOCKET )
		    {
		    	// Wait without timeout
		        tmp = fdSelect( 4, &ibits, &obits, &xbits, 0 );
		    }
		    else
		    {
		    	// Wait for set timeout - abort active connection on no activity
		        FD_SET(stcpactive, &ibits);
		        tmp = fdSelect( 4, &ibits, &obits, &xbits, &timeout );
		        if( tmp <= 0 )
		        {
		        	fdClose( stcpactive );
		            stcpactive = INVALID_SOCKET;
		        }
		    }

		    if( tmp < 0 )
		    	goto leave;

		    if( FD_ISSET(stcp, &ibits) )
		    {
		    	// We have a new connection. Assign it so sbusy at
		    	// first...
		      	size = sizeof( sin1 );
		     	stcpbusy = accept( stcp, (PSA)&sin1, &size );

		       	// If the active socket is free use it, else print out
		     	// a busy message
		     	if( stcpactive == INVALID_SOCKET )
		        	stcpactive = stcpbusy;
		      	else
		      		fdClose( stcpbusy );
		 	}

		    if( stcpactive != INVALID_SOCKET && FD_ISSET(stcpactive, &ibits) )
		    {
		    	// There is data available on the active connection
		        cnt = (int)recv( stcpactive, (char *)&recvbuf, 1024, 0 );

		        if( cnt > 0 )
		        {
		        	//PARSER WILL GO HERE BUT RIGHT NOW ECHO TO TEST
		        	//ParseCommand(recvbuf, sendbuf, 100);
		        	printf(sendbuf,recvbuf);
		        	if( send( stcpactive, sendbuf, size, 0 ) < 0 )
		            {
		        		fdClose( stcpactive );
		        		stcpactive = INVALID_SOCKET;
		            }
		        }
		        // If the connection got an error or disconnect, close
		        else
		        {
		        	fdClose( stcpactive );
		        	stcpactive = INVALID_SOCKET;
		        }
		    }
		}

	leave:
		fdCloseSession( TaskSelf() );
}

\

  • Hi Adam,

    What error msg do you get ?

    I would recommend using the networkIPAddrHook instead of networkOpenHook. The NDK stack may not be fully up when networkOpenHook is called.

    Best,
    Ashish
  • The call to stcp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); returns INVALID_SOCKET

    Tried using networkIPAddrHook got the same Error.

    I know the connection works, I was able to add Telnet to the Application layer and was able to send and recieve message over that.

    Currently I have the following:
    EMAC with EVMOMAPL138 selected
    IP with Obtain IP address automatically selected and default settings otherwise (IP address is being assigned and I am able to ping the connection)
    TCP with default settings
    DHCP with default settings
  • Hi Adam,

    Can you try putting a breakpoint in the NDK_socket() function and step through ? Stepping through the function will allow you to determine the failure is occurring during what operation (Is some memory alloc call failing or the file descriptor table pointer is bad or some other failure).

    Best,
    Ashish
  • Adam,

    I noticed a problem in your code for creating the socket:

           stcp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

           if( stcp != INVALID_SOCKET )

               goto leave;

    The if clause will cause the program to jump to "leave" when a valid socket is returned.  I think you should change the code to:

           stcp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

           if( stcp == INVALID_SOCKET )

               goto leave;

    Steve

  • Sorry about taking so long to get back to you, I was on travel.

    That was the issue, the interface is now up and I can connect.

    I Have a few minor things left to fix but will post them as new questions.

    Thanks.