Jaroslav Cermak

Error in UDP communication - query/response

Discussion created by Jaroslav Cermak on Sep 7, 2016
Latest reply on Oct 5, 2016 by Jaroslav Cermak

Hi All.

With MQX4.2 in CW10.6 I have run UDP communication. I am using UDP sockets,I am receiving query and then I am generating response. Everything works fine unless I set to response same source port as has MQX for receiving then I get error code 5731.

Packets example:
Incoming packet has:
  source port: 6055
  destination: 6050

Outgoing packet (my response) should be set to:
  source port: 6050
  destination: 6055
But my code works only if I set as source port different port then 6050. With port 6050 I get that error 5731.

Does anybody know how to fix it or where is problem?
The Code (Demo client) is attached below:

 

void discoveryTask(uint_32 initial_data)
{
   uint_32 sock;
   sockaddr_in local_sin;
   uint_32 result;
   uint_32 count;
   uint_16 local_len = sizeof(local_sin);
   _task_id taskID;
   uint_16 i;

   while(TRUE)
   {
      sock = socket(AF_INET, SOCK_DGRAM, 0);
      if (sock == RTCS_SOCKET_ERROR)
      {
         printf("\nError, socket create failed");
         return;
      }

      memset((char *) &local_sin, 0, sizeof(local_sin));
      local_sin.sin_family = AF_INET;
      local_sin.sin_port = SERVER_BROADCAST_PORT; // 6050
      local_sin.sin_addr.s_addr = INADDR_ANY;

      result = bind(sock, &local_sin, sizeof (sockaddr_in));
      if (result != RTCS_OK)
      {
         printf("\nError, bind() failed with error code %lx", result);
       }

      sock = RTCS_selectall(0);

      if (sock == 0)
      {
         printf("TimeOut expired. No data received. Trying again\n");
      }
      else
      {
         GPIOE_PCOR = PORTE5;
         count = recvfrom(sock, myBuffer, sizeof(myBuffer), 0, &local_sin, &local_len);
         if (count == RTCS_ERROR)
         {
            printf("\nrecvfrom() failed with error %lx", RTCS_geterror(sock));
         }
         else
         {
            if (result == 0)
            {
               if(flagPrvniPrijem)
               {
                  flagPrvniPrijem = 0;
                  GetIP(&myBuffer);
                  taskID = _task_create(0, CLIENT_TASK, 0);
               }
               for(i = 0; i < count; i++)
               {
                  buff_RX_uart_B_PC[i] = myBuffer[i];
               }
               pocet_prijatych_bajtu_B_PC = count;
               Vyhodnoceni_uart_B();
            }
         }
      }
      shutdown(sock, FLAG_ABORT_CONNECTION);
   }
}

 

void clientTask(uint_32 initial_data)
{
   static sockaddr_in addr/*, server_addr*/;
   static sockaddr_in rem_addr/*, server_addr*/;
   static uint_32 sock, error/*, value, mask*/;


   sock = socket(PF_INET, SOCK_DGRAM, 0);

   if (sock == RTCS_SOCKET_ERROR)
   {
      remoteIP = 0;
      return;
   }

   addr.sin_family = AF_INET;
   addr.sin_port = 6050;
   addr.sin_addr.s_addr = INADDR_ANY;

   error = bind(sock, &addr, sizeof(addr));

   if (error != RTCS_OK)
   {
      remoteIP = 0;
      return;
   }

   remoteIP = REMOTE_IP;
   rem_addr.sin_family = AF_INET;
   rem_addr.sin_port = DESTPORT; // 6055
   rem_addr.sin_addr.s_addr = remoteIP;

   error = connect(sock, &rem_addr, sizeof(rem_addr));
   if (error != RTCS_OK)
   {
      shutdown(sock, FLAG_ABORT_CONNECTION);
      return;
   }

      /*****Infinite loop waiting for user to push a button***/
   while(TRUE)

   {

      if(flagodeslatData)
      {
         flagodeslatData = 0;

         GPIOE_PSOR = PORTE5; // LED 2

         error = sendto(sock, TX_buff_uart_B_send, pocet_bajtu_TX, 0, &rem_addr, sizeof(rem_addr));

         if (error <= 0)
         {
            return;
         }
         remoteIP = 0;
       }
    }
   return;
}

Outcomes