3
Before you say that "the door is already being used, try another one", I have received this answer several times, and changing the door simply does not work.
Well, I’m developing a game that uses Socket via UDP to transmit data over the network, but every time I ask the Udpclient class it gives me the following error:
"Normally only one use of each socket address is allowed (protocol/network address/port)"
This is my code:
public void OnInit(Component.InitContext context)
{
if (context == InitContext.Activate)
{
servers = new List<ServerInfo>();
renderers = new List<GameObject>();
end = false;
bool tryagain = true;
while (tryagain)
{
try
{
listener = new UdpClient(port);
tryagain = false;
}
catch (Exception e)
{
tryagain = true;
listener.Close();
Log.Game.Write(e.Message);
}
}
ips = new List<string>();
Thread thread = new Thread(new ThreadStart(run));
thread.Start();
}
}
I made a loop at the time of instantiating my Istener, but it stays in the loop forever, the port variable is currently 11022, but I have tested with 11000, 11001, 11002, 11003, 1234, 7654, 9999, 123 among others I do not remember.
I also tried to access the netstat command by cmd to check which ports are being used, before running the program, none of the mentioned ports appear, but after running the program and the error appears, the port then appears in the netstat, and after a while some.
I was also instructed to change a record named Reservedports in the HKEY_LOCAL_MACHINE/SYSTEM/Currentcontrolset/services/Tcpip/Parameters path by adding my port, but it seems that this record does not exist
In fact this error only happens sometimes, when I change my computer or folder, the first times the program can access the port normally, then it seems that it can’t do it anymore. Maybe I’m not closing the door right, but my code has this part:
public void OnShutdown(Component.ShutdownContext context)
{
end = true;
listener.Close();
}
then the door should theoretically be closed at the end of the program, right?
What am I doing wrong?
You don’t need to use the tag
visual-studio
when the problem is not with the IDE.– Jéf Bueno
How are you declaring your Istener?
– Marco Souza
Udpclient Listener; as a global variable
– vilok600