Results 1 to 3 of 3

Thread: Username problems[Resolved]

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    4

    Username problems[Resolved]

    server code:
    VB Code:
    1. Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    2.  
    3. Dim strData As String
    4. Dim strData2() As String
    5. Dim Users() As String
    6. Dim word1 As String
    7. Dim word2 As String
    8. On Error Resume Next
    9.  
    10.    
    11.  
    12.     Socket(Index).GetData strData
    13.    
    14.     strData2 = Split(strData, "/")
    15.    
    16.     word1 = strData2(0)
    17.     word2 = strData2(1)
    18.    
    19.     If word1 = "name" Then
    20.    
    21.     Users(Index) = word2
    22.    
    23.     txtMain.Text = txtMain.Text & vbCrLf & "User " & Users(Index) & " Connected with ip " & Socket(Index).RemoteHostIP
    24.  
    25.     txtMain.SelStart = Len(txtMain.Text)
    26.  
    27.    
    28.     Else
    29.    
    30.     strData = Users(Index) & ": " & strData
    31.  
    32.     txtMain.Text = txtMain.Text & vbCrLf & strData
    33.  
    34.     txtMain.SelStart = Len(txtMain.Text)
    35.    
    36.     Dim i As Integer
    37.    
    38.         For i = 1 To Socket.Count
    39.          
    40.              If Socket(i).State = sckConnected Then
    41.  
    42.               Socket(i).SendData strData
    43.  
    44.               DoEvents
    45.    
    46.              End If
    47.          
    48.         Next i
    49.     End If

    Client code:
    VB Code:
    1. Private Sub Winsock_Connect()
    2.  
    3.     ' we are connected!
    4.  
    5.     lbl1.Caption = "Connected"
    6.     txtMain.Text = txtMain.Text & vbCrLf & "Connected to " & Winsock.RemoteHostIP & " on port 1007"
    7.    
    8.     Winsock.SendData "name/" & USRName
    9.    
    10.     DoEvents
    11.    
    12. 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"
    Last edited by camman977; Apr 30th, 2005 at 12:49 PM.

  2. #2
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: Username problems[Edited]

    Quote Originally Posted by camman977
    server code:
    VB Code:
    1. Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    2.  
    3. Dim strData As String
    4. Dim strData2() As String
    5. Dim Users() As String
    6. Dim word1 As String
    7. Dim word2 As String
    8. On Error Resume Next
    9.  
    10.    
    11.  
    12.     Socket(Index).GetData strData
    13.    
    14.     strData2 = Split(strData, "/")
    15.    
    16.     word1 = strData2(0)
    17.     word2 = strData2(1)
    18.    
    19.     If word1 = "name" Then
    20.    
    21.     Users(Index) = word2
    22.    
    23.     txtMain.Text = txtMain.Text & vbCrLf & "User " & Users(Index) & " Connected with ip " & Socket(Index).RemoteHostIP
    24.  
    25.     txtMain.SelStart = Len(txtMain.Text)
    26.  
    27.    
    28.     Else
    29.    
    30.     strData = Users(Index) & ": " & strData
    31.  
    32.     txtMain.Text = txtMain.Text & vbCrLf & strData
    33.  
    34.     txtMain.SelStart = Len(txtMain.Text)
    35.    
    36.     Dim i As Integer
    37.    
    38.         For i = 1 To Socket.Count
    39.          
    40.              If Socket(i).State = sckConnected Then
    41.  
    42.               Socket(i).SendData strData
    43.  
    44.               DoEvents
    45.    
    46.              End If
    47.          
    48.         Next i
    49.     End If

    Client code:
    VB Code:
    1. Private Sub Winsock_Connect()
    2.  
    3.     ' we are connected!
    4.  
    5.     lbl1.Caption = "Connected"
    6.     txtMain.Text = txtMain.Text & vbCrLf & "Connected to " & Winsock.RemoteHostIP & " on port 1007"
    7.    
    8.     Winsock.SendData "name/" & USRName
    9.    
    10.     DoEvents
    11.    
    12. 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"

    1. First of all remove On Error Resume Next
    2. You have defined Users as a dynamic Array, and i can't see any Redim Statement which creates that array...


    My Suggestion
    If you want to associate a username with a specific Socket.....i would recommend that you use the Tag property of Winsock control...

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    4

    Re: Username problems[Edited]

    Ok, i will use tag, thanks

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