Multiple UDP servers on the same board

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

Multiple UDP servers on the same board

556 Views
nikhilmoray
Contributor II

I have tried to create two separate servers on the same board who is having two Ethernet ports. I have created two sockets for two Ethernet interfaces and bind them with INADDR_ANY. But when I call sendto function for any of the sockets, data is transmitting out on both the networks (Sockets).

Please suggest a remedy for the same.

Labels (1)
3 Replies

459 Views
Martin_
NXP Employee
NXP Employee

Bind the first socket to the IP address of the first interface. Bind the second socket to the IP address of the second interface. For sending to a disjoint networks, you should configure gateway(s) and/or default gateway. System routing table selects appropriate interface.

-Martin

459 Views
nikhilmoray
Contributor II

Thanks your reply. I have tried this combination and it is working correctly in case of transmission. But reception is not working at all. It is neither receiving broadcasted nor point to point packets.

I have used below gateways:

Default gateway for Ethernet interface 1: 10.9.211.254

Default gateway for Ethernet interface 2: 192.168.1.254

Below is the implementation for UDP packet reception.

void fnReceiveUDP(UINT32_T u32SocketHandle)               //[IN]

{

//Init u32ReceiveLen;

  INT32_T i32ReceiveLen = RTCS_ERROR;

  // IP address struct instance

  sockaddr_in stSenderIP;

  // Source IP addr

  _ip_address u32SenderIP = ZERO;

  // Temp payload buffer

  UINT8_T au8TempRxBuffer[MAX_PAYLOAD_SIZE];

  // Length init

  UINT16_T u16len               = sizeof(stSenderIP);

  // Destination Ip address pointer init

  sockaddr_in *ptstRemoteAddr   = NULL;

  // UDP port

  UINT8_T u8UDPPort;

  // Initialise sender address information

  ptstRemoteAddr = &stSenderIP;

  // Set the protocol family

  ptstRemoteAddr->sin_family = AF_INET;

  // Set port number

  ptstRemoteAddr->sin_port = (UINT16_T)htons(&u8UDPPort,       //[IN]

                                             BRK_NW_UDP_PORT  //[IN]

                                            );

  // Set sender IP address

  ptstRemoteAddr->sin_addr.s_addr = u32SenderIP;

// Keep receiving the input messages

  while(TRUE)

  {

    // Call recvfrom() to receive a packet from the stack.

    i32ReceiveLen = recvfrom(u32SocketHandle,                //[IN]

                             &au8TempRxBuffer,               //[OUT]

                             sizeof(au8TempRxBuffer),        //[IN]

                             ZERO,                           //[IN]

                             (sockaddr *)ptstRemoteAddr,     //[IN]

                             &u16len                         //[IN]

                            );

// Check whether message has been received successfully or not

    if(i32ReceiveLen == RTCS_ERROR)

    {

    }

}

}

0 Kudos

459 Views
nikhilmoray
Contributor II

This function is for Socket 1 and the u32SenderIP = Source IP of socket 1 or Ethernet interface 1.

0 Kudos