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.

CC1100: UDP Sockets not working

Other Parts Discussed in Thread: CC3100, UNIFLASH

Hi,

I have some problems with the UDP sockets. I'm able to send data on one CC3100 and receive the data on another CC3100. But I'm unable to send and receive data on the same device.


I have the following working example

static void Working()
{
	uint8_t i;
	uint8_t buffer[10];
	int16_t mySocket = 0;
	int16_t status;
	SlSockAddrIn_t serverAddress;
	uint32_t ipAdress = 0xc0a80136; // 192.168.1.54

	// Address to send to server
	serverAddress.sin_family = SL_AF_INET;
	serverAddress.sin_port = sl_Htons((UINT16) 80);
	serverAddress.sin_addr.s_addr = sl_Htonl((UINT32) ipAdress);

	// Local Address

	mySocket = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0);
	if (mySocket < 0)
	{
		// Error
		return;
	}

	for (i = 0; i < 100; i++)
	{
		// Send Packet
		status = sl_SendTo(mySocket, buffer, 10, 0, (SlSockAddr_t *) &serverAddress, sizeof(SlSockAddrIn_t));

		if (status < -1)
		{
			// Error
			return;
		}
	}

	sl_Close(mySocket);
}

When I try to receive data on the same socket the sl_SendTo fails. It already fails when I bind the local Address (for receiving) to the socket:

static void NonWorking()
{
	uint8_t i;
	uint8_t buffer[10];
	int16_t mySocket = 0;
	int16_t status;
	SlSockAddrIn_t localAddress, serverAddress;
	uint32_t ipAdress = 0xc0a80136; // 192.168.1.54

	// Address to send to server
	serverAddress.sin_family = SL_AF_INET;
	serverAddress.sin_port = sl_Htons((UINT16) 80);
	serverAddress.sin_addr.s_addr = sl_Htonl((UINT32) ipAdress);

	// Local Address
	localAddress.sin_family = SL_AF_INET;
	localAddress.sin_port = sl_Htons((UINT16) 80);
	localAddress.sin_addr.s_addr = htonl(SL_INADDR_ANY);

	mySocket = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0);
	if (mySocket < 0)
	{
		// Error
		return;
	}

	status = sl_Bind(mySocket, (SlSockAddr_t *) &localAddress, sizeof(SlSockAddrIn_t));
	if (status < -1)
	{
		return;
	}

	for (i = 0; i < 100; i++)
	{
		// Send Packet
		status = sl_SendTo(mySocket, buffer, 10, 0, (SlSockAddr_t *) &serverAddress, sizeof(SlSockAddrIn_t));
		
		// This always fails!

		if (status < -1)
		{
			// Error
			return;
		}

		// Receive
		// TODO
	}

	sl_Close(mySocket);
}

I can get the sample above working by commenting out the sl_Bind line.

Any hints?

Thanks

