これを試してみてください:
スタートサーバ最初:
ポート33891をクライアントまたはexeファイルにサーバーをコンパイルします。次に、サーバーを最初に起動し、次にクライアントを起動します。 Scite4Autoitから。
;SERVER!! Start Me First !!!!!!!!!!!!!!!
#include <GUIConstants.au3>
; Initialize a variable to represent a connection
;==============================================
Global $MainSocket, $ConnectedSocket = -1
Global $g_IP = @IPAddress1
Global $g_port = 33891
; Start The TCP Services
;==============================================
TCPStartup()
; Create a Listening "SOCKET"
;==============================================
$MainSocket = TCPListen($g_IP, $g_port, 100)
If $MainSocket = -1 Then Exit
$RogueSocket = -1
; Create a GUI for chatting
;==============================================
$GOOEY = GUICreate("my server - I am " & @IPAddress1, 350, 200, @DesktopWidth/2 + 100, @DesktopHeight/2 - 350)
$edit = GUICtrlCreateEdit("", 10, 40, 330, 150, $WS_DISABLED)
$input = GUICtrlCreateInput("", 10, 10, 250, 20)
$butt = GUICtrlCreateButton("Send", 260, 10, 80, 20, $BS_DEFPUSHBUTTON)
GUISetState()
; 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 Then
; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1
;----------------------------------------------------------------
TCPCloseSocket($ConnectedSocket)
WinSetTitle($GOOEY, "", "my server - I am " & @IPAddress1)
$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
WinSetTitle($GOOEY, "", "my server - Hello " & SOCKET2IP($ConnectedSocket))
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 - I am " & @IPAddress1)
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 ;==>OnAutoItExit
Func SOCKET2IP($SHOCKET)
Local $sockaddr = DllStructCreate("short;ushort;uint;char[8]")
$a = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, "ptr", DllStructGetPtr($sockaddr), _
"int_ptr", DllStructGetSize($sockaddr))
If Not @error And $a[0] = 0 Then
$a = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
If Not @error Then $a = $a[0]
Else
$a = 0
EndIf
; release Struct not really needed as it is a local
$sockaddr = 0
Return $a
EndFunc ;==>SOCKET2IP
クライアント
#include <GuiConstants.au3>
#include <GuiEdit.au3>
#include <Misc.au3>
Local $Server = InputBox("Server", "Please input the server you would like to connect to:")
Local $Port = InputBox("Server Port", "Please input the port or the server you would like to connect to:")
TCPStartup()
$MainSocket = TCPConnect(TCPNameToIP($Server), $Port)
If $MainSocket = -1 Then Exit MsgBox(0, "Error", "Could Not Connect or Bad Connection")
GUICreate("Server Client", 390, 210)
$Send = GUICtrlCreateEdit("", 10, 10, 175, 150, $WS_VSCROLL)
Local $History = GUICtrlCreateEdit("Server Messages:", 200, 10, 175, 150, BitOR($WS_VSCROLL, $ES_READONLY))
$SButton = GUICtrlCreateButton("Send", 145, 165, 100, 35)
GUISetState()
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then
Exit
ElseIf $msg = $SButton Or _IsPressed ("0D") = 1 Then
$Text = GUICtrlRead($Send)
$TCPSent = TCPSend($MainSocket, $Text)
GUICtrlSetData($Send, "")
EndIf
$Recv = TCPRecv($MainSocket, 512)
If $Recv <> "" Then
GUICtrlSetData($History, GUICtrlRead($History) & @CRLF & $Recv)
_GUICtrlEdit_LineScroll ($History, 0, _GUICtrlEdit_GetLineCount ($History))
EndIf
WEnd
Func OnAutoItExit()
TCPCloseSocket($MainSocket)
TCPShutdown()
EndFunc;==>OnAutoItExit