Error in UDP communication - query/response

cancel
Showing results for 
Search instead for 
Did you mean: 

Error in UDP communication - query/response

287 Views
cerma
Contributor III

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;
}

Tags (4)
0 Kudos
2 Replies

71 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi Jaroslav:

Errocode 5731 means "port already open",  UDP protocol needs to make sure this port is not in use with same local ip addr. It will check the port when you "binding" and "connecting " ports. do you use this port somewhere else?

Regards

Daniel

0 Kudos

71 Views
cerma
Contributor III

Hi Daniel,

I am now shutting down socket after I successfully receive UDP frame (shutdown(sock, FLAG_ABORT_CONNECTION)). For transmitting I am opening new socket which I am closing after I send data. But even after this change only one message is sent. When I am trying to send another message I see in Wireshark ICMP message stated "port not found". If I use another target port than source port from request (incoming message) then is communication working fine, but after several hours occurs that receiving get stuck.

I need get communication working. If PC is sending request from its port (6050 - source port) to K60's port (6055 - target port). K60 responses. So response goes from K60's port (6055 - source port) to PC's port (6050 - target port). K60 has static IP address. PC can have any IP address according to net mask. K60 has to be able to receive data from any IP but always from same port and PC's IP can vary with every message. Is there any code sample which solves this issue?

// MAIN -------------------------------------------

InitializeNetworking(3, 2, 2, 0);

// ------------------------------------------------

 

// ------------------------------------------------

// Client Task

// ------------------------------------------------

void discoveryTask(uint_32 initial_data)

{     

       uint_32             sock;

       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);         // create socket                                                                             

             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;

             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;              // LED 2                  

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)

                           {

                                  end = 0;

                                  remoteIP = local_sin.sin_addr.s_addr;

                                       

                                  if(flagPrvniPrijem) // first reception

                                  taskID = _task_create(0, CLIENT_TASK, 0);

                                       

                                  flagPrvniPrijem = 0;

                                       

                                  for(i = 0; i < count; i++)   // copy frame

                                  {

                                        buff_RX_uart_B_PC[i] = myBuffer[i];

                                  }

                                  pocet_prijatych_bajtu_B_PC = count;

                                  Vyhodnoceni_uart_B(); // evaluation of the received frame

                           }

                    }

             }

             shutdown(sock, FLAG_ABORT_CONNECTION); // termination socket

       }

}

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*/;

   

       while(TRUE){

 

             if(flagodeslatData) // the received data are correct. I send a response.

             {

                   

                 sock = socket(PF_INET, SOCK_DGRAM, 0);   // create socket

              

                 if (sock == RTCS_SOCKET_ERROR)

                 {

                    remoteIP = 0;

                     return;

                 }

 

                 addr.sin_family      = AF_INET;

                 addr.sin_port        = 6049;        // 6050

                 addr.sin_addr.s_addr = INADDR_ANY;

 

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

                

                 if (error != RTCS_OK)

                 {

                    remoteIP = 0;

                     return;

                 }

 

             // remoteIP = REMOTE_IP; // I use an address from the received frame.

                    rem_addr.sin_family      = AF_INET;

                    rem_addr.sin_port        = DESTPORT;          

                    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;

                 }

                

                    flagodeslatData = 0;

 

                    GPIOE_PSOR = PORTE5;       // LED 2 

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

 

                    if (error <= 0)

                    {

                           printf("There was an error trying to send to the server\n");

                           return;

                    }

                    remoteIP = 0;

                   

                    shutdown(sock, FLAG_ABORT_CONNECTION);  // termination socket

             }

       }

   return;

}

Regards

Jaroslav

0 Kudos