Simple UDP communication

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Simple UDP communication

Jump to solution
4,258 Views
sergemonnerat
Contributor III

Hello,

I try to setup a simple udp communication between an UDP client on my PC and the K60 tower. I have wireshark on the PC and I see the outgoing frame to the K60. I have follow the socket example Quote of the Day in the Freescale MQX™ RTCS™ User’s Guide.

When I run the app on the board the RTCS_selectall function return immediately (before I send anything through the network) with a valid socket number but when I read the datas with the recvfrom function the IP address, the TCP port and the datas are 0. Afterthat when I try to send UDP frame's the selectall function never detect activity on the socket. Thank for your help. The udp task that I write:

/*TASK*-----------------------------------------------------------------

*

* Function Name  : udp_task

* Returned Value : void

* Comments       :

*

*END------------------------------------------------------------------*/

void udp_task(uint_32 temp)

{

  static uint_32      sSocketHndl;

  static uint_32      sListenSocketHndl;

  static uint_16      sRemoteRxLengh;

  static int_32        sNbOfByteReceived;

  static int_32        sNbOfByteTransmit;

  static sockaddr_in      sLocalAddr;

  static sockaddr_in      sRemoteTxAddr;

  static sockaddr_in      sRemoteRxAddr;

  static uchar                sUdpRemoteBuffer[kUDP_BUF_LEN];         

  static int_32       error;

  static uint_32     sUerror;

  static _enet_handle           sEthernetHandle;

  static _rtcs_if_handle        sRTCSHandle;

  static _enet_address          sMacAddress;

 

  /* We are running on a memory constrained device, lower RTCS's defaults */

  /* runtime RTCS configuration */

  _RTCSPCB_init = 4;

  _RTCSPCB_grow = 2;

  _RTCSPCB_max = 6;

  _RTCS_msgpool_init = 4;

  _RTCS_msgpool_grow = 2;

  _RTCS_msgpool_max  = 8;

  _RTCS_socket_part_init = 4;

  _RTCS_socket_part_grow = 2;

  _RTCS_socket_part_max  = 6;

  _RTCSTASK_stacksize = 2200;

  error = RTCS_create();

  ENET_get_mac_address(DEMOCFG_DEFAULT_DEVICE, ENET_IPADDR, sMacAddress);

  sUerror = ENET_initialize(DEMOCFG_DEFAULT_DEVICE,sMacAddress, 0, &sEthernetHandle);

  if(sUerror!=ENET_OK)

       _task_block();

  sUerror = RTCS_if_add(sEthernetHandle, RTCS_IF_ENET, &sRTCSHandle);

  if(sUerror!=RTCS_OK)

       _task_block();

  sUerror = RTCS_if_bind(sRTCSHandle, ENET_IPADDR, ENET_IPMASK);

  if(sUerror!=RTCS_OK)

       _task_block();

  // Config du gateway address

  /*sUerror = RTCS_gate_add(ENET_IPGATEWAY, INADDR_ANY, INADDR_ANY);

  if(sUerror!=RTCS_OK)

  _task_block();*/

  sSocketHndl = socket(PF_INET, SOCK_DGRAM, 0);

  if(sSocketHndl == RTCS_SOCKET_ERROR)

       _task_block();

  sLocalAddr.sin_family = AF_INET;

  sLocalAddr.sin_port = 10;

  sLocalAddr.sin_addr.s_addr = IPADDR(157,26,100,93);//INADDR_ANY;

  sUerror = bind(sSocketHndl, &sLocalAddr, sizeof(sLocalAddr));

  if(sUerror != RTCS_OK)

       _task_block();

 

  sListenSocketHndl=sSocketHndl;

  sRemoteTxAddr.sin_family = AF_INET;

  sRemoteTxAddr.sin_port = 8;

  sRemoteTxAddr.sin_addr.s_addr = IPADDR(157,26,100,78);

  sUerror=connect(sListenSocketHndl, &sRemoteTxAddr, sizeof(sRemoteTxAddr));

  if(sUerror != RTCS_OK)

       _task_block();

  while(1)

  {

  sSocketHndl = RTCS_selectall(0);

  if(sSocketHndl == RTCS_SOCKET_ERROR)

       _task_block();

  if(sSocketHndl==sListenSocketHndl)

  {

  memset(&sRemoteRxAddr, 0, sizeof(sRemoteRxAddr));

  memset(&sUdpRemoteBuffer,0, kUDP_BUF_LEN); //clean out buffer

  sRemoteRxLengh = sizeof(sRemoteRxAddr);

  sNbOfByteReceived = recvfrom(sSocketHndl, sUdpRemoteBuffer, kUDP_BUF_LEN, 0, &sRemoteRxAddr, &sRemoteRxLengh);

  if (sNbOfByteReceived == RTCS_ERROR)

       _task_block();

  sRemoteRxAddr.sin_addr.s_addr=INADDR_BROADCAST;

  sRemoteRxLengh = sizeof(sRemoteRxLengh);

  sNbOfByteTransmit = sendto(sSocketHndl,"Fuck hackers",13,RTCS_MSG_NOLOOP,&sRemoteRxAddr, sRemoteRxLengh);

  if (sNbOfByteTransmit == RTCS_ERROR)

       _task_block();

  }

  }

}

Labels (1)
0 Kudos
1 Solution
1,705 Views
Luis_Garabo
NXP TechSupport
NXP TechSupport

Hi,

Some time ago I developed this code about a UDP Server. I am attaching here. I hope it helps you.

You also may find useful the appnote:

http://cache.freescale.com/files/32bit/doc/app_note/AN4644.pdf?fsrch=1&sr=3

http://cache.freescale.com/files/32bit/doc/app_note/AN4644SW.zip?fsrch=1&sr=3

Regards,

-Garabo

View solution in original post

0 Kudos
1 Reply
1,706 Views
Luis_Garabo
NXP TechSupport
NXP TechSupport

Hi,

Some time ago I developed this code about a UDP Server. I am attaching here. I hope it helps you.

You also may find useful the appnote:

http://cache.freescale.com/files/32bit/doc/app_note/AN4644.pdf?fsrch=1&sr=3

http://cache.freescale.com/files/32bit/doc/app_note/AN4644SW.zip?fsrch=1&sr=3

Regards,

-Garabo

0 Kudos