server code:
VB Code:
Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim strData As String Dim strData2() As String Dim Users() As String Dim word1 As String Dim word2 As String On Error Resume Next Socket(Index).GetData strData strData2 = Split(strData, "/") word1 = strData2(0) word2 = strData2(1) If word1 = "name" Then Users(Index) = word2 txtMain.Text = txtMain.Text & vbCrLf & "User " & Users(Index) & " Connected with ip " & Socket(Index).RemoteHostIP txtMain.SelStart = Len(txtMain.Text) Else strData = Users(Index) & ": " & strData txtMain.Text = txtMain.Text & vbCrLf & strData txtMain.SelStart = Len(txtMain.Text) Dim i As Integer For i = 1 To Socket.Count If Socket(i).State = sckConnected Then Socket(i).SendData strData DoEvents End If Next i End If
Client code:
VB Code:
Private Sub Winsock_Connect() ' we are connected! lbl1.Caption = "Connected" txtMain.Text = txtMain.Text & vbCrLf & "Connected to " & Winsock.RemoteHostIP & " on port 1007" Winsock.SendData "name/" & USRName DoEvents End Sub
this code is supposed to set a username for each socket, for a chat
when the client connects, it send name/(the username) and when the server recieves data starting with name/ it sets the username in an array with the same index as the socket. when i run the program, and connect to myself, it doesn't set a name. whenever i send a message it is just the message with no username. someone help please, thanks.
edit: ignore the weird indentations in the code, it was a copy/paste problem
edit2: upon taking out the on error resume next line of code, and moving the dim Users() as string to the top of the code and not in this event, i recieved the error: "Subscript out of range"




Reply With Quote