|
-
Jan 3rd, 2008, 12:55 PM
#1
Thread Starter
Addicted Member
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?
-
Jan 3rd, 2008, 01:12 PM
#2
Re: [2008] Can I add a TCPClient Component
 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:
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.
Dim data() As String = message.Split("|"c) 'Split the message on each | and place in the string array.
Select Case data(0)
Case "CONNECT"
'We use GetClientByName to make sure no one else is using this username.
'It will return Nothing if the username is free.
'Since the client sent the message in this format: CONNECT|UserName, the username will be in the array on index 1.
If GetClientByName(data(1)) Is Nothing Then
'The username is not taken, we can safely assign it to the sender.
sender.Username = data(1)
End If
End Select
End Sub
Private Function GetClientByName(ByVal name As String) As ConnectedClient
For Each cc As ConnectedClient In clients
If cc.Username = name Then
Return cc 'client found, return it.
End If
Next
'If we've reached this part of the method, there is no client by that name
Return Nothing
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|