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.

CCS: CC3200 how to get port number from socket or packet?

Other Parts Discussed in Thread: CC3200

Tool/software: Code Composer Studio

I have a CC3200 connecting via P2P to an Android phone with the Android as the Group Owner. The CC3200 sends a UDP packet on a prefixed port (8080) from which the Android retrieves its IP and dynamically generated port number. The Android stores that information and all future communication with the CC320 occurs over the dynamically generated port.

My problem is with finding the dynamically generated port on the CC3200. How do I extract it from the socket and save it to a variable?

My code is from the udp_socket example (slightly modified) as follows.

int UdpSendSocket(unsigned short usPort)
{
	SlSockAddrIn_t  sAddr;
	int             iCounter;
    int             iAddrSize;
    int             iSockID;
    int             iStatus;
    short           sBufLen;

    // filling the buffer
    for (iCounter=0 ; iCounter < strlen(g_cAck) ; iCounter++)
    {
    	g_cBsdBuf[iCounter] = (char)(g_cAck[iCounter]);
    }

    //sTestBufLen  = BUF_SIZE;  //original code
    //set buffer length to size of message
    sBufLen = strlen(g_cAck);

    //filling the UDP server socket address
    sAddr.sin_family = SL_AF_INET;
    sAddr.sin_port = sl_Htons((unsigned short)usPort);					//rearranges order of bytes for port
    sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulGatewayIp);		//rearranges order of bytes for IP

    iAddrSize = sizeof(SlSockAddrIn_t);

    // creating a UDP socket
    iSockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0);
    if( iSockID < 0 )
    {
        // error
        ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
    }

    // for a UDP connection connect is not required
    		// sending packet
            iStatus = sl_SendTo(iSockID, g_cBsdBuf, sBufLen, 0,
                                    (SlSockAddr_t *)&sAddr, iAddrSize);
            if( iStatus <= 0 )
            {
                // error
                sl_Close(iSockID);
                ASSERT_ON_ERROR(SEND_ERROR);
            }

    UART_PRINT("Sent %u packets successfully\n\r", g_ulPacketCount);

    //closing the socket after sending 1 packet
    sl_Close(iSockID);

    return SUCCESS;
}

  • Hi Greg,

    You will need to specify the port using sl_Bind.

    -Aaron
  • Hello Aaron

    I don't understand your answer. The documentation says that sl_Bind is for giving the socket the local address. My issue is with retrieving the port that is dynamically generated when I send my first message. ie -  I send a first udp packet to Android on port 8080 at the Group Owner address. Android gets the message and also the dynamically generated port from the packet, usually somewhere between 29000 and 49999. Back on the CC3200 I also want to retrieve that port before I close the socket. With it I will create a consistently open receive socket that will be used to receive commands from the Android for controlling a pneumatic popup target. Even if sl_Bind utilized a port number you would have to have it before using it with the sl_Bind function. Case in point, when I create a socket to receive on I implement the sl_Bind function as follows;

    iStatus = sl_Bind(iSockID, (SlSockAddr_t *)& sLocalAddr, iAddrSize);

    In this case sLocalAddr is the IP but I have to have it to use the sl_Bind function. Unless I'm missing something, (which is entirely possible) sl_Bind plays no part in retrieving information.

    Greg

  • Use getsockname() on the socket you create.

  • I don't see where getsockname() is supported on the CC3200. I've been all through the documentation and socket.h. Additionally I ran across this thread that says it isn't supported.

    e2e.ti.com/.../409466

    But thanks for taking the time to reply.

    Greg

  • Greg,

    Yes, sl_Bind will allow you to set the port. This is how you will know what the port is.

    -Aaron