Dave

  • Sorry my mistake. I can't bind to the socket where I send data to. I have now a working solution.

    But the send_to only works once. The second time send_to is called it returns an error (-1):

    static void NowWorkingOnce()
    {
    	uint8_t i;
    	uint8_t buffer[10];
    	int16_t mySocket = 0;
    	int16_t status;
    	uint16_t AddrSize = 0;
    	SlSockAddrIn_t localAddress, serverAddress;
    	uint32_t ipAdress = 0xc0a80136; // 192.168.1.54
    
    	// Address to send to server
    	serverAddress.sin_family = SL_AF_INET;
    	serverAddress.sin_port = sl_Htons((UINT16) 80);
    	serverAddress.sin_addr.s_addr = sl_Htonl((UINT32) ipAdress);
    
    	AddrSize = sizeof(SlSockAddrIn_t);
    
    	mySocket = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0);
    	if (mySocket < 0)
    	{
    		// Error
    		return;
    	}
    
    	for (i = 0; i < 100; i++)
    	{
    		// Send Packet
    		status = sl_SendTo(mySocket, buffer, 10, 0, (SlSockAddr_t *) &serverAddress, sizeof(SlSockAddrIn_t));
    		
    		// Works once
    
    		if (status < -1)
    		{
    			// Error
    			return;
    		}
    
    		// Receive
    		status = sl_RecvFrom(mySocket, buffer, 1, 0, (SlSockAddr_t *) &localAddress, (SlSocklen_t*) &AddrSize);
    
    		if (status < -1)
    		{
    			return;
    		}
    	}
    
    	sl_Close(mySocket);
    }

    Any idea?

  • David,

    Below is the code snippet for doing UDP loopback - It's tested on CC3100 ES1.32 w/ SDKv0.5.2 and is found to be working fine. Let me know if you need additional information.

    static INT32 UdpEcho(UINT16 Port)
    {
        SlSockAddrIn_t  ServerAddr = {0};
        SlSockAddrIn_t  LocalAddr = {0};
    
        UINT16          idx = 0;
        UINT16          AddrSize = 0;
        UINT16          LoopCount = 0;
    
        INT16           SockID = 0;
        INT16           Status = 0;
    
        UINT8           PrintBuf[32] = {0};
    
        /* Fill the buffer */
        for(idx = 0; idx < BUF_SIZE; idx++)
        {
            uBuf.BsdBuf[idx] = (char)(idx % 10);
        }
    
        ServerAddr.sin_family = SL_AF_INET;
        ServerAddr.sin_port = sl_Htons((UINT16)Port);
        ServerAddr.sin_addr.s_addr = sl_Htonl((UINT32)g_DeviceIp);
    
        LocalAddr.sin_family = SL_AF_INET;
        LocalAddr.sin_port = sl_Htons((UINT16)Port);
        LocalAddr.sin_addr.s_addr = 0;
    
        AddrSize = sizeof(SlSockAddrIn_t);
    
        SockID = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0);
        ASSERT_ON_ERROR(__LINE__, SockID);
    
        Status = sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize);
        if( Status < 0 )
        {
            sl_Close(SockID);
            ASSERT_ON_ERROR(__LINE__, Status);
        }
    
        while (LoopCount < NO_OF_PACKETS)
        {
            Status = sl_SendTo(SockID, uBuf.BsdBuf, BUF_SIZE, 0,
                               (SlSockAddr_t *)&ServerAddr, AddrSize);
            if( Status <= 0 )
            {
                sl_Close(SockID);
                return BSD_UDP_ECHO_FAILED;
            }
    
            sprintf((char *)PrintBuf, "%d", Status);
            CLI_Write(" Sent [");
            CLI_Write(PrintBuf);
            CLI_Write("] bytes \n\r");
    
            Status = sl_RecvFrom(SockID, uBuf.BsdBuf, BUF_SIZE, 0,
                                 (SlSockAddr_t *)&LocalAddr, (SlSocklen_t*)&AddrSize );
            if( Status <= 0 )
            {
                sl_Close(SockID);
                return BSD_UDP_ECHO_FAILED;
            }
    
            sprintf((char *)PrintBuf, "%d", Status);
            CLI_Write(" Received [");
            CLI_Write(PrintBuf);
            CLI_Write("] bytes \n\r");
    
            LoopCount++;
        }
    
        sl_Close(SockID);
        return SUCCESS;
    }

    BR,

    Praneet

  • Dear Praneet


    Thank for your UDP Loopback code, but unfortunately it doesn't work on my board.

    The command

     Status = sl_SendTo(SockID, uBuf.BsdBuf, BUF_SIZE, 0,       (SlSockAddr_t *)&ServerAddr, AddrSize); 


    always fails with status -1

    When I comment out (delete) the line

    Status = sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize);


    the send works but the receive not.

    So, I'm still unable to send and receive data from a UDP socket. Any solution?

    My device: XCC3100HZ


    Regards,

    David

  • David,

    What's the value of 'BUF_SIZE' in your application? On what port are you sending the data out? Are you using the destination address as "localhost" or are you using the IP address that was acquired by your device? Is the size of data that you intend to send and receive different?

    If you could please send me your code, I can review and test it on my platform.

    Please find attached the sample-application with which I tested UDP loopback - Modify the values for 'SSID_NAME', 'SEC_TYPE' and 'PASSKEY' per your AP's properties and execute it on your platform. I've also attached my test-logs for reference.

    BR,

    Praneet

    5826.udp_socket.zip

    2746.udp_loopback_logs.zip

  • Dear Praneet

    Thanks for your code.

    After I had updated the service pack of my device with uniflash everything is working fine!

    http://e2e.ti.com/support/wireless_connectivity/f/968/t/353494.aspx

    Thanks for your help!

    David