关闭 TCP 套接字(socket).
TCPCloseSocket ( 套接字 )
套接字 | 由 TCPListen 或 TCPAccept 函数返回的套接字标识(SocketID). |
成功: | 返回 1. |
失败: | 返回 0, 设置 @error |
@error: | 为 windows API WSAGetError 的返回值 (请查阅 MSDN). |
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
;==============================================
;==============================================
;服务器!! 首先启动 !!!!!!!!!!!!!!!
;==============================================
;==============================================
; 初始化代表连接的变量
;==============================================
Global $ConnectedSocket = -1
Global $MainSocket
Example()
Func Example()
OnAutoItExitRegister("Cleanup")
Local
$g_IP, $RogueSocket, $GOOEY, $edit, $input, $butt, $msg
Local
$ret, $recv
$g_IP
= "127.0.0.1"
; 启动 TCP
服务
;==============================================
TCPStartup()
; 创建监听
"SOCKET"
;==============================================
$MainSocket = TCPListen($g_IP, 65432, 100)
If
$MainSocket =
-1 Then Exit
$RogueSocket = -1
; 创建聊天 GUI
对话框
;==============================================
$GOOEY = GUICreate("我的服务端", 300, 200)
$edit
= GUICtrlCreateEdit("", 10, 40, 280, 150, $WS_DISABLED)
$input = GUICtrlCreateInput("", 10, 10, 200, 20)
$butt
= GUICtrlCreateButton("发送",
210, 10, 80, 20, $BS_DEFPUSHBUTTON)
GUISetState()
; GUI
消息循环
;==============================================
While
1
$msg = GUIGetMsg()
; GUI 关闭
;--------------------
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
; 用户点击发送
;--------------------
If $msg = $butt Then
If $ConnectedSocket
> -1 Then
$ret = TCPSend($ConnectedSocket, GUICtrlRead($input))
If @error Or $ret < 0 Then
; 发生错误时, 关闭套接字并复位
ConnectedSocket 为 -1
;----------------------------------------------------------------
TCPCloseSocket($ConnectedSocket)
WinSetTitle($GOOEY, "", "我的服务器 -
用户端切断连接")
$ConnectedSocket = -1
ElseIf $ret > 0 Then
;
用我们发送的数据更新编辑控件
;----------------------------------------------------------------
GUICtrlSetData($edit, GUICtrlRead($edit) & GUICtrlRead($input) & @CRLF)
EndIf
EndIf
GUICtrlSetData($input, "")
EndIf
If $RogueSocket > 0 Then
$recv =
TCPRecv($RogueSocket, 512)
If Not @error Then
TCPCloseSocket($RogueSocket)
$RogueSocket = -1
EndIf
EndIf
; 如果没有连接则寻找一个
;--------------------
If $ConnectedSocket
= -1 Then
$ConnectedSocket =
TCPAccept($MainSocket)
If $ConnectedSocket
< 0 Then
$ConnectedSocket = -1
Else
WinSetTitle($GOOEY, "",
"我的服务器 - 用户端连接")
EndIf
; 如果连接,则尝试读一些数据
;--------------------
Else
; 运行一个无条件的接收,避免另外的用户端尝试连接
;----------------------------------------------------------------
$RogueSocket =
TCPAccept($MainSocket)
If $RogueSocket
> 0 Then
TCPSend($RogueSocket, "~~rejected")
EndIf
$recv =
TCPRecv($ConnectedSocket, 512)
If $recv
<> ""
And $recv
<> "~~bye"
Then
; 用我们接收到的数据更新编辑控件
;----------------------------------------------------------------
GUICtrlSetData($edit,
GUICtrlRead($edit)
& ">"
& $recv
& @CRLF)
ElseIf @error
Or $recv
= "~~bye"
Then
; 发生错误时, 关闭套接字并复位 ConnectedSocket 为
-1
;----------------------------------------------------------------
WinSetTitle($GOOEY, "",
"我的服务器 - 用户端切断连接")
TCPCloseSocket($ConnectedSocket)
$ConnectedSocket = -1
EndIf
EndIf
WEnd
GUIDelete($GOOEY)
EndFunc ;==>Example
Func Cleanup()
;脚本退出时关闭打开的套接字以及 TCP 服务
;----------------------------------------------------------------------
If
$ConnectedSocket > -1
Then
TCPSend($ConnectedSocket, "~~bye")
Sleep(2000)
TCPRecv($ConnectedSocket, 512)
TCPCloseSocket($ConnectedSocket)
EndIf
TCPCloseSocket($MainSocket)
TCPShutdown()
EndFunc ;==>Cleanup
provider with jb51.net (unicode) |