Ticket #3671: 01_Bug_TCP_Server.au3

File 01_Bug_TCP_Server.au3, 1.4 KB (added by TJF, 7 years ago)

The first starting Server example

Line 
1#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
2#AutoIt3Wrapper_Res_Language=1031
3#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
4#AutoIt3Wrapper_Run_Stop_OnError=y
5#AutoIt3Wrapper_Run_Tidy=y
6#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
7
8; I am the server, start me first! (Start in second the TCPConnect example script).
9
10#include <MsgBoxConstants.au3>
11
12Global $iSocket
13
14Example()
15
16Func Example()
17        TCPStartup()
18
19        OnAutoItExitRegister("OnAutoItExit")
20
21        Local $sIPAddress = "127.0.0.1"
22        Local $iPort = 65432
23        Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
24        Local $iError = 0
25
26        If @error Then
27                $iError = @error
28                MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "SERVER:", "Could not listen, Error code: " & $iError)
29                Return False
30        EndIf
31
32        Local $iSocket = 0
33        Do
34                $iSocket = TCPAccept($iListenSocket)
35
36                If @error Then
37                        $iError = @error
38                        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "SERVER:", "Could not accept the incoming connection, Error code: " & $iError)
39                        Return False
40                EndIf
41
42                $recv = TCPRecv($iSocket, 4)
43                If $recv Then
44                        MsgBox(0, "SERVER:", "Receive: " & $recv & @CRLF & "After pressing ok, will send TCPS...")
45                        $send = TCPSend($iSocket, "TCPS")
46                EndIf
47
48        Until $iSocket <> -1 ;if different from -1 a client is connected.
49
50EndFunc   ;==>Example
51
52Func OnAutoItExit()
53        TCPCloseSocket($iSocket)
54        TCPShutdown()
55EndFunc   ;==>OnAutoItExit