Results 1 to 5 of 5

Thread: how to create Winsock Datar Arrival Time out event?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    3

    Question how to create Winsock Datar Arrival Time out event?

    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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to create Winsock Datar Arrival Time out event?

    Welcome to the forums.

    Where are you sending the data? In other words, who is doing the receiving?

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: how to create Winsock Datar Arrival Time out event?

    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.
    My usual boring signature: Nothing

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: how to create Winsock Datar Arrival Time out event?

    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.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: how to create Winsock Datar Arrival Time out event?

    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:

    vb Code:
    1. Option Explicit
    2.  
    3. 'Enter the number of seconds before it "times out".
    4. Private Const TIMEOUT_SECONDS As Integer = 10
    5. 'The number of seconds that have passed (the timeout counter).
    6. Private intSeconds As Integer
    7.  
    8. 'Sending the data.
    9. Private Sub cmdSendData_Click()
    10.     'Disable timeout timer and reset counter.
    11.     tmrTimeOut.Enabled = False
    12.     intSeconds = 0
    13.    
    14.     'Send the data.
    15.     With Winsock1
    16.         .Protocol = sckUDPProtocol
    17.         .RemoteHost = "localhost"
    18.         .RemotePort = 1234
    19.         .SendData "Hello, world!"
    20.     End With
    21.    
    22.     'Start counting down for the timeout.
    23.     tmrTimeOut.Interval = 1000
    24.     tmrTimeOut.Enabled = True
    25. End Sub
    26.  
    27. 'Set up some stuff.
    28. Private Sub Form_Load()
    29.     tmrTimeOut.Enabled = False
    30.     tmrTimeOut.Interval = 1000 'Fires every second.
    31. End Sub
    32.  
    33. 'The timeout counter. This event fires every second.
    34. Private Sub tmrTimeOut_Timer()
    35.     intSeconds = intSeconds + 1
    36.     If intSeconds = TIMEOUT_SECONDS Then
    37.         MsgBox "TIMEOUT ENCOUNTERED!"
    38.         Winsock1.Close 'I don't think this works for UDP? But you get the idea...
    39.         tmrTimeOut.Enabled = False
    40.     End If
    41.     Me.Caption = "Timeout in " & TIMEOUT_SECONDS - intSeconds & IIf(TIMEOUT_SECONDS - intSeconds = 1, " second", " seconds")
    42. End Sub
    43.  
    44. 'Receiving data...
    45. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    46.     Dim strData As String
    47.     Winsock1.GetData strData, vbString, bytesTotal
    48.    
    49.     'Reset timer/counter.
    50.     tmrTimeOut.Enabled = False
    51.     intSeconds = 0
    52.     tmrTimeOut.Enabled = True
    53. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width