.NET C#SNMP UDP套接字错误 [英] .NET C# SNMP UDP Socket Bug

查看:98
本文介绍了.NET C#SNMP UDP套接字错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请耐心等待这个问题,并通读到最后。我将非常感激!!!



我在一台带有4个网络适配器的计算机上创建4个插槽,并将每个插槽绑定到1个网络适配器。

网络适配器1位于192.168.100.10。套接字1绑定到网络适配器1

网络适配器2位于192.168.100.20。套接字2绑定到网络适配器2

网络适配器3位于192.168.100.30。套接字3绑定到网络适配器3

网络适配器4位于192.168.100.40。套接字4绑定到网络适配器4

下面是我使用的代码。



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;





每个网络适配器都有一个本地网络,其中一个设备的IP为192.168。 100.1。我将SNMP(UDP协议)请求数据发送到此设备。

套接字1将数据发送到设备1(192.168.100.1),同时绑定到192.168.100.10网络适配器1

套接字2将数据发送到设备2(192.168.100.1),同时绑定到192.168.100.20网络适配器2

套接字3将数据发送到设备3(192.168.100.1),同时绑定到192.168.100.30网络适配器3

套接字4将数据发送到设备4(192.168.100.1),同时绑定到192.168.100.40网络适配器4





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); //bufer has the snmp request
Thread.Sleep(4000);
if (_socket.Available > 0)
{
    recv = _socket.ReceiveFrom(inbuffer, ref remote);
}
_socket.Close();





上面的代码是用4个线程创建的,每个套接字都是总是发送和接收数据。发送1个数据请求和响应后,套接字将关闭。为下一个SNMP请求创建一个新的。套接字阻塞是真的。 TTL默认为128.



这适用于90%的SNMP请求。但是对于一组特定的SNMP请求(10%),我看到SNMP _socket.Available总是返回0.疯狂的是我可以看到数据SNMP respsonse回到Wire-Shark。我确实看到了Wireshark中的响应,当_socket.Available> 0或_socket.Available = 0时,原始wireshark数据没有区别。这10%的SNMP请求实际上有时会工作,并且在某些时候不起作用。我确实在线鲨中看到了相同的数据。



我尝试了很多故障排除,但最重要的是我看到Wireshark中的数据响应但是代码仍显示_socket.Available = 0。我在想,也许其他套接字之一正在窃取数据?即使套接字绑定到另一个网卡,这可能吗?



我也尝试了下面的代码,我忽略了_socket.Available并使用Timeout而不是Sleeping。我得到了相同的结果。



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







我一次只能运行一个套接字,但这会减慢程序的速度。



我是还要尝试2个插座,一个用于发送,一个用于接收以查看是否修复它。



有什么想法吗?我实际上在SNMPSharpNet库中使用了很多代码。如果您对此问题或任何建议有任何见解,请告诉我。





谢谢!!




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!!

推荐答案

可能会回答我的一些问题的好文章

http://msdn.microsoft.com/en-us/magazine/cc163648.aspx [ ^ ]



在我的代码中我使用syrnchronous ReceiveFrom函数而不是asyncronous BeginReceiveFrom 。使用同步版本是否有任何问题,因为我正在发送一些数据,只有1个返回缓冲区。



根据这篇文章,它说UDP将丢弃数据包即使是暂时的收据,BeginReceiveFrom在套接字上也不活动。这是在可靠性委员会的标题下。它继续说'接受数据包和从另一个BeginReceiveFrom调用之间有一段很短的时间。即使这个调用是MessageReceivedCallback中的第一个,也就是应用程序没有正在监听的时间很短。

我粘贴代码形式链接以供参考 -

Nice article which might answer some of my questions
http://msdn.microsoft.com/en-us/magazine/cc163648.aspx[^]

In my code I am using syrnchronous ReceiveFrom function instead of the asyncronous BeginReceiveFrom. Is there any problem using the syncronous version as I am sending some data and only expection 1 returned buffer.

According to this article, it says that UDP will drop packets upon receipt if even momentarily, BeginReceiveFrom is not active on the socket. This is under the heading 'Reliability Procotols'. It goes on to say that 'there is a short span of time between acceptance of a packet and calling from another BeginReceiveFrom. Even if this call wre the first one in MessageReceivedCallback, there's a short period when the app isn't listening'.
I pasted the code form the link for reference below-
Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
EndPoint bindEndPoint = new IPEndPoint(IPAddress.Any, ProtocolPort);
byte[] recBuffer = new byte[PlayerInfo.MaxWireSize];
receiveSocket.Bind(bindEndPoint);
receiveSocket.BeginReceiveFrom(recBuffer, 0, recBuffer.Length, SocketFlags.None, ref bindEndPoint, new AsyncCallback(MessageReceivedCallback), (object)this);
void MessageReceivedCallback(IAsyncResult result) { EndPoint remoteEndPoint = new IPEndPoint(0, 0); 
try {
int bytesRead = receiveSocket.EndReceiveFrom(result, ref remoteEndPoint); player.FromBuffer(recBuffer, 0, Math.Min(recBuffer.Length, bytesRead));
} 
catch (SocketException e) 
{
Console.WriteLine("Error: {0} {1}", e.ErrorCode, e.Message); 
} 
receiveSocket.BeginReceiveFrom(recBuffer, 0, recBuffer.Length, SocketFlags.None, ref bindEndPoint, new AsyncCallback(MessageReceivedCallback), (object)this); }



在我的代码中,我使用相同的套接字进行发送和接收。我被认为对IP的绑定功能会自动缓冲到达该网卡的所有数据包,这样即使没有立即调用BeginReceive或ReceiveFrom函数,数据也会在缓冲区中。无论如何,我认为这对于TCP / IP连接是正确的。这是不同的UDP ????


In my code, I am using the same socket for sending and recieve. I was led to believe that the bind fuction to the IP would automatically buffer all the packets arriving at that network card so that even if the BeginReceive or ReceiveFrom function is not called immediately, the data would be in the buffer. This I believe is correct for TCP/IP connections anyway. Is this different for UDP????


该解决方案最终修复了测试PC上的路由,以确保所有IP正在与正确的网络接口通信。
The solution turned out to be fixing the routes on the Test PC to make sure all the IP's were talking to correct network interfaces.


这篇关于.NET C#SNMP UDP套接字错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