Results 1 to 40 of 163

Thread: [RESOLVED] [2008] Can I add a TCPClient Component

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [2008] Can I add a TCPClient Component

    ok so Im tring to build the third option
    so I have this code
    Code:
        Private Sub MessageReceived(ByVal sender As ConnectedClient, ByVal Message As String)        'A message has been received from one of the clients.        'To determine who its from, use the sender object.        'sender.SendMessage can be used to reply to the sender.        
            If Mid(Message, 1, 1) = "/" Then
                Select Case Mid(Message, 2, 8)
                    Case "Connect:"
                        sender.SendMessage("/Connected")
                        SetConnectionLabelText("Connected")
                    Case Else
                        WriteToMainText(Message)
                End Select
            End If
        End Sub
    considering the cilent send "/Connect:USERNAME" when it connects
    how to add the username to the list?

    I was checking ur previous topic and u had a different diclaration
    Dim Clients As New Dictionary(Of String, Net.Sockets.TcpClient)

    whats the difference?
    and u added the name in DoListen Sub not MessageReceived Sub?
    what should I do?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Can I add a TCPClient Component

    Quote Originally Posted by perito
    ok so Im tring to build the third option
    so I have this code
    Code:
        Private Sub MessageReceived(ByVal sender As ConnectedClient, ByVal Message As String)        'A message has been received from one of the clients.        'To determine who its from, use the sender object.        'sender.SendMessage can be used to reply to the sender.        
            If Mid(Message, 1, 1) = "/" Then
                Select Case Mid(Message, 2, 8)
                    Case "Connect:"
                        sender.SendMessage("/Connected")
                        SetConnectionLabelText("Connected")
                    Case Else
                        WriteToMainText(Message)
                End Select
            End If
        End Sub
    considering the cilent send "/Connect:USERNAME" when it connects
    how to add the username to the list?

    I was checking ur previous topic and u had a different diclaration
    Dim Clients As New Dictionary(Of String, Net.Sockets.TcpClient)

    whats the difference?
    and u added the name in DoListen Sub not MessageReceived Sub?
    what should I do?
    Alright, start by creating a string variable for holding the username in the ConnectedClient class, and create a string property for it. (If you dont know how, I'll shortly update my example). This variable will (obviously) hold the clients username.

    Heres a method of handling messages used in MSDN 101 examples:
    Send the messages with the | character as separator. If the user wants to connect with a username he'd send this: "CONNECT|MyName"

    Your messageReceived method would then look like this:
    VB.Net Code:
    1. Private Sub messageReceived(ByVal sender As ConnectedClient, ByVal message As String)
    2.         'A message has been received from one of the clients.
    3.         'To determine who its from, use the sender object.
    4.         'sender.SendMessage can be used to reply to the sender.
    5.  
    6.         Dim data() As String = message.Split("|"c) 'Split the message on each | and place in the string array.
    7.         Select Case data(0)
    8.             Case "CONNECT"
    9.                 'We use GetClientByName to make sure no one else is using this username.
    10.                 'It will return Nothing if the username is free.
    11.                 'Since the client sent the message in this format: CONNECT|UserName, the username will be in the array on index 1.
    12.                 If GetClientByName(data(1)) Is Nothing Then
    13.                     'The username is not taken, we can safely assign it to the sender.
    14.                     sender.Username = data(1)
    15.                 End If
    16.         End Select
    17.  
    18.     End Sub
    19.  
    20.     Private Function GetClientByName(ByVal name As String) As ConnectedClient
    21.         For Each cc As ConnectedClient In clients
    22.             If cc.Username = name Then
    23.                 Return cc 'client found, return it.
    24.             End If
    25.         Next
    26.         'If we've reached this part of the method, there is no client by that name
    27.         Return Nothing
    28.     End Function
    Note the GetClientByName function, it will be useful later aswell.

    About my use of the Dictionary in the other example, it provides the functionallity to store items in a collection and let each item have a (in this case) string key to identify them. We could've used that in this code aswell, but I chose the List instead.

    Also, ignore all my other code in that example, the examples in there are fairly simple and nothing you should be using.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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