tcpListener dont exit

Andersson

New member
Joined
Nov 24, 2008
Messages
4
Programming Experience
1-3
Hello
I have this problem where my windows.form doesnt quit when user logoff or system shutdown. It hangs the logoff/shutdown process witch is impossible to accept in a school.

The form is always active in background and hidden until an connection is made and then shows the form and hides it on a button click. (not visible in taskbar, but in systray)

The process hangs becouse of tcpListener.Stop() in sub popupmessage_FormClosing, that i have figured out.
Anyone know how to solve this?

My code:
VB.NET:
Const portNumber As Integer = 2516
    Dim tcpListener As New TcpListener(portNumber)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Visible = False
        conn()
    End Sub

Sub disconn()
        Me.Visible = False
        Me.tcpListener.Stop()
        conn()
    End Sub
    Sub conn()
        
        Me.tcpListener.Start()
        'Console.WriteLine("Waiting for connection...")
        Try

            Me.tcpListener.AcceptTcpClient()

            Me.Visible = True
            Me.Show()
            Me.Focus()

        Catch er As Exception
            tcpListener.Stop()
        End Try
    End Sub

Private Sub popupmessage_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If e.CloseReason = CloseReason.WindowsShutDown Then
            Me.tcpListener.Stop() '[COLOR="Red"] Here is it the code freezes, Why ? oh Why?[/COLOR]
            e.Cancel = False
        End If
        End If
    End Sub
 
Most likely because it is busy at the time in the AcceptTcpClient blocking call. You can use a loop in the listener thread where you check if there is a connection Pending before you start to accept.

I say listener thread because you will need to learn multithreading when dealing with sockets, you can't block the UI thread like you are doing now, that harms the running state of the operating system.
 
Pending

Mr JohnH, thanks for the quick reply.

Sounds logic what you are saying but implement it as a newbie isnt that simple..

So i tried like this with no luck
I must connect twice to get the form to popup and at second connect the form freezes, Do you perhaps have a code for this ?

Try
Me.tcpListener.Start()
Do While True
If tcpListener.Pending Then
Me.tcpListener.AcceptTcpClient()
Dim tcpClient As TcpClient = Me.tcpListener.AcceptTcpClient()

Me.Visible = True
Me.Show()
Me.Focus()

Else

End If

Loop
Catch er As Exception
tcpListener.Stop()
End Try
 
Me.tcpListener.AcceptTcpClient()
Dim tcpClient As TcpClient = Me.tcpListener.AcceptTcpClient()
You are calling AcceptTcpClient twice, the first you don't get, so the code stops at second same as before.

You should also sleep the thread for each loop iteration, there is no need for it spin cpu cycles like a hurricane, it can mostly sleep for example in 1 second intervals. The connecting TcpClient doesn't timeout until 30 seconds or so, there is no hurry about this.
 
Tcplistener

Hello

So i skipped the showform and use a msgbox instead.

And also now i have the pending in place, but the application just refuses to stop when a logoff of shutdown is beeing initiated.

What to do now ?

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tcpListener.Start()
        Do While True

            Try

                If tcpListener.Pending = False Then

                    Thread.Sleep(2000)
                Else
                    Me.tcpListener.AcceptTcpClient()

                    Dim msg As String = "Denna utskrift har raderats." & vbCrLf & vbCrLf & vbCrLf & "Du har inga pengar kvar på ditt utskriftskonto" & vbCrLf & vbCrLf & vbCrLf & _
                    "Du kan fylla på utskriftskontot hos Morgan,Sven-Erik eller i biblioteksreceptionen"

                    Dim title As String = "Utskriftsinformation"
                    Dim style As MsgBoxStyle
                    style = MsgBoxStyle.Critical ' Or MsgBoxStyle.Critical Or MsgBoxStyle.YesNo

                    MsgBox(msg, style, title)


                    Thread.Sleep(2000)
                End If

            Catch er As Exception

                MsgBox(er.ToString)
            End Try

        Loop
        tcpListener.Stop()
    End Sub
 
Finally its working

:)
Found a code example here:
vbCity/DevCity.NET Forums :: FAQ :: .NET :: VB.NET :: Winsock
Attachment: Demo Sockets.zip (42 KB)


Final code for me:

VB.NET:
Imports System.Net.Sockets
Imports System.Windows.Forms


Public Class popupmessage


    Private _Timer As New Timer()

    Private _HostSocket As Socket

    Private _TcpListener As New TcpListener(7153)
    
    Friend WithEvents ClearMessagesButton As System.Windows.Forms.Button



    Private Sub CheckTcpListener(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Visible = False
        
        If _TcpListener.Pending = True Then
        
            _HostSocket = _TcpListener.AcceptSocket

        
            If _HostSocket.Poll(1, SelectMode.SelectRead) Then
        
                Dim numBytes As Int32 = _HostSocket.Available
                Dim Data(numBytes) As Byte
                _HostSocket.Receive(Data)

                ''DisableCloseButton(Me.Handle)
                Me.Show()
                Me.WindowState = FormWindowState.Normal

                _Timer.Stop()

            End If
        End If
    End Sub
    


    Private Sub popupmessage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Visible = False
        _Timer.Interval = 2000

        _Timer.Start()

        Me.Visible = False

        _TcpListener.Start()

        ' Add the CheckTcpListner method as the handler for _Timer Tick events
        AddHandler _Timer.Tick, AddressOf CheckTcpListener

        Me.Visible = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        _Timer.Start()
    End Sub
 
Back
Top