In our project, we are using two separate software Ethernet interfaces for two Ethernet ports. Thus, I have bind them with two different IP addresses and created two separate sockets for the same. But when I bind the socket with INADDR_ANY, and send the data through socket 1, both Ethernet ports are transmitting the same out at the same time. Moreover, while receiving, only one socket is receiving the data.
But if we bind sockets to two different IP addresses, both are working fine. Both sends the data correctly. But in this case, no one is receiving a single packet. Means, only transmission is working. Kindly refer the code written below and suggest me better solution if any.
// Set RTCS task priority to 7 (default)
_RTCSTASK_priority = RTCS_TASK_PRIORITY;
//RTCS task init
u32Result = RTCS_create();
// Verify whether RTCS task has been created successfully
if(RTCS_OK != u32Result)
{
// Failed to create RTCS task
}
else
{
/* Initialize Ethernet device 0 */
u32Result = ipcfg_init_device(MACNET_DEVICE_0, au8SourceMAC);
// Verify whether Ethernet 0 has been initialized properly
if(ENET_OK != u32Result)
{
// Failed to initialise Ethernet 0
u32InitStatus |= MAC0_INIT_FAIL;
}
else
{
// MAC 0 has been initialized successfully
}
/* Initialize Ethernet device 1 */
u32Result = ipcfg_init_device(MACNET_DEVICE_1, au8SourceMAC1);
// Verify whether Ethernet 0 has been initialized properly
if(ENET_OK != u32Result)
{
// Failed to initialise Ethernet 1
u32InitStatus |= MAC1_INIT_FAIL;
}
else
{
// Ethernet 0 has been initialized successfully
}
}
// Bind Ethernet 0 to static IP
u32Result = ipcfg_bind_staticip(MACNET_DEVICE_0, &stEthernet0Para);
// Verify whether binding of new IP has been done successfully
if(IPCFG_OK != u32Result)
{
// Failed to bind IP with Ethernet 0
u32RTCSInitStatus |= MAC0_INIT_FAIL;
}
else
{
// Ethernet 0 has been initialized successfully
}
// Bind Ethernet 1 to static IP
u32Result = ipcfg_bind_staticip(MACNET_DEVICE_1, &stEthernet1Para);
// Verify whether binding of new IP has been done successfully
if(IPCFG_OK != u32Result)
{
// Failed to initialise Ethernet 1
u32RTCSInitStatus |= MAC1_INIT_FAIL;
}
else
{
// Ethernet 1 has been initialized successfully
// Temp address
u32EthBrkNwIPAddr = IPADDR(10,9,211,117);
// Brake Network Socket Initialization
*ptu32BrkNwSocketHandle = fnBrkNwSocketInit(u32EthBrkNwIPAddr, (UINT16_T)BRK_NW_UDP_PORT);
// Temp address
u32EthBrkNwIPAddr = IPADDR(10,9,211,120);
// Brake Network Socket Initialization
*ptu32TrainNwSocketHandle = fnBrkNwSocketInit(u32EthBrkNwIPAddr, (UINT16_T)TRN_NW_UDP_PORT);
}
// Socket init function definition
UINT32_T fnBrkNwSocketInit(UINT32_T u32SenderIP, | //[IN] | |
UINT16_T u16PortNumber | //[IN] | |
) |
{
// Process status
UINT32_T u32Status;
// Socket handle init
UINT32_T u32SocketHandle;
// Local IP address pointer
sockaddr_in *ptstLocalAddr;
// Local UDP port
UINT8_T u8UDPPort = ZERO;
// Socket struct instance (Part of a stack)
sockaddr_in stLocalEndPoint;
// Create the UDP socket
u32SocketHandle = socket(AF_INET, | //[IN]: Protocol family | |
SOCK_DGRAM, //[IN]: Type of Socket | ||
ZERO | //[IN]: Protocol (Ignored) | |
); |
// Verify whether parent socket has been created
if(RTCS_SOCKET_ERROR == u32SocketHandle)
{
// Failed to create a parent socket |
}
else
{
// Setup local end point parameters | |
ptstLocalAddr = &stLocalEndPoint; |
// Set up protocol family | ||
ptstLocalAddr->sin_family | = AF_INET; |
// Set up UDP port | ||
ptstLocalAddr->sin_port | = htons(&u8UDPPort, u16PortNumber); |
// Set up number IP addresses which are serviced | |
ptstLocalAddr->sin_addr.s_addr = INADDR_ANY; | |
// | ptstLocalAddr->sin_addr.s_addr = u32SenderIP; |
// Set Socket options | ||
u32Status = fnBrkNwSocketOptions(u32SocketHandle); | //[IN] |
// Verify whether socket has been configured correctly | ||
if(u32Status != RETURN_SUCCESS) | ||
{ | ||
// Failed to configure a parent socket | ||
} | ||
else | ||
{ | ||
// Bind the socket to a port | ||
u32Status = bind(u32SocketHandle, | //[IN] | |
ptstLocalAddr, | //[IN] | |
sizeof(sockaddr_in) | //[IN] | |
); | ||
// Verify whether parent socket has been binded to the correct IP address | ||
if(RTCS_OK != u32Status) | ||
{ | ||
// Failed to bind | ||
} | ||
else | ||
{ | ||
// Binding between socket and Ip address has been done successfully | ||
} | ||
} //if(u32Status != RETURN_SUCCESS) |
} //else if(RTCS_SOCKET_ERROR == u32SocketHandle)
// Return socket handle
return(u32SocketHandle);
}
Could you please specify what you are sending and what you want receive?
Your description fits to my suspicion that you are working with broadcast/multicast. Is it right?
You sending broadcast and this is sending by both interfaces, but you are not able receive any broadcast when you bind for specific IP address.
I suppose that interfaces are on separates network segments (they are not connected to the same switch/LAN network). Correct?
Have a great day,
RadekS
Yes, I am trying to broadcast the packets from both the interfaces and they are connected to two separate PCs with two different series of IP addresses. When two sockets are bound with the specific addresses (Interface Addresses), none of the packets have been received from any of the them. I have tried to receive broadcasted as well as point to point packets, but nothing worked. But as soon as I bind those sockets with ADDR_ANY, they started receiving all the packets.