dreiecon Level: Trainee
 Registered: 13-08-2006 Posts: 2
|
Problem with server code.
Ok. I'm trying to send an array as a string from the client to the server.
The server gets the string in god knows what VB format, but it will NOT split the string into an array.
How can I do this?
Here is the server's code
Private blnarray() As Boolean
Private Sub Command1_Click()
If Socket.Count <> 1 Then
For i = i To Socket.Count - 1
Socket(i).Close
Next i
Else
status = "Server Stopped!"
End If
End Sub
Private Sub Command2_Click()
If Socket.Count <> 1 Then
For i = 1 To Socket.Count - 1
If blnarray(i) = False Then 'Checks if the socket at index "i" is available
Socket(i).Close
Exit Sub
End If
Next i
End If
Unload All
End Sub
Private Sub Form_Load()
ReDim blnarray(0)
Socket(0).LocalPort = "22101"
Socket(0).Listen
blnarray(0) = True
End Sub
Private Sub Form_Terminate()
Close
End Sub
Private Sub Form_Unload(Cancel As Integer)
Close
End Sub
Private Sub Socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
'Check if any free sockets
If Socket.Count <> 1 Then
For i = 1 To Socket.Count - 1
If blnarray(i) = False Then 'Checks if the socket at index "i" is available
Socket(i).Accept requestID 'Accepted connection to old but available socket
Exit Sub
End If
Next i
End If
'Only runs if no open sockets
'Increase size of blnArray
ReDim Preserve blnarray(Socket.UBound + 1)
'Load new Winsock1
Load Socket(Socket.UBound + 1)
'Accept Connection to newly created socket
Socket(Socket.UBound).Accept requestID
'Updates the Array at requested position to true stating socket is in use at theis index
blnarray(Socket.UBound) = True
'Do not worry your new Winsock1 control automatically is given an open port by the Windows OS
End Sub
Private Sub Socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
status = "Receiving Data!"
ReDim dat(5)
Dim dat_a As String
Call Socket(Index).GetData(dat_a, vbString)
dat = Split(dat_a, ",")
status = "Data Received!"
If inarray("chat", dat) = True Then
Dim dat1 As String
dat1 = "chat," + dat(3) + "," + dat(2) + "," + dat(4)
If Socket.Count <> 1 Then
For i = 1 To Socket.Count - 1
If blnarray(i) = False Then
Socket(i).SendData (dat1)
Exit Sub
End If
Next i
End If
Else
Socket(Index).SendData "Error! You are not using the game client to access this server and port. Your IP has been logged and sent to our anti hacking group!"
'Dim dat1 As String
'dat1 = "chat," + dat(3) + "," + dat(2) + "," + dat(4)
'Socket(Index).SendData dat1
End If
End Sub
Private Sub Socket_SendComplete(Index As Integer)
status = "Pending New Connection. . ."
Socket(Index).Close
End Sub
Private Sub Socket_SendProgress(Index As Integer, ByVal bytesSent As Long, ByVal bytesRemaining As Long)
status = "Sending Data!"
End Sub
Public Function inarray(expression As String, a As Variant) As Boolean
Dim fval As Boolean
x = 4
If x <> 1 Then
For i = 1 To x = -1
If a(i) = expression Then
inarray = True
End If
Next i
End If
End Function
|
And here is the client code.
'Private dat1_a() As String
Private connected As Boolean
Private Sub chatsubmit_Click()
'dat = "chat," + chattype.Text + "," + to_c.Text + "," + chattxt.Text
dat = Array("chat", chattype.Text, to_c.Text, chattxt.Text)
Dim dat_a As String
dat_a = Join(dat, ",")
If connected = True Then
sock.SendData (dat_a)
ElseIf connected = False Then
If sock.State = 8 Then
sock.Close
While sock.State <> sckClosed
Wend
sock.Connect
Else
'sock.Connect
End If
End If
End Sub
Private Sub conn_Timer()
If connected = False Then
sock.Close
While sock.State <> sckClosed
Wend
sock.Connect
Else
Exit Sub
End If
End Sub
Private Sub Form_Load()
'text As String
text_v = "[System] Welcome to the example of the chat system."
chatview = chatview + vbCrLf + text_v
sock.RemoteHost = "andreavb.ipowermysql.com"
sock.RemotePort = "22101"
sock.Connect
End Sub
Private Sub sock_Close()
connected = False
End Sub
Private Sub sock_Connect()
connected = True
End Sub
Private Sub sock_DataArrival(ByVal bytesTotal As Long)
Dim dat_a1() As String
Dim dat1 As String
Call sock.GetData(dat1)
dat_a1 = Split(dat1, ",")
Dim dat1_a(0 To 4) As String
count1 = 4
'If count1 <> 1 Then
' For i = 1 To count1 - 1
' dat1_a(i) = dat_a1(i)
' Next i
'End If
MsgBox (dat1)
If dat1_a(0) = "GM" Then
gmview = "[GM] " + dat1_a(3)
ElseIf dat1_a(0) = "System" Then
chatview = "[System] " + dat1_a(3)
Else
chatview.Caption = dat1
End If
End Sub
Private Sub sock_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
m = MsgBox("Error. Disconnected from the server and cannot reconnect.", vbOKOnly, "Error with connection!")
Unload Form1
Unload Form2
Close
End Sub
Private Sub Timer1_Timer()
gmview = ""
Timer1.Enabled = False
End Sub
|
The client works fine but the server gets a type mismatch error.
Thanks.
|