本帖最后由 shqf 于 2015-3-31 21:16 编辑
关于问题一,联机一般不会自动 断掉的。否则像联机游戏、线上聊天难以实现了。不管哪一方都 可主动断掉。
关于问题二,说不清楚,选了我编的一个服务端程序的一部分,你可看看,不过这段代码的功能是当有客户端连上服务端,服务端就接收消息,后判断消息类型,再作不同的讯息回复,然后断开,再等待客户端连接。While 1
If $ConnectedSocket = -1 Then ; If no connection look for one
$ConnectedSocket = TCPAccept($MainSocket)
If $ConnectedSocket >= 0 Then
$szIP_Accepted = _SocketToIP($ConnectedSocket) ;获取客户端IP地址
_SetEditData("与客户端" & $szIP_Accepted & "建立连接")
EndIf
Else ;If connected try to read some data
$Recv = BinaryToString(TCPRecv($ConnectedSocket, 2014, 1), 4)
If @error Then
; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1
_SetEditData("接收失败")
TCPCloseSocket($ConnectedSocket)
$ConnectedSocket = -1
ElseIf $Recv <> "" Then
_SetEditData("收到:" & $Recv)
$MsgBody = StringSplit($Recv, "|")
If $MsgBody[1] = "VEF" Then ;Verify验证指令,格式为"VEF|中文用户名|口令"
$SendMsg="要回复的讯息..."
$Send = TCPSend($ConnectedSocket, StringToBinary($SendMsg , 4))
If @error Or $Send = 0 Then
_SetEditData("回复失败")
EndIf
;ElseIf $MsgBody[1] =
;如果发来的是其他信息,就作相应的其他处理
;...
EndIf
;CLOSE SOCKET AND RESET ConnectedSocket to -1
TCPCloseSocket($ConnectedSocket)
$ConnectedSocket = -1
_SetEditData("断开与客户端的连接" & @CRLF)
EndIf
EndIf
$nMsg = GUIGetMsg()
Switch $nMsg
Case
;...
Case
;...
EndSwitch
WEnd
|