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

Asked

Viewed 5,784 times

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.

  • How are you declaring your Istener?

  • Udpclient Listener; as a global variable

1 answer

0


There is a Listener binding to the door port in exclusive mode. To indicate shared binding, initialize your Udpclient as follows:

listener udpServer = new UdpClient();

listener.Client.SetSocketOption(
    SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

listener.Client.Bind(localpt);
  • It worked! But the line;listener.Client.Bind(localpt); gives invalid argument error, but just taking it seems to make it work

  • @vilok600 the 3rd line is well dependent on its implementation - there is no problem in removing it. I’m glad it worked!

  • Well, I think it was a false alarm, it worked 3 times, but now it stopped working again :/ As I’m trying to implement multiplayer, I have two test computers, a desktop and a notebook, the funny thing is that the two are accessing the same project file, located on the desktop, but the notebook works perfectly, did not give error even once, so maybe this is the same desktop problem itself, or the kind of folder mapping, sei la

Browser other questions tagged

You are not signed in. Login or sign up in order to post.