Results 1 to 7 of 7

Thread: Needing to Gather Variables through Winsock

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    316

    I have a client and a server application that I am using to first of all do an authentication method.

    I understand the basics of Calling the GetData and SendData through Winsock. I even wrote a chat program doing that. But, what if I want to authenticate a user in a program via winsock. How would I send these variables across using the SendData Method, and how would they be received by the GetData Method on the server.
    Using VS 6 Enterprise w/ SP5 & Windows 2000 Professional

  2. #2
    Guest
    Your client program could send something like:

    USER mralston
    PASS vbk1ng

    The server program would receive this and first check the first few (4 in this case) characters of each line to findout what info is being passed... this could be the name of the variable perhaps? Once it knows what it's receiving it takes the rest of the line as the info being passed to feed into a variable.

    Have you ever watched the commands being sent by a program like CuteFTP? The same as I've just suggested. Other protocols like POP3 are also like this.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    316
    Maybe I need to be more specific here. Here is what I am wanting to do from the client.

    Code:
    Private Sub cmdOK_Click()
    Dim strUserName As String, strPassword As String
    
    strUserName = xxxxxx
    strPassword = xxxx
    
    Call tcpClient.SendData("USER " & strUserName)
    Call tcpClient.SendData("PASSWORD " & strPassword)
    
    End Sub
    The Server has the following for the DataArrival Event:

    Code:
    Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
        Dim strMessage As String, strValidateUser As Integer, strValidatePass As Integer, strUserName As String, strPassword As String
    
        Call tcpServer.GetData(strMessage) 
        
        If Left$(strMessage, 4) = "USER" Then
        strUserName = strMessage
            
            If strUserName = "USER xxxxxx" Then
            strValidateUser = 1
            End If
    
        End If
    
        If Left$(strMessage, 8) = "PASSWORD" Then
        strPassword = strMessage
    
            If strPassword = "PASSWORD xxxx" Then
            strValidatePass = 1
            End If
    
        End If
           
        If strValidateUser = 1 And strValidatePass = 1 Then
           Call tcpServer.SendData("CONNECT_ACCEPT")
       
        Else
           Call tcpServer.SendData("CLIENT_REJECT")
    
        End If
    
    End Sub
    Would I want to use Case statements for this? And, better yet, is there a more simpler, cleaner way of writing this code on the client and server end?

    Thanks in advance for the help

    [Edited by bedowin on 08-30-2000 at 11:17 AM]
    Using VS 6 Enterprise w/ SP5 & Windows 2000 Professional

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    316
    Okay, here's the problem now.

    Everything works fine with the code above accept if I use the following line of code on the client side:

    Code:
    Call tcpClient.SendData("USER " & "xxxxxx")
    Call tcpClient.SendData("PASS " & "xxxx")
    It will just sit there, not getting any response back from the server. If I take out the Password Line and don't require it on the server side, everything works fine again.

    Are the two SendData methods getting squeezed together, so that the server can't distinguish that the two strings are separate when they are being sent over?
    Using VS 6 Enterprise w/ SP5 & Windows 2000 Professional

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    How many bytes are present in the DataArrival parameter? I.E. tcpClient_DataArrival(ByVal bytesTotal As Long)

    It's not uncommon for the buffer to contain more than 1 record. If the two strings are being concatenated then
    bytesTotal would equal 20 ("USER " & "xxxxxx = 11, "PASS " & "xxxx" = 9). If so, then use Left$, Mid$, etc. to get what you need.

    A better approach would be to send the fields in a record.

    Add the following public declarations to your project:
    Public Type dclLogRec
    UserId As String * 8
    PassWd As String * 8
    End Type
    Public Declare Sub CopyMemory Lib "KERNEL32" _
    Alias "RtlMoveMemory" (hpvDest As Any, _
    hpvSource As Any, _
    ByVal cbCopy As Long)

    Change your cmdOK_Click() routine to something like:

    Private Sub cmdOK_Click()
    Dim Login As dclLogRec
    Dim bTBuff() As Byte
    Dim vTData As Variant

    Login.UserId = txtUser ' my test client had
    Login.PassWd = txtPswd ' a TextBox for each
    ReDim bTBuff(Len(Login))
    CopyMemory bTBuff(0), Login, Len(Login)
    vTData = bTBuff
    tcpClient.SendData vTData

    End Sub

    And tcpServer_DataArrival to something like:

    Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
    Dim Login As dclLogRec
    Dim bTBuff() As Byte

    ReDim bTBuff(bytesTotal)
    tcpServer.GetData bTBuff, vbArray + vbByte, bytesTotal
    CopyMemory Login, bTBuff(0), Len(Login)
    txtUser = Login.UserId ' my test server had
    txtPswd = Login.PassWd ' a TextBox for each
    End Sub

    BTW, I am new to this list. Can someone tell me how to set the fonts in my reply? The code would be easier to read if it was in a fixed pitch font like Courier.

  6. #6
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Use the following:

    [code]
    'Your code
    [/code]

  7. #7
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Uh, I forgot to answer

    well, what I do is to set a token so I can split the incoming messages:

    The function adds the token and sends the strings:
    Code:
    Sub Send(iCommand as String, optional iParameter as String ="")
       If WS.State = sckConnected Then
          WS.SendData iCommand & IIf(iParameter = "", "", ";") & iParameter & "¶"
       Else
          MsgBox "Not yet connected!", vbCritical, "Error sending data"
       EndIf
    End Sub
    
    
    Sub Command1_Click()
       Send "User", UserName
       Send "Password", Password
    End Sub
    Then when receiving data I split it up:
    Code:
    Sub Execute( iCommand as String )
       Dim A as Long
       Dim Arg() as String
    
       Arg = Split( iCommand, ";" )
    
       Select Case Arg( 0 )
       Case "User"
          MsgBox "User [" & Arg( 1 ) & "] logged in."
       Case "Password"
          MsgBox "Password ist [" & Arg( 1 ) & "]."
       Case Else
          MsgBox "Unknown command [" & Arg( 0 ) & "] received.", vbCritical, "Error executing"
       End Select
    End Sub
    
    Sub WS_DataArrival(...)
       Dim A as Long
       Dim Temp as String
       Dim Arg() as String
    
       'Get data
       WS.GetData Temp, vbString
    
       'Split and execute
       Arg = Split( Temp, "¶" )
       For A = 0 to UBound( Arg, 1 )
          Execute Arg( A )
       Next
    End Sub
    Hope that's not too hard... it's a good way to handle this winsock error as I think.

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