Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all, I programmed a network program that uses UDP protocol. The program simply send text to other program and the other program received and respond with answer.
The problem is I have multiple forms the two forms uses the same port and for that my Visual Studio give an exception that I can't use same protocol/network/ or session.
So, after I finish working with the first form close it at this event the port must closed and the second form works with the same port that opened in the first form.

VB
' this object is thread that receives data.
    Dim t As New Threading.Thread(AddressOf ReceiveMessage)
' this function will start the listenning
    Public Function Listen(ByVal port As Integer) As Boolean
        _LPort = port
        _IsListening = True
        _Timer.Interval = 100
        _Timer.Start()
        _IPEP = New IPEndPoint(IPAddress.Any, _LPort)
        _UDPlsn = New UdpClient(_IPEP)
        done = False
        t.Start()
    End Function
' this function will stop listenning
    Public Sub StopListen()
        IsListening = False
        done = True
        t.Abort()
    End Sub
' this sub is the thread that receive messages from other program.
    Private Sub ReceiveMessage()
        Try
            Do
                _Data = _UDPlsn.Receive(_IPEP)
            Loop Until done
        Catch ex As Exception

        End Try
        Threading.Thread.CurrentThread.Abort()
    End Sub


How can I close the port in C# or VB.net ?????
Posted
Updated 31-Oct-12 0:00am
v2
Comments
Timberbird 31-Oct-12 4:29am    
Have you tried UdpClient.Close()?
[no name] 31-Oct-12 5:09am    
Yes, but useless!!
Richard MacCutchan 31-Oct-12 6:00am    
What does that mean?
[no name] 31-Oct-12 6:10am    
I have tried UDPclient.close() but the problem stay same. The VS.NET give an error exception that I can't use same port/protocol or session in mutilple forms.
Timberbird 31-Oct-12 8:00am    
And have you checked socket state after that? Using netstat command you can check whether socket is actually closed - it often stays in CLOSE_WAIT for some time after closing, and that may be the reason you can't use the same address/port

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900