
Originally Posted by
Nobody77
You could make an "infinite" try catch loop until the port is no longer used, but this is a lot better than it just crashing still
e.g.
Code:
public void Bind() {
try {
ListenSocket.Bind(new IPEndPoint(IPAddress.Any, Program.Port));
ListenSocket.Listen(0xff);
ListenSocket.BeginAccept(_listenServer, null);
}
catch {
Program.ConsoleManager.WriteError($"Error binding to a socket, port {Program.Port} taken! Retrying in 5 seconds.");
Thread.Sleep(5000);
Bind();
}
}
But why would you do this? In your code you just check every 5th second wether its used or not. You could easily restart the exe in that time. The idea of this is actually also to inform people what's wrong rather than having a text wall to read through. For the experienced, sure it's not hard to find the issue, because we have seen this "wall" of text, before. Yours is a nice solution too, and it can also be used together with mine to make it all even more easy.