发送UDP ping到整个网络,回复给发送者。

huangapple go评论88阅读模式
英文:

Sending udp ping to entire network, reply to sender

问题

我目前正在尝试为我们的业务创建一个测试工具,在这个工具中,我想向网络中的所有客户端发送一个“ping” UDP消息,相应的客户端也应该回复一条消息。

我使用一个简单的消息拆分来过滤消息,并在消息中指定命令,例如Ping。

想法是在所有客户端上监听消息的服务,并在一个计算机上运行一个“服务器”应用程序,该应用程序发送ping,然后将相应可用的系统添加到列表中。

我遇到的问题是,当我在网络中的两台不同计算机上进行测试时,它不起作用,但在我本地计算机上运行两个应用程序时正常工作(可能因为它只在本地运行)。

发送ping的服务器代码:

public static void SendToClients(string message)
{
    UdpClient udpClient = new UdpClient();
    IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, Constants.CLIENT_PORT);

    var data = Encoding.ASCII.GetBytes(message);
    udpClient.Send(data, data.Length, ip);
    udpClient.Close();

    Console.WriteLine("[SERVER] Sent: " + message);
}

用于监听响应的服务器代码:

UdpClient udpClient = new UdpClient();
udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, Constants.SERVER_PORT));
var from = new IPEndPoint(0, 0);
var task = Task.Run(() =>
{
    Console.WriteLine("[SERVER] socket online");
    while (true)
    {
        var recvBuff = udpClient.Receive(ref from);
        Console.WriteLine("[SERVER] Received: " + Encoding.ASCII.GetString(recvBuff));
        parseMessage(Encoding.ASCII.GetString(recvBuff));
    }
});

客户端代码监听并发送回服务器:

UdpClient udpClient = new UdpClient();
udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, Constants.CLIENT_PORT));
var from = new IPEndPoint(0, 0);
var task = Task.Run(() =>
{
    Console.WriteLine("Socket online");
    while (true)
    {
        var recvBuff = udpClient.Receive(ref from);
        Console.WriteLine("Received: " + Encoding.ASCII.GetString(recvBuff));
        ParseMessage(Encoding.ASCII.GetString(recvBuff));
    }
});
public static void SendToServer(string message)
{
    UdpClient udpClient = new UdpClient();
    IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, Constants.SERVER_PORT);

    var data = Encoding.ASCII.GetBytes(message);
    udpClient.Send(data, data.Length, ip);
    udpClient.Close();

    Console.WriteLine("Sent: " + message);
}

我是否误解了使用udpclient的设置,或者我是否错误地使用了广播和IPAddress.Any?我似乎无法监听IPAddress.Broadcast,它会失败并显示该地址在其上下文中不可用的错误消息。

我无法在我的在线研究中找到关于如何使用套接字的问题。但我确实有一个想法,问题可能是我测试的计算机有多个网络接口。一个物理的和3-4个虚拟的,都有不同的IP地址。这可能会引起问题吗?是否有一种方法可以指定要使用的接口?

英文:

I'm currently trying to create a testing tool for our business, where I want to send out a "ping" udp message to all clients in the network, and respective clients should respond with a message as well.

I'm filtering the messages using a simple message split and in the message stating the command. Ping, for example.

The idea is to have a service listening for messages on all clients, and a "server" application on one computer that sends out the ping and then adds respective available system to a list.

Issue I'm having is that it works fine on my local computer running both applications (probably because it just works locally), but when I test this on two different computers in a network it will not work.

Server code for sending out ping:

public static void SendToClients(string message)
{
    UdpClient udpClient = new UdpClient();
    IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, Constants.CLIENT_PORT);

    var data = Encoding.ASCII.GetBytes(message);
    udpClient.Send(data, data.Length, ip);
    udpClient.Close();

    Console.WriteLine("[SERVER] Sent: " + message);
}

My server code for listening to responses:

UdpClient udpClient = new UdpClient();
udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, Constants.SERVER_PORT));
var from = new IPEndPoint(0, 0);
var task = Task.Run(() =>
{
	Console.WriteLine("[SERVER] socket online");
	while (true)
	{
		var recvBuff = udpClient.Receive(ref from);
		Console.WriteLine("[SERVER] Received: " + Encoding.ASCII.GetString(recvBuff));
		parseMessage(Encoding.ASCII.GetString(recvBuff));
	}
});

Client code listening and sending back to server:

UdpClient udpClient = new UdpClient();
udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, Constants.CLIENT_PORT));
var from = new IPEndPoint(0, 0);
var task = Task.Run(() =>
{
    Console.WriteLine("Socket online");
    while (true)
    {
        var recvBuff = udpClient.Receive(ref from);
        Console.WriteLine("Received: " + Encoding.ASCII.GetString(recvBuff));
        ParseMessage(Encoding.ASCII.GetString(recvBuff));
    }
});
public static void SendToServer(string message)
{
    UdpClient udpClient = new UdpClient();
    IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, Constants.SERVER_PORT);

    var data = Encoding.ASCII.GetBytes(message);
    udpClient.Send(data, data.Length, ip);
    udpClient.Close();

    Console.WriteLine("Sent: " + message);
}

Have I misunderstood the setup with udpclient and/or am I thinking incorrectly with broadcast and IPAddress.Any? I can't seem to listen to the IPAddress.Broadcast where it just fails and says that the address is not available in its context..

I can't seem to find a issue with this from my research online on how to use sockets. I do however have a thought that the issue might be that the computers that I tested this on has several network interfaces. One physical and 3-4 virtual ones, all with different IP-addresses. Could this cause the issue? Is there a way for me to specify which interface to work with?

答案1

得分: 0

在进一步测试其他系统后,它可以工作。最初的想法是从连接到网络的其他部分的计算机上发送ping,使用VPN连接。但那没有成功。

通过常规网络发送数据运行正常。所以我猜这是我们的设置问题,而不是代码本身的问题。

我将不得不咨询我们的网络人员,看看VPN是否受到特定数据的限制,或者研究配置。

我还注意到防火墙设置不正确。我混淆了本地和远程端口,并了解到在发送时会在本地设置一个随机端口,但远程端口将是我在代码中设置的端口(在这种情况下定义为Constants.SERVER_PORT)。在Windows防火墙中,将远程端口设置为“任何”以解决入站包问题。将本地端口设置为“任何”以解决出站包的发送问题。

英文:

After further testing on other systems it works. The original idea was to send the pings from a computer that is connected to the rest of the network with a VPN. That didn't work.

Sending the data over regular network worked fine. So guess it's an issue with our setup and not the code per se.

I will either have to consult our network staff and see if the VPN is restricted with specific data or research the configurations.

I also noticed that the firewall settings were incorrect. I mixed up local and remote ports and learned that it will set a random port locally when sending, but the remote port will be the one I set in the code. (In this case defined in Constants.SERVER_PORT. Setting the remote port to Any for inbound connections in windows firewall solved incoming packages. And local port to Any in outbound connections solved the sending of packages.

huangapple
  • 本文由 发表于 2023年7月24日 17:31:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定