Results 1 to 7 of 7

Thread: Winsock: make a server that can use senddata

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    3

    Winsock: make a server that can use senddata

    Hi,
    I have this part of code (server) for DataArrival:
    Code:
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim stringa, r, username, password, U, P As String
    Dim AryData() As String
    
    Winsock1.GetData stringa, vbString
    
    AryData = Split(stringa, ",")
    U = AryData(0)
    P = AryData(1)
    
    Open "C:\dati.dat" For Input As #1
    txtUsername.Text = U
    txtPassword.Text = P
    
    Do While Not EOF(1)
        Input #1, username, password
        If username = U And password = P Then
            r = "OK"
        Else
            r = "ERROR"
        End If
    Loop
    
    Close #1
    
    txtRisposta.Text = r
    Winsock1.SendData r
    
    End Sub
    And this for client:
    Code:
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim stringa, r As String
    
    Winsock1.GetData stringa, vbString
    
    txtRecvData.Text = r
    
    If r = "ERROR" Then
        MsgBox "Non sei registrato", vbOKOnly, "Errore"
    End If
    If r = "OK" Then
        MsgBox "Hai effettuato l'accesso", vbOKOnly, "OK"
    End If
    
    End Sub
    I don't know, but the client don't receive the variable "r".

    Why?

    Sorry, but I'm new of Winsock

    (If you need whole code, tell me)

    Thanks, bye

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Winsock: make a server that can use senddata

    There are some 'newbie' style problems with your code.
    1.
    Code:
    Dim stringa, r, username, password, U, P As String
    Only P will be defined as a String, all the others on the line will be defined as Variants. You'll need something like:
    Code:
    Dim stringa As String, r As String, username As String
    Dim password As String, U As string, P As String
    I assume these definitions are in the Declarations Section of the Form since U and P are used in the DataArrival event but not set to any value there.
    2.
    Code:
    Open "C:\dati.dat" For Input As #1
    You are assuming that #1 is an available file handle. It may or may not be. It would be safer to use the FreeFile Function which will always return a free handle
    Code:
    Dim intFile As Integer
    intFile = FreeFile
    Open "C:\dati.dat" For Input As #intFile
    and use #intFile instead of #1 when performing the I/O functions
    3.
    Code:
    Do While Not EOF(1)
        Input #1, username, password
        If username = U And password = P Then
            r = "OK"
        Else
            r = "ERROR"
        End If
    Loop
    Will always set r to "ERROR" unless the last record in the file matches the given username and password. You should exit the loop once you've found a match. For example
    Code:
    r = "ERROR"
    Do 
        Input #intFile, username, password
        If username = U And password = P Then
            r = "OK"
        EndIf
    Loop Until r = "OK" or EOF(#intFile)
    4. I've left this 'till last as it's more complicated that the above. When you're using Winsock, data will arrive in 'dribs and drabs' since TCP is stream orientated rather than record orientated.

    Although you may send username,password in one SendData it will not necessarily all arrive together in one DataArrival event. You may get username, in one DataArrival event and password in the next one. Your code has to take that possibility into account. A common method is to buffer the incoming data until you know that a complete record has arrived. In order to know that, it's necessary to terminate each record with an End of Record character (eg vbCr). Once you see a vbCr in the buffer, you know that at least one complete record has arrived so you can then extract it from the buffer and process it.
    Here's a simple DataArrival event which buffers the data until a vbCr is received.
    Code:
    Static strBuffer As String
    Dim strData As String
    Dim strRecord As String
    Dim aryData() As String
    Dim intPos As Integer
    Dim boComplete As Boolean
    '
    ' Read the Data and append it to the Buffer
    '
    Winsock1.GetData strData
    strBuffer = strBuffer & strData
    Do
        '
        ' Is there a vbCr in the Buffer?
        ' if there is then intPos will equal it's position
        ' in the buffer, otherwise it will be zero
        '
        intPos = InStr(strBuffer, vbCr)
        If intPos > 0 Then
            '
            ' Yes, we have at least one complete record
            ' unblock it into strRecord
            '
            strRecord = Mid$(strBuffer, 1, intPos-1)
            '
            ' Get the username and password that was sent
            '
            aryData = Split(strRecord, ",")
            '
            ' Now you can insert the code to check the
            ' username and password with aryData(0) and aryData(1)
            ' Once you've done that (and Closed #intFile)
            '
            ' Is there anything else in the Buffer?
            '
            If intPos < Len(strBuffer) Then
                '
                ' Yes, move it to the front of the buffer
                ' and go round the Do Loop again
                ' to process it
                '
                strBuffer = Mid$(strBuffer, intPos + 1)
            Else
                '
                ' No, then flush the buffer
                ' and signal to exit the loop
                '
                strBuffer = ""
                boComplete = True
            End If
        Else
            '
            ' vbCr is not in the buffer, just exit 
            ' and wait for the next DataArrival event
            '
            boComplete = True
        End If
    Loop Until boComplete = True
    If you use the above code then you should always put a vbCr at the end of each record you send.
    For example
    Code:
    Winsock1.SendData username & "," & password & vbCr
    Hope that helps.
    Last edited by Doogle; Dec 28th, 2008 at 04:49 AM.

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Winsock: make a server that can use senddata

    Here is code I wrote years ago that might help:

    http://www.vbforums.com/showthread.php?t=236973
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    3

    Re: Winsock: make a server that can use senddata

    Thanks you both!

    I have another problem: how can I use Winsock's command (like SendData) in more form?
    Example: I have 2 form, form1 and form2. form1 have Winsock1; I want send data from form2 without create another Winsock. It's possible? If yes, how?

    Thanks

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Winsock: make a server that can use senddata

    If I remember correctly (been years since I've done VB)!

    Code:
    form1.winsock
    would be the control. If you then want a text box on form 2 to display the message it would be:

    Code:
    text1.text = form1.winsock1.getdata text
    text would be the data you are receiving.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    3

    Re: Winsock: make a server that can use senddata

    It doesn't work

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Winsock: make a server that can use senddata

    Quote Originally Posted by error 404
    It doesn't work
    Using the example linked to above!

    Code:
          'Client code
      
          Dim str As String
    
          Private Sub Command2_Click()
      
          Winsock1.SendData "|MESSAGE|" & Text2.Text
      
          End Sub
    
          Private Sub Command1_Click()
    
          Winsock1.Connect Text1.Text, "1001"
     
          End Sub
    
          'Server code
    
          Dim str As String
    
          Private Sub Form_Load()
    
          Winsock1.LocalPort = 1001
          Winsock1.Listen
          End Sub
    
          Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
          Winsock1.Accept requestID
          End Sub
     
          Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
          Winsock1.GetData str
          If InStr(1, str, "|MESSAGE|") <> 0 Then
          message = Mid$(str, "10", Len(str))
          Text1.Text = message
          End If
          End Sub
    Just change the server winsock control to the form that it's on.

    example:

    Code:
    form2.Winsock1.GetData str
    DON'T FORGET: Declare the variable!

    example:
    Code:
       Dim str As String
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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