Results 1 to 7 of 7

Thread: Step through app, stop, restart, now get "Only one useage of socket permitted"

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    67

    Step through app, stop, restart, now get "Only one useage of socket permitted"

    So I'm opening a TCPListener on socket 8000. Debugging code, finding bugs, stopping the app and debugging again.

    Somehow I'm causing a problem such that it's not closing the TCP port, and it's causing a major problem such that even after I stop my program and restart it the code to open the TCP port the app is still throwing an exception, and it keeps happening until I reboot.

    It's going to be real hard to find the bug when I have to reboot each time to debug the app!

    I used netstat -b and it does Not show port 8000 open.

    Of course I can try changing the port each time I run the app... But I really want to know why the app is still throwing an exception on a fresh run and netstat doesn't show the port open?

    Help. And Thanks!

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    67

    Re: Step through app, stop, restart, now get "Only one useage of socket permitted"

    I've found it only happens if Stop the program using the Stop button in Visual Studio Or if my application terminates without first executing the code that closes the TCPListener. If my program exits properly it does not occur.

    Edit: Shouldn't the built-in garbage collection handle closing the port properly when an unexpected termination occurs where I have no way of handling the termination (such as an End Task)?

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Step through app, stop, restart, now get "Only one useage of socket permitted"

    Where is the code that closes the listener located?

    I can't say that I have ever had this problem, though that might be that I don't use TCP much, but I have used UDP a fair amount, and that listens to ports, and has a similar issue. In my case, the easiest solution was to change a setting on the socket such that there can be multiple shared users of the same port. I had to do that because I had multiple modules communicating via UDP on the same port on the same computer. That solution might work for you (it can be found by searching the networking forum for threads started by me, and having the word UDP, it is the thread with 7-8 replies).

    However, that's not a good solution in this case, because you have an actual problem, and getting around the problem by sharing a port is nothing but a hack (and not the super moderator here, either). A better solution would be to solve the problem, which is why I asked that first question. If your TCP is in a class, then does it implement IDisposable, and is the socket closing code in the Dispose method? If not, then I think it should be, or at least the Dispose method should ensure that the socket is closed.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    67

    Re: Step through app, stop, restart, now get "Only one useage of socket permitted"

    Quote Originally Posted by Shaggy Hiker View Post
    Where is the code that closes the listener located?

    I can't say that I have ever had this problem, though that might be that I don't use TCP much, but I have used UDP a fair amount, and that listens to ports, and has a similar issue. In my case, the easiest solution was to change a setting on the socket such that there can be multiple shared users of the same port. I had to do that because I had multiple modules communicating via UDP on the same port on the same computer. That solution might work for you (it can be found by searching the networking forum for threads started by me, and having the word UDP, it is the thread with 7-8 replies).

    However, that's not a good solution in this case, because you have an actual problem, and getting around the problem by sharing a port is nothing but a hack (and not the super moderator here, either). A better solution would be to solve the problem, which is why I asked that first question. If your TCP is in a class, then does it implement IDisposable, and is the socket closing code in the Dispose method? If not, then I think it should be, or at least the Dispose method should ensure that the socket is closed.
    I'm checking and closing the socket in 2 places:
    1) At a subroutine that handles application exit
    2) The socket runs in a thread, and I have a ThreadDone event which gets triggered when the thread completes, which also closes the socket.

    Yes, the socket is in a class. No, I've not implemented IDisposable. I've never used it before cause I didn't know why I would want to use it... I guess now I know one reason

    So in IDisposable, basically I'll just check if tcplistener is nothing and if not, close it? Seems easy enough.

    Thanks!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    67

    Re: Step through app, stop, restart, now get "Only one useage of socket permitted"

    Looking at this link: http://msdn.microsoft.com/en-us/libr...isposable.aspx

    I'm trying to understand Dispose... But it looks like if I implement it I also have to manually dispose of all managed resources too?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    67

    Re: Step through app, stop, restart, now get "Only one useage of socket permitted"

    I REALLY need some help with this same issue. I've tried everything I can think of, and nothing is working.

    Here's some basic code, and what happens is that if it crashes or the program in any way stops unexpectedly, then run the program again, I get the error "Only one useage of socket permitted". However the socket is NOT open! Netstat -b does Not list the port. Even if I then exit the program properly (which calls oComms.Stop()) I still get the error the next time I run the program. Note that this code has been cut down to make it easier to read...

    I have even tried implementing idisposable to no avail.

    Code:
    In main form:
        Dim oThread
        Dim oComms
        Public Sub StartThread()
            If oComms Is Nothing Then
                oComms= New myDataComms()
            End If
            'exit sub if thread already running and not done
            If Not oThread Is Nothing Then
                If oThread.ThreadState = ThreadState.Running Or oThread.ThreadState = ThreadState.Suspended Or oThread.ThreadState = ThreadState.WaitSleepJoin Then
                    Exit Sub
                End If
            End If
            oThread = New Thread(AddressOf oComms.Listen)
            oComms.run = True
            oThread.Start()
        End Sub
    
    Public Class myDataComms
        'Implements IDisposable
        Private disposed As Boolean = False
    
        Dim tcpListener As TcpListener
        Dim tcpClient As TcpClient
        Dim networkStream As NetworkStream
        Public run As Boolean = False
        Public Event ThreadProcess()
        Public Event ThreadDone()
    
        Public Sub Stop()
            run = False
            If (Not IsNothing(tcpListener)) Then
                Try
                    If tcpListener.Server.IsBound Then
                        tcpListener.Server.Close()
                    End If
                Catch ex As Exception
                End Try
                tcpListener.Stop()
            End If
            If (Not IsNothing(networkStream)) Then
                networkStream.Close()
            End If
            If (Not IsNothing(tcpClient)) Then
                tcpClient.Close()
            End If
        End Sub
        Public Sub Listen()
            Try
                tcpListener = New TcpListener(IPAddress.Any, 8001)
                'tcpListener.ExclusiveAddressUse = True
                tcpListener.Start()
                While run
                    If tcpListener.Pending Then
                        'Accept the pending client connection and return a TcpClient initialized for communication. 
                        tcpClient = tcpListener.AcceptTcpClient()
                        ' Get the stream
                        networkStream = tcpClient.GetStream()
                        ' Read the stream into a byte array
                        Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                        'networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                        '' Return the data received from the client to the console.
                        
                        Dim myCompleteMessage As StringBuilder = New StringBuilder()
    
                        Do
                            numberOfBytesRead = networkStream.Read(bytes, 0, bytes.Length)
                            myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(bytes, 0, numberOfBytesRead))
                        Loop While networkStream.DataAvailable
    
                        Dim responseString As String = "blah blah"
                                RaiseEvent ThreadProcess()
                            End If
                        Catch ex As Exception
                            responseString = "error"
                        End Try
    
                        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
                        networkStream.Write(sendBytes, 0, sendBytes.Length)
                        'Any communication with the remote client using the TcpClient can go here.
                        'Close TcpListener and TcpClient.
                        networkStream.Close()
                        tcpClient.Close()
                    End If
                    Thread.Sleep(200)
                End While
    
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            RaiseEvent ThreadDone()
        End Sub
        '' Implement IDisposable.
        '' Do not make this method virtual.
        '' A derived class should not be able to override this method.
        'Public Overloads Sub Dispose() Implements IDisposable.Dispose
        '    Stop()
        '    Dispose(True)
        '    ' This object will be cleaned up by the Dispose method.
        '    ' Therefore, you should call GC.SupressFinalize to
        '    ' take this object off the finalization queue 
        '    ' and prevent finalization code for this object
        '    ' from executing a second time.
        '    GC.SuppressFinalize(Me)
        'End Sub
        '' Dispose(bool disposing) executes in two distinct scenarios.
        '' If disposing equals true, the method has been called directly
        '' or indirectly by a user's code. Managed and unmanaged resources
        '' can be disposed.
        '' If disposing equals false, the method has been called by the 
        '' runtime from inside the finalizer and you should not reference 
        '' other objects. Only unmanaged resources can be disposed.
        'Private Overloads Sub Dispose(ByVal disposing As Boolean)
        '    ' Check to see if Dispose has already been called.
        '    If Not Me.disposed Then
        '        ' If disposing equals true, dispose all managed 
        '        ' and unmanaged resources.
        '        If disposing Then
        '            ' Dispose managed resources.
        '        End If
    
        '        ' Call the appropriate methods to clean up 
        '        ' unmanaged resources here.
        '        ' If disposing is false, 
        '        ' only the following code is executed.
        '        Stop()
    
        '        ' Note disposing has been done.
        '        disposed = True
    
        '    End If
        'End Sub
        '' This finalizer will run only if the Dispose method 
        '' does not get called.
        '' It gives your base class the opportunity to finalize.
        '' Do not provide finalize methods in types derived from this class.
        'Protected Overrides Sub Finalize()
        '    ' Do not re-create Dispose clean-up code here.
        '    ' Calling Dispose(false) is optimal in terms of
        '    ' readability and maintainability.
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub
    
    End Class

  7. #7

    Re: Step through app, stop, restart, now get "Only one useage of socket permitted"

    Take a look at my TCP Client & Server class in my signature. I had a similar issue but I managed to solve it by doing a few different things.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width