none
Only one usage of each socket address (protocol/network address/port) is normally permitted - C# RRS feed

  • Question

  • Hi i encounter the above error when i tick and untick the checkbox on my form. my code below

      public void udpListen()
            {
                try
                {
                    //e = new EmailSetUp();
                    //e.ShowDialog();
                    //rE = e.getEmail();
                    //Debug.Write(rE);
                    // Construct a socket and bind it to the trap manager port 162 
                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
                    EndPoint ep = (EndPoint)ipep;
                    socket.Bind(ep);
                    // Disable timeout processing. Just block until packet is received 
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
               
                bool run = true;
                while (run)
                {
                    byte[] indata = new byte[16 * 1024];
                    // 16KB receive buffer 
                    int inlen = 0;
                    IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint inep = (EndPoint)peer;
                    try
                    {
                        inlen = socket.ReceiveFrom(indata, ref inep);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception {0}", ex.Message);
                        inlen = -1;
                    }

    my checkbox code 

     if (chkTraps.Checked)
                {
                    trap = new Traps();
                    
                    Thread oThread = new Thread(new ThreadStart(trap.udpListen));
                    oThread.Start();
                    
                } 

    Thursday, November 21, 2013 3:57 PM

Answers

  • You can't have two different sockets listening on the same port number.  The listener should only be started once.  Usually you put the code in the class constructor as an async event so you can continue runnning other code whiee you wait for incoming data (non blocking) .  You code is blocking.  You could simple have a a test that doens't start the listener if it is already started.

    A form project is automatically blocking.  There is a built in infinite loop so the program doesn't terminate.  All get gets run off of some event.  Your code added a 2nd block to the form whichisn't required.

    The website below shows the async listener class.  The code is meants for a console application, and in a form project you can eliminate the ManualResetEvent.  Simple call the function DoBeginAcceptTcpClient once in your code to start the listener and let the function return without blocking to the parent function.  Every time a receive message ocurs the code will execute the event DoAcceptTcpClientCallback.


    jdweng

    Thursday, November 21, 2013 6:56 PM

All replies

  • Your codes does not seem complete (no Catch or finally block for the first try and the while has no closing bracket).

    I am no Network specialist, but I guess some other programm is already listening on this Port/Socket. It could also be that you did not properly unregister the port in an earlier attempt/execution of this code. Or that a different isntance of the Thread is still running. (as far as we know from your code the while loop is infinite).

    Sockets are propably unmanaged resources, so you should use a using statment or a finally block to properly Dispose of them after use.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


    Thursday, November 21, 2013 4:33 PM
  • i have brackets but the code is quite long do i didnt paste everything just a snippet of code here. but my socket is inside the try. i tried using the finally but it say "object reference not set to an instance of an object"
    Thursday, November 21, 2013 4:38 PM
  • You arre creating a new socket every time you runn the code.  Try something like the changes below.  You while() loop never exists so you will not be able to run the code a second time until you remove the endless loop

            static Socket socket;
             public void udpStart()
            {
                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
                    EndPoint ep = (EndPoint)ipep;
                    socket.Bind(ep);
                    // Disable timeout processing. Just block until packet is received 
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);      
            }
             public void udpListen()
            {
                try
                {
                    //e = new EmailSetUp();
                    //e.ShowDialog();
                    //rE = e.getEmail();
                    //Debug.Write(rE);
                    // Construct a socket and bind it to the trap manager port 162 
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception {0}", ex.Message);
                    int inlen = -1;
                }
                bool run = true;
                while (run)
                {
                    byte[] indata = new byte[16 * 1024];
                    // 16KB receive buffer 
                    int inlen = 0;
                    IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint inep = (EndPoint)peer;
                    try
                    {
                        inlen = socket.ReceiveFrom(indata, ref inep);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception {0}", ex.Message);
                        inlen = -1;
                    }
                }
                
            }


    jdweng


    Thursday, November 21, 2013 4:39 PM
  • i tried ur code if i create a new method my checkbox code will only run the udpListen() because the udpListen to is to listen for any incomming traps
    Thursday, November 21, 2013 4:47 PM
  • You can't have two different sockets listening on the same port number.  The listener should only be started once.  Usually you put the code in the class constructor as an async event so you can continue runnning other code whiee you wait for incoming data (non blocking) .  You code is blocking.  You could simple have a a test that doens't start the listener if it is already started.

    A form project is automatically blocking.  There is a built in infinite loop so the program doesn't terminate.  All get gets run off of some event.  Your code added a 2nd block to the form whichisn't required.

    The website below shows the async listener class.  The code is meants for a console application, and in a form project you can eliminate the ManualResetEvent.  Simple call the function DoBeginAcceptTcpClient once in your code to start the listener and let the function return without blocking to the parent function.  Every time a receive message ocurs the code will execute the event DoAcceptTcpClientCallback.


    jdweng

    Thursday, November 21, 2013 6:56 PM