非常奇怪的問題。當試圖在服務器上註冊新用戶時,服務器將回發諸如「OK」和「usernameTaken」之類的消息。將此返回的字符串與另一個字符串進行比較(根據返回值執行操作)時,比較不起作用。套接字返回中斷字符串變量
Dim backupX As New CBackup
backupX.startSocket("127.0.0.1", 8888)
Dim str1 As String = backupX.registerUser("user1", "testpass")
Dim str2 As String = "usernameTaken"
If String.Equals(str1, str2) Then
MsgBox("Strings are Equal() ")
Else
MsgBox("Strings are not Equal() - " & str1 & " vs " & str2)
End If
這導致:
那麼這說明的是,即使字符串是相等,最高審計機關,他們都沒有。 MsgBox應該說是Strings are not Equal() - usernameTaken vs usernameTaken
,它完全離開了vs usernameTaken
。
這是怎麼回事?
上CBackup類額外的信息:
backupX.registerUser功能:
Public Function registerUser(ByVal name As String, ByVal password As String) As String
Dim md5 As New CMD5
If name.Contains(",") Then
Return "0-commaInName"
Else
Return SocketSendAndReceiveMSG("registerUser," & name & "," & md5.GenerateStringHash(password))
End If
End Function
SocketSendAndReceiveMSG功能:
Private Function SocketSendAndReceiveMSG(ByVal msg As String) As String
Return socket.sendAndReceiveMSG(msg)
End Function
socket.sendAn dReceiveMSG功能:
Public Function sendAndReceiveMSG(ByVal msg As String) As String
Dim serverStream As NetworkStream = clientSocket.GetStream()
sendMSG(msg & "$", serverStream)
Return receiveMSG(serverStream)
End Function
receiveMSG功能
Public Function receiveMSG(ByVal serverStream As NetworkStream) As String
Dim inStream(10024) As Byte
Dim buffSize As Integer = clientSocket.ReceiveBufferSize
serverStream.Read(inStream, 0, buffSize)
Dim returndata As String = System.Text.Encoding.ASCII.GetString(inStream)
Form1.msg("Data from Server : " & returndata)
Return returndata
End Function
看起來是一樣的插座代碼工作得很好..每個函數返回相應的函數返回值(registerUser - > SocketSendAndReceiveMSG - > sendAndReceiveMSG - > receiveMSG)。我不明白這可能會如何將str1
字符串變成這樣..
返回字節是否包含幾個可能會導致上述情況的退格或類似內容?你試過簡單地輸出服務器的返回字節來查看它包含的內容嗎? – Smudge202 2012-02-20 16:11:34
@ Smudge202我向服務器添加了'msg(「發送的字節數:」&sendBytes.Length)'。它報告:'發送的字節:13'。在我看來很好。編輯:我還添加了一個檢查客戶端; 'MsgBox(「str1 length:」&str1.Length&「 - str2 length:」&str2.Length)'。它報告:'str1:10024 - str2:13'。多麼奇怪!必須與'receiveMSG'函數中的'Dim inStream(10024)As Byte'有關......現在檢查出它 – natli 2012-02-20 16:28:13
如果它不打印「vs」,那麼你的代碼的某個舊版本正在運行?你有沒有嘗試重建你的解決方案?如果不是這樣,那麼也許你的方法根本就沒有被調用,而另一種類似的方法是... – 2012-02-20 16:58:44