tcp网络函数(已解决)
本帖最后由 liufenglg 于 2010-9-1 17:09 编辑刚学au3
今天看到TCPCloseSocket这一函数怎么也弄不懂
帮助里的这一例子
点send发送信息没反应
不知道问题在哪,请求帮助
;SERVER!! Start Me First !!!!!!!!!!!!!!!
#include <GUIConstants.au3>
$g_IP = "127.0.0.1"
; Start The TCP Services
;==============================================
TCPStartUp()
; Create a Listening "SOCKET"
;==============================================
$MainSocket = TCPListen($g_IP, 65432,100 )
If $MainSocket = -1 Then Exit
$RogueSocket = -1
; Create a GUI for chatting
;==============================================
$GOOEY = GUICreate("my server",300,200)
$edit = GUICtrlCreateEdit("",10,40,280,150,$WS_DISABLED)
$input = GUICtrlCreateInput("",10,10,200,20)
$butt = GUICtrlCreateButton("Send",210,10,80,20,$BS_DEFPUSHBUTTON)
GUISetState()
; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1
; GUI Message Loop
;==============================================
While 1
$msg = GUIGetMsg()
; GUI Closed
;--------------------
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
; User Pressed SEND
;--------------------
If $msg = $butt Then
If $ConnectedSocket > -1 Then
$ret = TCPSend( $ConnectedSocket, GUICtrlRead($input))
If @ERROR Or $ret < 0 Then
; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1
;----------------------------------------------------------------
TCPCloseSocket( $ConnectedSocket )
WinSetTitle($GOOEY,"","my server - Client Disconnected")
$ConnectedSocket = -1
ElseIf $ret > 0 Then
; UPDATE EDIT CONTROL WITH DATA WE SENT
;----------------------------------------------------------------
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 no connection look for one
;--------------------
If $ConnectedSocket = -1 Then
$ConnectedSocket = TCPAccept( $MainSocket)
If $ConnectedSocket < 0 Then
$ConnectedSocket = -1
Else
WinSetTitle($GOOEY,"","my server - Client Connected")
EndIf
; If connected try to read some data
;--------------------
Else
; EXECUTE AN UNCONDITIONAL ACCEPT IN CASE ANOTHER CLIENT TRIES TO CONNECT
;----------------------------------------------------------------
$RogueSocket = TCPAccept( $MainSocket)
If $RogueSocket > 0 Then
TCPSend( $RogueSocket, "~~rejected" )
EndIf
$recv = TCPRecv( $ConnectedSocket, 512 )
If $recv <> "" And $recv <> "~~bye" Then
; UPDATE EDIT CONTROL WITH DATA WE RECEIVED
;----------------------------------------------------------------
GUICtrlSetData($edit, GUICtrlRead($edit) & ">" & $recv & @CRLF)
ElseIf @error Or $recv = "~~bye" Then
; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1
;----------------------------------------------------------------
WinSetTitle($GOOEY,"","my server - Client Disconnected")
TCPCloseSocket( $ConnectedSocket )
$ConnectedSocket = -1
EndIf
EndIf
WEnd
GUIDelete($GOOEY)
Func OnAutoItExit()
;ON SCRIPT EXIT close opened sockets and shutdown TCP service
;----------------------------------------------------------------------
If $ConnectedSocket > -1 Then
TCPSend( $ConnectedSocket, "~~bye" )
Sleep(2000)
TCPRecv( $ConnectedSocket,512 )
TCPCloseSocket( $ConnectedSocket )
EndIf
TCPCloseSocket( $MainSocket )
TCPShutDown()
EndFunc 帮顶,,,也想学习
页:
[1]