winapi
Jun 11th, 2000, 04:24 AM
Hi,
I have been working on a multi user chat program and I'm having some problems with the winsock control. What basically happens is the client connects to my server program. After the connection if established it send a string AUTH,username,password to the server program. The server program then parses the string and breaks it down into a multi dim array parsedinfo(socketnum,itemnum). Lets say socket 0 gets a connection. My parser then writes that data into parsedinfo(0,1)="AUTH" parsedinfo(0,2)="username"
parsedinfo(0,3)="password" then it tests against parsedinfo(index,1) to figures out what command is sent and does the apporaite action. After the users is OKED access the client computer then requestes online/offline users. I then do a loop through all open sockets and refresh everyones on/off user list. I seem to be having some kind of refresh/timing issue with my program at this point. Sometimes the userlists won't refresh and other times I'll get the command codes in my users listbox. I see that if I put doevents in my loops that send info over the open socket it seems to help somtimes, but it still screwes up every once and a while. Here is my code. Can someone please help???
CLIENT:
'CLIENT LOGIN
Private Sub login_but_Click()
main.wsock(0).Connect server_ip, server_port
timervar = 0
Do While main.wsock(0).State <> 7
DoEvents
Label1.Caption = main.wsock(0).State
timervar = timervar + 1
If timervar = 60000 Then main.wsock(0).Close: MsgBox "Connection Failed", vbCritical, "Error!": Exit Sub
Loop
main.wsock(0).SendData "AUTH," & username.Text & "," & password.Text
End Sub
'CLIENT DATA ARRIVAL
Private Sub wsock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
wsock(Index).GetData tmpwsock(Index), vbString
Call PARSE(tmpwsock(Index), ",", Index)
'Login Passed
If Parsed(Index, 1) = "100" Then
username = Login.username.Text
password = Login.password.Text
wsock(index).SendData "UINF1"
main.Show
Unload Login
End If
' Login Failed
If Parsed(Index, 1) = "101" Then
MsgBox "Username / Password was not validated", vbCritical, "Login Failed!": wsock(index).Close: Exit Sub
End If
' Getting Online User INfo
If Parsed(Index, 1) = "102" Then
online.Clear
For looper(Index) = 2 To 100
If Parsed(Index, looper(Index)) = "" Then Exit For
online.AddItem Parsed(Index, looper(Index))
Next
wsock(Index).SendData "UINF2"
End If
' Getting Offline User INfo
If Parsed(Index, 1) = "103" Then
offline.Clear
For looper(Index) = 2 To 100
If Parsed(Index, looper(Index)) = "" Then Exit For
offline.AddItem Parsed(Index, looper(Index))
Next
End If
If Parsed(Index, 1) = "104" Then
wsock(Index).SendData "UINF1"
End If
SERVER:
THE CODES THE SERVER RECIEVES AND SENDS ARE AS FOLLOWED
-=INCOMMING=- form client to server
AUTH - Client Request authentication
UINF1 - Client Request ON-Line User Information
UINF2 - Client Request Off-Line User Information
-=OUTGOING=- from server to client
100 - AUTH passed ok for login
101 - AUTH Failed
102 - Sending Online users
103 - Sending Off line Users
104 - Requests that Client Refreshes ONOFFList
'CONNECTION REQUEST SERVER
newport = -1
For x = 0 To wsock2.Count - 1
If wsock2(x).State = 0 Then newport = x: Exit For
Next
If newport = -1 Then Load wsock2(wsock2.Count): newport = wsock2.Count - 1
wsock2(newport).Accept requestID
wsock1.Close
wsock1.Listen
Private Sub wsock2_DataArrival(Index As Integer, ByVal bytesTotal As Long)
wsock2(Index).GetData tmpwsock(Index)
Call PARSE(tmpwsock(Index), ",", Index)
'AUTH CHECK
'userinfo(x,0) = username
'userinfo(x,4) = password
'userinfo(x,5) = flag 0=offline 1=online
If Parsed(Index, 1) = "AUTH" Then
For looper(Index) = 0 To usernum
If userinfo(looper(Index), 0) = Parsed(Index, 2) Then
If userinfo(looper(Index), 4) = Parsed(Index, 3) Then
socketinfo(Index) = userinfo(looper(Index), 0)
userinfo(looper(Index), 5) = "1"
List1.AddItem userinfo(looper(Index), 0)
List2.AddItem wsock2(Index).RemoteHostIP
List3.AddItem Index
wsock2(Index).SendData "100"
Exit Sub
End If
End If
Next
wsock2(Index).SendData "101"
End If
'ONLINE USER REQUEST
If Parsed(Index, 1) = "UINF1" Then
onoffu(Index) = ""
For looper(Index) = 0 To usernum
If userinfo(looper(Index), 5) = "1" Then onoffu(Index) = onoffu(Index) & userinfo(looper(Index), 0) & ","
Next
For looper(Index) = 0 To wsock2.Count - 1
'<--- IF I INSERT DOEVENTS IT SEEMS TO HELP MY PROBLEM
If wsock2(looper(Index)).State = 7 Then wsock2(looper(Index)).SendData "102," & onoffu(Index)
Next
End If
'OFFLINE USER REQUEST
If Parsed(Index, 1) = "UINF2" Then
onoffu(Index) = ""
For looper(Index) = 0 To usernum
If userinfo(looper(Index), 5) = "0" Then onoffu(Index) = onoffu(Index) & userinfo(looper(Index), 0) & ","
Next
looper(Index) = 0
For looper(Index) = 0 To wsock2.Count - 1
'<--- IF I INSERT DOEVENTS IT SEEMS TO HELP MY PROBLEM
If wsock2(looper(Index)).State = 7 Then wsock2(looper(Index)).SendData "103," & onoffu(Index)
Next
End If
END FUNCTION
[Edited by winapi on 06-11-2000 at 05:33 PM]
I have been working on a multi user chat program and I'm having some problems with the winsock control. What basically happens is the client connects to my server program. After the connection if established it send a string AUTH,username,password to the server program. The server program then parses the string and breaks it down into a multi dim array parsedinfo(socketnum,itemnum). Lets say socket 0 gets a connection. My parser then writes that data into parsedinfo(0,1)="AUTH" parsedinfo(0,2)="username"
parsedinfo(0,3)="password" then it tests against parsedinfo(index,1) to figures out what command is sent and does the apporaite action. After the users is OKED access the client computer then requestes online/offline users. I then do a loop through all open sockets and refresh everyones on/off user list. I seem to be having some kind of refresh/timing issue with my program at this point. Sometimes the userlists won't refresh and other times I'll get the command codes in my users listbox. I see that if I put doevents in my loops that send info over the open socket it seems to help somtimes, but it still screwes up every once and a while. Here is my code. Can someone please help???
CLIENT:
'CLIENT LOGIN
Private Sub login_but_Click()
main.wsock(0).Connect server_ip, server_port
timervar = 0
Do While main.wsock(0).State <> 7
DoEvents
Label1.Caption = main.wsock(0).State
timervar = timervar + 1
If timervar = 60000 Then main.wsock(0).Close: MsgBox "Connection Failed", vbCritical, "Error!": Exit Sub
Loop
main.wsock(0).SendData "AUTH," & username.Text & "," & password.Text
End Sub
'CLIENT DATA ARRIVAL
Private Sub wsock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
wsock(Index).GetData tmpwsock(Index), vbString
Call PARSE(tmpwsock(Index), ",", Index)
'Login Passed
If Parsed(Index, 1) = "100" Then
username = Login.username.Text
password = Login.password.Text
wsock(index).SendData "UINF1"
main.Show
Unload Login
End If
' Login Failed
If Parsed(Index, 1) = "101" Then
MsgBox "Username / Password was not validated", vbCritical, "Login Failed!": wsock(index).Close: Exit Sub
End If
' Getting Online User INfo
If Parsed(Index, 1) = "102" Then
online.Clear
For looper(Index) = 2 To 100
If Parsed(Index, looper(Index)) = "" Then Exit For
online.AddItem Parsed(Index, looper(Index))
Next
wsock(Index).SendData "UINF2"
End If
' Getting Offline User INfo
If Parsed(Index, 1) = "103" Then
offline.Clear
For looper(Index) = 2 To 100
If Parsed(Index, looper(Index)) = "" Then Exit For
offline.AddItem Parsed(Index, looper(Index))
Next
End If
If Parsed(Index, 1) = "104" Then
wsock(Index).SendData "UINF1"
End If
SERVER:
THE CODES THE SERVER RECIEVES AND SENDS ARE AS FOLLOWED
-=INCOMMING=- form client to server
AUTH - Client Request authentication
UINF1 - Client Request ON-Line User Information
UINF2 - Client Request Off-Line User Information
-=OUTGOING=- from server to client
100 - AUTH passed ok for login
101 - AUTH Failed
102 - Sending Online users
103 - Sending Off line Users
104 - Requests that Client Refreshes ONOFFList
'CONNECTION REQUEST SERVER
newport = -1
For x = 0 To wsock2.Count - 1
If wsock2(x).State = 0 Then newport = x: Exit For
Next
If newport = -1 Then Load wsock2(wsock2.Count): newport = wsock2.Count - 1
wsock2(newport).Accept requestID
wsock1.Close
wsock1.Listen
Private Sub wsock2_DataArrival(Index As Integer, ByVal bytesTotal As Long)
wsock2(Index).GetData tmpwsock(Index)
Call PARSE(tmpwsock(Index), ",", Index)
'AUTH CHECK
'userinfo(x,0) = username
'userinfo(x,4) = password
'userinfo(x,5) = flag 0=offline 1=online
If Parsed(Index, 1) = "AUTH" Then
For looper(Index) = 0 To usernum
If userinfo(looper(Index), 0) = Parsed(Index, 2) Then
If userinfo(looper(Index), 4) = Parsed(Index, 3) Then
socketinfo(Index) = userinfo(looper(Index), 0)
userinfo(looper(Index), 5) = "1"
List1.AddItem userinfo(looper(Index), 0)
List2.AddItem wsock2(Index).RemoteHostIP
List3.AddItem Index
wsock2(Index).SendData "100"
Exit Sub
End If
End If
Next
wsock2(Index).SendData "101"
End If
'ONLINE USER REQUEST
If Parsed(Index, 1) = "UINF1" Then
onoffu(Index) = ""
For looper(Index) = 0 To usernum
If userinfo(looper(Index), 5) = "1" Then onoffu(Index) = onoffu(Index) & userinfo(looper(Index), 0) & ","
Next
For looper(Index) = 0 To wsock2.Count - 1
'<--- IF I INSERT DOEVENTS IT SEEMS TO HELP MY PROBLEM
If wsock2(looper(Index)).State = 7 Then wsock2(looper(Index)).SendData "102," & onoffu(Index)
Next
End If
'OFFLINE USER REQUEST
If Parsed(Index, 1) = "UINF2" Then
onoffu(Index) = ""
For looper(Index) = 0 To usernum
If userinfo(looper(Index), 5) = "0" Then onoffu(Index) = onoffu(Index) & userinfo(looper(Index), 0) & ","
Next
looper(Index) = 0
For looper(Index) = 0 To wsock2.Count - 1
'<--- IF I INSERT DOEVENTS IT SEEMS TO HELP MY PROBLEM
If wsock2(looper(Index)).State = 7 Then wsock2(looper(Index)).SendData "103," & onoffu(Index)
Next
End If
END FUNCTION
[Edited by winapi on 06-11-2000 at 05:33 PM]