364,112 Members | 2289 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Server-Client Program help

samvb
100+
P: 162
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?
Jan 24 '12 #1
Share this Question
Share on Google+
5 Replies


Joseph Martell
Expert 100+
P: 165
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):
Expand|Select|Wrap|Line Numbers
  1. Public Class Main
  2.     Private _quitFlag As Boolean
  3.     Private Sub TimeToQuit(ByVal o As Object, ByVal e As Timers.ElapsedEventArgs)
  4.         _quitFlag = True
  5.     End Sub
  6.  
  7.     Private quitTimer As Timers.Timer
  8.  
  9.     Public Sub Main()
  10.         _quitFlag = False
  11.  
  12.         While True
  13.             If (_quitFlag) Then
  14.                 Exit While
  15.             End If
  16.             'look for condition that causes quitTimer to start
  17.             StartQuitTimer()
  18.  
  19.         End While
  20.  
  21.     End Sub
  22.  
  23.     Private Sub StartQuitTimer()
  24.         quitTimer = New Timers.Timer(5 * 60 * 1000)
  25.         AddHandler quitTimer.Elapsed, AddressOf TimeToQuit
  26.         quitTimer.Start()
  27.     End Sub
  28. End Class
  29.  
  30.  
Jan 24 '12 #2

samvb
100+
P: 162
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...

Expand|Select|Wrap|Line Numbers
  1. Try
  2.  
  3.             While True
  4.                 Me.Text = (blnautocloseapp) & " " & TimeOfDay
  5.  
  6.                 Dim clientListener As New TcpListener(ipAddress, 8505)
  7.                 clientListener.Start()
  8.                 mySocket = clientListener.AcceptSocket()
  9.                 mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None)
  10.                 str = System.Text.Encoding.ASCII.GetString(recieveBuff, 0, recieveBuff.Length).Trim(Microsoft.VisualBasic.ChrW(0))
  11.                 If str <> "" Then
  12.                     str = understandSentMessage(str)
  13.                     NotifyIcon1.Text = "DoubleS Library Manager Notification Window"
  14.                     NotifyIcon1.ShowBalloonTip(1000, "DoubleS Library Manager: Administrator Message", str, ToolTipIcon.Info)
  15.                     'close app or what?
  16.  
  17.                     If blnautocloseapp = False Then 'auto end program.
  18.  
  19.                         inttime = 0
  20.                     End If
  21.                 End If
  22.  
  23.                 mySocket.Close()
  24.                 mySocket.Dispose()
  25.  
  26.  
  27.  
  28.  
  29.                 clientListener.Stop()
  30.  
  31.                 If blnautocloseapp = True Then
  32.  
  33.                     inttime = inttime + 1
  34.                     MsgBox(inttime)
  35.                     If inttime = 2 Then
  36.                         MsgBox("Good bye!")
  37.                         End
  38.                     End If
  39.  
  40.                 End If
  41.             End While
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.         Catch ex As Exception
  49.             MsgBox(ex.Message)
  50.  
  51.         End Try
  52.  
Jan 25 '12 #3

samvb
100+
P: 162
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...

Expand|Select|Wrap|Line Numbers
  1. Try
  2.  
  3.             While True
  4.                 Me.Text = (blnautocloseapp) & " " & TimeOfDay
  5.  
  6.                 Dim clientListener As New TcpListener(ipAddress, 8505)
  7.                 clientListener.Start()
  8.                 mySocket = clientListener.AcceptSocket()
  9.                 mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None)
  10.                 str = System.Text.Encoding.ASCII.GetString(recieveBuff, 0, recieveBuff.Length).Trim(Microsoft.VisualBasic.ChrW(0))
  11.                 If str <> "" Then
  12.                     str = understandSentMessage(str)
  13.                     NotifyIcon1.Text = "DoubleS Library Manager Notification Window"
  14.                     NotifyIcon1.ShowBalloonTip(1000, "DoubleS Library Manager: Administrator Message", str, ToolTipIcon.Info)
  15.                     'close app or what?
  16.  
  17.                     If blnautocloseapp = False Then 'auto end program.
  18.  
  19.                         inttime = 0
  20.                     End If
  21.                 End If
  22.  
  23.                 mySocket.Close()
  24.                 mySocket.Dispose()
  25.  
  26.  
  27.  
  28.  
  29.                 clientListener.Stop()
  30.  
  31.                 If blnautocloseapp = True Then
  32.  
  33.                     inttime = inttime + 1
  34.                     MsgBox(inttime)
  35.                     If inttime = 2 Then
  36.                         MsgBox("Good bye!")
  37.                         End
  38.                     End If
  39.  
  40.                 End If
  41.             End While
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.         Catch ex As Exception
  49.             MsgBox(ex.Message)
  50.  
  51.         End Try
  52.  
Jan 25 '12 #4

Joseph Martell
Expert 100+
P: 165
So even after your program receives the "exit" code and 5 minutes has elapsed, you still want to continue listening for more commands?
Jan 25 '12 #5

samvb
100+
P: 162
hey...i managed to solve it with multi-threading, thank you so much for ur help.
Jan 27 '12 #6

Post your reply

Help answer this question



Didn't find the answer to your Visual Basic .NET question?

You can also browse similar questions: Visual Basic .NET