目前,客户端发送的消息是这样的:更好的socket通信系统
Public Function checkMD5(ByVal userID As Integer, ByVal gameID As Integer, ByVal file As String, ByVal fileFull As String) As String
Dim make As New CMakeMSG
Dim md5 As New CMD5
make.append("checkfileMD5")
make.append(userID)
make.append(containerID)
make.append(file)
make.append(md5.GenerateFileHash(fileFull))
Return SocketSendAndReceiveMSG(make.makestring)
End Function
服务器可能会收到这样的事情:
checkfileMD5-MSGDelimit0-12-MSGDelimit1-54-MSGDelimit2-filename.txt-MSGDelimit3-*md5hash*
它然后读出:
Private _message As String
Public Function handleMessage() As String
Dim brokenMessage As New ArrayList
brokenMessage = breakDown() 'Split to ArrayList
If brokenMessage(0) = "checkfileMD5" Then
Try
If brokenMessage.Count > 5 Then
Return "0-structureMessedUp"
End If
Return CompareFileMD5(brokenMessage(1), brokenMessage(2), brokenMessage(3), brokenMessage(4))
Catch ex As Exception
Return "0-structureMessedUp"
End Try
End If
End Function
所以它所做的就是将收到的信息和spli将其转换为使用-MSGDelimit-作为分隔符的数组。所以在这种情况下,CompareFileMD5()
函数将收到12,54,filename.txt,*md5hash*
。并基于它可以返回到客户端,无论MD5是否匹配。
当然,它工作,但它感觉sl and,服务器上的代码变得非常混乱。
这里的相关性较低的功能,从上面的代码(怀疑的问题,但你永远不知道):
Private Function breakDown() As ArrayList
Try
Dim theArray As New ArrayList
Dim copymsg As String = _message
Dim counter As Integer = 0
Do Until Not copymsg.Contains("-MSGDelimit")
Dim found As String
found = copymsg.Substring(0, copymsg.IndexOf("-MSGDelimit" & counter & "-"))
theArray.Add(found)
copymsg = copymsg.Replace(found & "-MSGDelimit" & counter & "-", "")
counter += 1
Loop
theArray.Add(copymsg)
Return theArray
Catch ex As Exception
Module1.msg(ex.Message)
End Try
End Function
Private Function CompareFileMD5(ByVal userID As Integer, ByVal gameID As Integer, ByVal filename As String, ByVal source As String) As String
Try
Dim tryFindFile As String = Module1.filedatabase.findfile(userID, gameID, filename)
If Not tryFindFile = "notFound" Then
Dim fileFull As String = tryFindFile & "\" & filename
Dim md5 As New CMD5
If md5.GenerateFileHash(fileFull) = source Then
Return "Match"
Else
Return "NoMatch"
End If
Else
Return "notFound"
End If
Catch ex As Exception
Module1.msg("0")
Return "0"
End Try
End Function
那么,该如何处理的更好/清洁/更专业的建议吗?
感谢您的回复,您能否详细说明''可能是发送数据的客户端版本'',因为我不明白这句话。目前JSON看起来相当不错。 – natli 2012-02-22 16:22:51
@natli:对不起,这并没有帮助我在句子中留下了一个字。我编辑并添加了澄清信息。 – 2012-02-22 16:30:19
啊,现在有道理!非常感谢,看起来我有一些工作要做;) – natli 2012-02-22 16:39:19