Click to See Complete Forum and Search --> : how to create Winsock Datar Arrival Time out event?
ehality
Jan 23rd, 2008, 12:44 PM
Hi,
I am building an application which uses Winsock to send data over UDP (i.e I don't reserve a socket I just send data and recive it by binding ports)
My problem is I have to buil a logic that will interact based on wiether data arrived or not
Data Arrival Event is working great for me but what about the opposite case when data is not recived how can I tell?
I tried to enable timers at the same time I send the data but I think they get inturrupted when the data arrival event fires or some thing I don't understand happening
I was thinking of Data Arrival Time out Event (or Data not Arrived Flag) but I don't know how to build one!
I hope that some one can help me with that
Thanks in Advance
Hack
Jan 24th, 2008, 08:22 AM
Welcome to the forums. :wave:
Where are you sending the data? In other words, who is doing the receiving?
Shaggy Hiker
Jan 24th, 2008, 12:01 PM
A timer seems to be the only option. You would enable the timer when you send the data, and disable it in the Data Arrival Event handler. The fact of the timer being enabled would be a data sent flag, effectively.
However, you state that you are having problems with that implementation. Could you give us more specifics on the problem? It sounds a bit odd.
With UDP, if a packet HAS to get through, then the receiver has to echo something back to the sender. With UDP, I generally just echo the packet back to the sender. Thus, for a critical message, I might send the message every N milliseconds until I get an echo back from the target, at which time I stop sending.
opus
Jan 25th, 2008, 12:31 AM
The way Shaggy presented will help you if your application is sending messages over the UDP-connection not constantly.
However if you use the UDP-connection to send constantly, the receiving end could use an own timer to check if a new message arrived in time or not.
Use a global variable to save the time when a message arrives, reset in the DataArrival event.
Use a Timer_event to check if the difference between this saved arrivaltime and the actual-time is over your limit.
DigiRev
Jan 29th, 2008, 03:32 AM
As others have said, this is a job for the timer control, since Winsock doesn't support any kind of timeout for not receiving data.
You can either start the timeout counter after you send data, or immediately after the previous data arrival event. You'll actually want to do it for both. Here's a rough example. I haven't tested it, and it's been quite awhile since I've messed with VB, but hopefully it works or you can grasp the idea:
Option Explicit
'Enter the number of seconds before it "times out".
Private Const TIMEOUT_SECONDS As Integer = 10
'The number of seconds that have passed (the timeout counter).
Private intSeconds As Integer
'Sending the data.
Private Sub cmdSendData_Click()
'Disable timeout timer and reset counter.
tmrTimeOut.Enabled = False
intSeconds = 0
'Send the data.
With Winsock1
.Protocol = sckUDPProtocol
.RemoteHost = "localhost"
.RemotePort = 1234
.SendData "Hello, world!"
End With
'Start counting down for the timeout.
tmrTimeOut.Interval = 1000
tmrTimeOut.Enabled = True
End Sub
'Set up some stuff.
Private Sub Form_Load()
tmrTimeOut.Enabled = False
tmrTimeOut.Interval = 1000 'Fires every second.
End Sub
'The timeout counter. This event fires every second.
Private Sub tmrTimeOut_Timer()
intSeconds = intSeconds + 1
If intSeconds = TIMEOUT_SECONDS Then
MsgBox "TIMEOUT ENCOUNTERED!"
Winsock1.Close 'I don't think this works for UDP? But you get the idea...
tmrTimeOut.Enabled = False
End If
Me.Caption = "Timeout in " & TIMEOUT_SECONDS - intSeconds & IIf(TIMEOUT_SECONDS - intSeconds = 1, " second", " seconds")
End Sub
'Receiving data...
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString, bytesTotal
'Reset timer/counter.
tmrTimeOut.Enabled = False
intSeconds = 0
tmrTimeOut.Enabled = True
End Sub
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.