所以即时学习在网络软件中使用套接字和线程。到目前为止,该软件(不是我创建的)能够在多个组中聊天,但我的任务是允许用户编写耳语功能。但是,我卡在编码区,如果“if”函数在“for each”函数里面工作,我肯定会工作,无论如何这里主要是我的代码
Private clientCollection As New Hashtable()
Private usernameCollection As New Hashtable()
clientCollection.Add(clientID, CData)
usernameCollection.Add(clientID, username)
哦,在我忘记之前,上面和下面的代码在服务器表单页面上
在客户端,我编写代码: writer.write(“PMG”& vbnewline& txtReceiverUsername& Message)
然后接下来是服务器上的检查部分:
ElseIf message.Substring(0, 3) = "PMG" Then
'filter the message, check who to send to
Dim newMessage As String = message.Substring(3)
Dim messagearray As String() = newMessage.Split(vbNewLine)
Dim receiver As String = messagearray(1)
'0 = "", 1 = receiver, 2 = message
在我之前写的,clientcollection包含(clientID,连接数据*)和usernamecollection包含(clientID,username)。在我的情况下,我只有用户名数据,我需要跟踪它直到clientcollection哈希表上的连接数据。
'find realid from usernamecollection, then loop clientcollection
Dim clientKey As String = 0
For Each de As DictionaryEntry In usernameCollection
'''''
'this if part Is Not working
If de.Value Is receiver Then
clientKey = de.Key
End If
'''''
Next de
'match objKey with clientcollection key
For Each dec As DictionaryEntry In clientCollection
If dec.Key = clientKey Then
Dim clients As ClientData = dec.Value
If clients.structSocket.Connected Then
clients.structWriter.Write("PMG" & messagearray(2))
End If
End If
Next dec
End If
所以,我怎么知道if部分是错误的?我只是在“next de”代码之前尝试了这些代码
For Each client As ClientData In clientCollection.Values
If client.structSocket.Connected Then
client.structWriter.Write("PMG" & "receiver:" & messagearray(1))
client.structWriter.Write("PMG" & "loop username: " & de.Value)
client.structWriter.Write("PMG" & "loop key: " & de.Key)
client.structWriter.Write("PMG" & "receiver key:" & clientKey)
End If
Next
代码允许我检查de.key和de.value。它们是正确的,但唯一不起作用的是“if”区域内的代码。
任何人都可以在“if de.key = receiver”旁边建议其他代码吗?我也试过使用if de.key.equal(接收器)而且它也不起作用