Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UDP networking with multiple network

I'm currently developing a network application on my machine with 2 network interfaces with the following setup -

Current network setting

I want to send UDP message by using the first configuration with IP 192.168.1.2.

using (var udpClient = new UdpClient(5556))
{
    udpClient.Connect(IPAddress.Parse("192.168.1.2"), 5556);
    // DO STUFF
}

When I try this I get the following error -

No connection could be made because the target machine actively refused it

Strange this is that when I disable my other network that this works perfectly but with 2 connections (LAN & WiFi) it doesn't work anymore... I guess that it is sending on wrong adapter? Is this because my Default gateway is the same or what am I doing wrong? I'm new to developing network-based applications...

like image 564
Tom Kerkhove Avatar asked Oct 21 '22 22:10

Tom Kerkhove


2 Answers

You have to specify an IPEndPoint with networking card IP like this:

var endpoint = new IPEndPoint(IPAddress.Parse("192.168.1.2"), portNum);
UdpClient client = new UdpClient(endpoint);
like image 63
VladL Avatar answered Nov 04 '22 21:11

VladL


You are not telling UdpClient which IP to use.

UdpClient has a constructor that can take an IPEndPoint.

const string ip = "192.168.1.2";
const int port = 5556;

var listenEndpoint = new IPEndPoint(IPAddress.Parse(ip), port);
var udpClient = new UdpClient(listenEndpoint);
like image 39
Sam Leach Avatar answered Nov 04 '22 21:11

Sam Leach