Results 1 to 10 of 10

Thread: winsock data arrival

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    183

    winsock data arrival

    I made a program that communicate with some electronic device using winsock udp protocol, i am trying to write a function that will tell me rather or not a device is online by looking at if anything is sent back from the device.

    this is my function
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

    Dim strData As String

    Winsock1.GetData strData, vbString, bytesTotal

    If Not Len(strData) = 0 Then
    online = 1
    End If

    End Sub

    i declared the variable "online" as a global variable, but somehow "online" is always 0...i think i am trying to do this the wrong way, can anyone give me a hand?

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    183

    Re: winsock data arrival

    yes data does arrive, strdata is the string i receive from my devices.

  4. #4

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    183

    Re: winsock data arrival

    a break point you mean using the sleep function? yes the function will get call several times since i am trying to test connection with around 5-6 devices

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: winsock data arrival

    Why sleep? Just press F9 at the needed line of code and the program will stop executing at this very point (you can continue it from this place rather than restart it).

    You can count how many times your sub gets called.

    This code:
    vb.net Code:
    1. If Not Len(strData) = 0 Then
    2.     online = 1
    3. End If

    provides that your sub sets the online variable to 1
    if it somehow reverts to 0 then your global variable is modified somewhere else.

    A little advice - change your sub into function:

    vb.net Code:
    1. Private Function IsOnline(ByVal bytesTotal As Long) As Boolean
    2.     Dim strData As String
    3.     Winsock1.GetData strData, vbString, bytesTotal
    4.     If strData.Length > 0 Then Return True Else Return False
    5. End If

    Then you can simply call it:

    If Online(your args) Then ...

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    183

    Re: winsock data arrival

    unfortunately i cannot modify the sub function because it is associated with the winsock property, i tried changing the data_arrival function to return a value, it will give me an error

    in my main function i have this

    Do Until (allocate_rs.EOF)
    ID = allocate_rs.Fields("Cont_IP").Value
    Call check_online
    allocate_rs.MoveNext
    MsgBox online
    Loop

    i tried using your suggestion in the sub function

    If Not Len(strData) = 0 Then
    online = 1
    End If

    but everytime "online" is displayed as blank (no value)

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: winsock data arrival

    This code will always set Online = 1 if your strData is not empty.

    Code:
    If Not Len(strData) = 0 Then
        online = 1
    End If
    ALWAYS!

    Either your 'online' variable is modified elsewhere (you don't use multithreading here, do you?) or your strData is empty. There are no other options. You say you've checked and strData is not empty then we only have one remaining option: your global variable called 'online' is modified elsewhere

    vb.net Code:
    1. By the way, you could shadow your online variable by some local one with the same name.
    2.  
    3. Module GlobalModule
    4.    Public Online As Integer = 1 ' Global variable
    5.  
    6.    Public Sub Main
    7.          MsgBox(Online) ' Will return 1
    8.          Call SomeSub()
    9.          MsgBox(Online) ' Will return 1
    10.    End Sub
    11.  
    12.    Public Sub SomeSub()
    13.          Dim Online As Integer  ' You shadow the global one
    14.          MsgBox(Online)  ' Will return 0
    15.    End Sub
    16. End Module

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    183

    Re: winsock data arrival

    i think i find out the problem now...is not about my code (or maybe it is), i realized that when i do a loop to send data

    Do Until (read_controller_rs.EOF)
    send = "02000BFFFFFFFFFFFF113100FFD403"
    Winsock1.SendData send
    read_controller_rs.MoveNext
    Loop

    my data arrival will only be fired AFTER the loop goes through, so it means that if i have 4 senddata, i will not receive anything until all data has been sent then my data arrival will fire and i get all 4 replys at once.

    I am still pretty confuse about why it work this way, i want to get a respond everytime i sent data but instead i get 4 responds back after i send all 4 data....

  10. #10
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: winsock data arrival

    Not a perfect solution but...

    vb.net Code:
    1. Do Until(read_controller_rs.EOF)
    2.     For each c As Char In send
    3.        Winsock1.SendData c
    4.     Next
    5.     read_controller_rs.MoveNext
    6. Loop

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