❔ Change UDP Port

I've got a question. I'm working on a small GUI where I receive data by UDP. This already works fine. But I want to add the functionality to change the listening port. Here I struggle. Can some one help me?
public partial class Form1 : Form
{
private UdpClient udpClient;
private List<Measurment> measurments = new List<Measurment>();
private String serverIdentifier = "-temperature measurement system werner-";
private IPEndPoint receiveAdr;
private bool firtTimeReading = true;

private void btReadData_Click(object sender, EventArgs e)
{
if (firtTimeReading)
{
receiveAdr = new IPEndPoint(IPAddress.Any, int.Parse(this.nud_port.Value.ToString()));
udpClient = new UdpClient(receiveAdr);
this.ReceiveAsync();
firtTimeReading = false;
}
else
{
// Do something to change port to another value... here I need help
}
}

private async void ReceiveAsync() // async bedeutet, dass die Methode im Hintergrund ausgeführt wird
{
while (true)
{
UdpReceiveResult result = await udpClient.ReceiveAsync();
string value = Encoding.UTF8.GetString(result.Buffer);
...
}
}
public partial class Form1 : Form
{
private UdpClient udpClient;
private List<Measurment> measurments = new List<Measurment>();
private String serverIdentifier = "-temperature measurement system werner-";
private IPEndPoint receiveAdr;
private bool firtTimeReading = true;

private void btReadData_Click(object sender, EventArgs e)
{
if (firtTimeReading)
{
receiveAdr = new IPEndPoint(IPAddress.Any, int.Parse(this.nud_port.Value.ToString()));
udpClient = new UdpClient(receiveAdr);
this.ReceiveAsync();
firtTimeReading = false;
}
else
{
// Do something to change port to another value... here I need help
}
}

private async void ReceiveAsync() // async bedeutet, dass die Methode im Hintergrund ausgeführt wird
{
while (true)
{
UdpReceiveResult result = await udpClient.ReceiveAsync();
string value = Encoding.UTF8.GetString(result.Buffer);
...
}
}
1 Reply
Accord
Accord3y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server