I am using sockets to send message between computers. I send the messages alright. What I want is when the message "x0x" comes, the client must be closed within 5 minutes. Teh admin can cancel the request by sending message "-xx" as well. The routine that checks for incoming connections and accepts messages uses WHILE TRUE...END WHILE since it must always listen. Once I get a message, I need to fire a timer control or other that fires within 5 minutes. But the program cant leave the WHILE TRUE at all. If i exit it early, the timer fires and the program does get exit. How do I fire the timer or other routine while still the WHILE TRUE routine checks for incomin connections and messages?
Server-Client Program help
Collapse
X
-
Start the timer and have the timer's elapsed event handler set a flag that tells your main loop to quit. Then, in your main while loop check the state of the flag. When the flag is set, it is time to quit.
Example code follows (this is UNTESTED, so there may be bugs still):
Code:Public Class Main Private _quitFlag As Boolean Private Sub TimeToQuit(ByVal o As Object, ByVal e As Timers.ElapsedEventArgs) _quitFlag = True End Sub Private quitTimer As Timers.Timer Public Sub Main() _quitFlag = False While True If (_quitFlag) Then Exit While End If 'look for condition that causes quitTimer to start StartQuitTimer() End While End Sub Private Sub StartQuitTimer() quitTimer = New Timers.Timer(5 * 60 * 1000) AddHandler quitTimer.Elapsed, AddressOf TimeToQuit quitTimer.Start() End Sub End Class
-
Thats the thing...if I exit it, then there won't be a way to check for incoming messages. What if the admin wants to cancel the previous command (.e. to exit the application). The while true must be active always, you know. Isnt it possible to have a timer or other function for that matter to work while the while loop contines as well?
I tired the code below which avoids calling timer or so but after first message has arrived, the boolean check at the bottom dont increase the value of inttime variable.
Sorry for my english...
Code:Try While True Me.Text = (blnautocloseapp) & " " & TimeOfDay Dim clientListener As New TcpListener(ipAddress, 8505) clientListener.Start() mySocket = clientListener.AcceptSocket() mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None) str = System.Text.Encoding.ASCII.GetString(recieveBuff, 0, recieveBuff.Length).Trim(Microsoft.VisualBasic.ChrW(0)) If str <> "" Then str = understandSentMessage(str) NotifyIcon1.Text = "DoubleS Library Manager Notification Window" NotifyIcon1.ShowBalloonTip(1000, "DoubleS Library Manager: Administrator Message", str, ToolTipIcon.Info) 'close app or what? If blnautocloseapp = False Then 'auto end program. inttime = 0 End If End If mySocket.Close() mySocket.Dispose() clientListener.Stop() If blnautocloseapp = True Then inttime = inttime + 1 MsgBox(inttime) If inttime = 2 Then MsgBox("Good bye!") End End If End If End While Catch ex As Exception MsgBox(ex.Message) End Try
Comment
-
Thats the thing...if I exit it, then there won't be a way to check for incoming messages. What if the admin wants to cancel the previous command (.e. to exit the application). The while true must be active always, you know. Isnt it possible to have a timer or other function for that matter to work while the while loop contines as well?
I tired the code below which avoids calling timer or so but after first message has arrived, the boolean check at the bottom dont increase the value of inttime variable.
Sorry for my english...
Code:Try While True Me.Text = (blnautocloseapp) & " " & TimeOfDay Dim clientListener As New TcpListener(ipAddress, 8505) clientListener.Start() mySocket = clientListener.AcceptSocket() mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None) str = System.Text.Encoding.ASCII.GetString(recieveBuff, 0, recieveBuff.Length).Trim(Microsoft.VisualBasic.ChrW(0)) If str <> "" Then str = understandSentMessage(str) NotifyIcon1.Text = "DoubleS Library Manager Notification Window" NotifyIcon1.ShowBalloonTip(1000, "DoubleS Library Manager: Administrator Message", str, ToolTipIcon.Info) 'close app or what? If blnautocloseapp = False Then 'auto end program. inttime = 0 End If End If mySocket.Close() mySocket.Dispose() clientListener.Stop() If blnautocloseapp = True Then inttime = inttime + 1 MsgBox(inttime) If inttime = 2 Then MsgBox("Good bye!") End End If End If End While Catch ex As Exception MsgBox(ex.Message) End Try
Comment
-
So even after your program receives the "exit" code and 5 minutes has elapsed, you still want to continue listening for more commands?Comment
Comment