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.

SIMPLELINK-CC3220-SDK: Listen to UDP messages on specific port

Part Number: SIMPLELINK-CC3220-SDK

Hi,

From what I understand from the UDP socket documentation, we can only listen to a specific port AND IP address! 

My goal would be to listen to a specific UDP port but no specific IP. Any clients can send data to that port and I would like the CC3220 to handle those requests.

Anyone can think about a way to achieve this?

Thanks !

Vincent V.

  • Hi Vincent,

    Thanks for the question. Will get back to you shortly.
  • One possibility could be that all the clients are able to receive broadcast udp messages.

    To send an udp broadcast message the target IP should be "255.255.255.255" which is the broadcast address and one specific PORTlet's say port 3000.

    Any udp packets sent to this ip/port should be received by all clients listening to port 3000.

    How the clients can figure out if the message is targetted to him?

    The payload message can be a json string . e.g {"id":"client_123","msg":"This is a test"} if the client ID match the json field "id" then do something if not ignore it.

    Hope this hepls.

  • Investigating further I found a way to achieve this.

    The receiving socket address can be set to "SL_INADDR_ANY" and will therefore listen to any IP a connected client could have.

    Here is the pseudo-code to illustrate how it works:

    SlSockAddrIn_t Addr;
    _i16 Sd;
    _i16 Status;
    _i8 RecvBuf[1460];
    SlSocklen_t AddrSize;
    
    	Sd = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0);
    	if( 0 > Sd ) {
    		UART_PRINT("Error Socket\r\n");
    	}
    
    	SlSockNonblocking_t BlockingOption;
    	BlockingOption.NonBlockingEnabled = 1;
    
    	// Enable or disable non-blocking mode
    	Status = sl_SetSockOpt(Sd, SL_SOL_SOCKET, SL_SO_NONBLOCKING, (_u8*)&BlockingOption, sizeof(BlockingOption));
    	if( 0 > Status ) {
    		UART_PRINT("Error Set non-blocking\r\n");
    	}
    
    
    	Addr.sin_family = SL_AF_INET;
    	Addr.sin_port = sl_Htons(5001);
    	Addr.sin_addr.s_addr = SL_INADDR_ANY;
    
    	Status = sl_Bind(Sd, (SlSockAddr_t*)&Addr, sizeof(SlSockAddrIn_t));
    	if( 0 > Status ) {
    		UART_PRINT("Error Bind\r\n");
    	}
    
    
    	// Set Addr for recv
    	Addr.sin_family = SL_AF_INET;
    	Addr.sin_port = sl_Htons(5001);
    	Addr.sin_addr.s_addr = SL_INADDR_ANY;
    	AddrSize = sizeof(SlSockAddrIn_t);
    	
    
    	Status = sl_RecvFrom(Sd, RecvBuf, 1460, 0, ( SlSockAddr_t *)&Addr, &AddrSize);
    	if(Status == SL_ERROR_BSD_EAGAIN) {
    		// no data...
    		UART_PRINT("No data...\r\n");
    	}
    	else if(Status > 0) {
    		UART_PRINT("Recv data ! :)\r\n");
    	}
    	else {
    		UART_PRINT("Error Rcv\r\n");
    	}

  • One more thing: I would like to know from which IP the message is coming from..
    Is there a way to achieve this ?
  • Hi,

    Please see BSD sockets tutorial - wiki.treck.com/Introduction_to_BSD_Sockets

    Jan
  • Thanks.

    To complete the answer: The IP and port of the sender can be read out from the SlSockAddr_t struct that was passed as input.

    Status = sl_RecvFrom(mRegisteredUdpPortList[ii].socketHandle, RecvBuf, 1460, 0, (SlSockAddr_t*)&Addr, &AddrSize);
    uint32_t src_ip =   Addr.sin_addr.s_addr;
    uint16_t src_port = Addr.sin_port;