Please help with this problem be patient and read through to the end. I will really appreciate it!!!
I am creating 4 sockets on a computer with 4 network adapters and binding each socket to 1 network adapter.
Network Adapter 1 is at 192.168.100.10. Socket 1 binds to Network Adapter 1
Network Adapter 2 is at 192.168.100.20. Socket 2 binds to Network Adapter 2
Network Adapter 3 is at 192.168.100.30. Socket 3 binds to Network Adapter 3
Network Adapter 4 is at 192.168.100.40. Socket 4 binds to Network Adapter 4
Below is code I use.
Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipEndPoint = new IPEndPoint(localIP, 0);
EndPoint ep = (EndPoint)ipEndPoint;
Each network adapter has a Local Network with one different device at IP 192.168.100.1. I send SNMP (UDP protocol) request data to this device.
Socket 1 sends data to device 1 (192.168.100.1) while binded to 192.168.100.10 Network Adapter 1
Socket 2 sends data to device 2 (192.168.100.1) while binded to 192.168.100.20 Network Adapter 2
Socket 3 sends data to device 3 (192.168.100.1) while binded to 192.168.100.30 Network Adapter 3
Socket 4 sends data to device 4 (192.168.100.1) while binded to 192.168.100.40 Network Adapter 4
EndPoint remote = (EndPoint)new IPEndPoint(peer.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any,0);
IPEndPoint ipEndPoint = new IPEndPoint(_localIP, 0);
EndPoint ep = (EndPoint)ipEndPoint;
_socket.Bind(ep);
byte[] inbuffer = new byte[64 * 1024];
_socket.SendTo(buffer, bufferLength, SocketFlags.None, (EndPoint)netPeer);
Thread.Sleep(4000);
if (_socket.Available > 0)
{
recv = _socket.ReceiveFrom(inbuffer, ref remote);
}
_socket.Close();
The above code is created in 4 threads and each socket is always sending and receiving data. After sending 1 data request and response, the socket gets Closed. A new one is created for the next SNMP request. Socket blocking is true. TTL is 128 by default.
This works for 90% of the SNMP request. However for 1 particular set of SNMP requests(10%), I see that the SNMP _socket.Available always returns 0. The crazy thing is that I can see the data SNMP respsonse come back in Wire-Shark. I do see the response in Wireshark an there is no difference in the raw wireshark data when _socket.Available >0 or _socket.Available = 0. This 10% of SNMP request actually work sometimes and doesn't work sometime. I do see the same data in wire-shark both times.
I tried a lot of troubleshooting but the bottom line is that I see the data response in Wireshark but the code still shows _socket.Available=0. I was thinking that maybe that one of the other sockets is stealing the data? Is that possible even though the socket is binding to another network card?
I also tried the below code where I ignored the _socket.Available and use Timeouts instead of Sleeping. I get the same result.
IPEndPoint ipEndPoint = new IPEndPoint(_localIP, 0);
EndPoint ep = (EndPoint)ipEndPoint;
_socket.Bind(ep);
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeout);
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, timeout);
_socket.SendTo(buffer, bufferLength, SocketFlags.None, (EndPoint)netPeer);
recv = _socket.ReceiveFrom(inbuffer, ref remote);
I was going to have only one socket run at a single time but that will slow down the program.
I was also going to try 2 sockets, one for send and one fore receive to see if it fixes it.
Any ideas? I am using a lot of the code in SNMPSharpNet library actually. Please let me know if you have any insight into this issue or any suggestions.
Thanks!!