c# – ERROR: "normally only one use of each socket address is allowed"


Before they say "the port is already in use, try another one", I've gotten this answer several times, and switching ports just doesn't work.

Well, I'm developing a game that uses Socket via UDP to transmit data over the network, but every time I instantiate the UdpClient class it gives me the following error:

"Normally only one use of each socket address (protocol/network address/port) is allowed"

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)
                    listener = new UdpClient(port);
                    tryagain = false;
                catch (Exception e)
                    tryagain = true;
            ips = new List<string>();
            Thread thread = new Thread(new ThreadStart(run));

I did a loop when instantiating my listener, but it stays in the loop forever, the port variable is currently 11022, but I've already tested it with 11000, 11001, 11002, 11003, 1234, 7654, 9999, 123 among others I I do not remember.

I also tried to access the command netstat 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 netstat, and after a while disappears.

I was also instructed to change a registry called ReservedPorts in the path HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/services/Tcpip/Parameters by adding my port, but it doesn't seem like this registry exists

In fact, this error only happens sometimes, when I change computer or folder, the first few times the program manages to access the port normally, then it seems like it can't. Maybe I'm not closing the door properly, but my code has this part:

 public void OnShutdown(Component.ShutdownContext context)
        end = true;


so the door should theoretically be closed when finishing the program, right?

What am I doing wrong?


There is a listener binding to the port port in exclusive mode. To indicate shared binding, launch your UdpClient as follows:

listener udpServer = new UdpClient();

    SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

Scroll to Top