-
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.
-
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. :)
-
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]
-
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?
-
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.
-
Use the following:
[code]
'Your code
[/code]
-
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.