套接字返回打破字符串变量

时间:2012-02-20 16:02:35

标签: vb.net

非常奇怪的问题。当尝试在服务器上注册新用户时,服务器将回发诸如“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

这导致:

vbSocketProblem

所以这表明即使字符串 相等,也就是说它们不是。 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.sendAndReceiveMSG 功能:

Public Function sendAndReceiveMSG(ByVal msg As String) As String
    Dim serverStream As NetworkStream = clientSocket.GetStream()
    sendMSG(msg & "$", serverStream)
    Return receiveMSG(serverStream)
End Function

接收MSG功能

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字符串变量..

1 个答案:

答案 0 :(得分:0)

在行If String.Equals(str1, str2) Then上设置断点并检查两个字符串。它们的长度是一样的吗?程序是在您的断点处停止还是另一个代码部分正在运行?