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?
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.