Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.12 KB | None | 0 0
  1. #include <GUIConstantsEx.au3>
  2.  
  3. Opt('MustDeclareVars', 1)
  4.  
  5. ;==============================================
  6. ;==============================================
  7. ;SERVER!! Start Me First !!!!!!!!!!!!!!!
  8. ;==============================================
  9. ;==============================================
  10.  
  11. Example()
  12.  
  13. Func Example()
  14.     ; Set Some reusable info
  15.     ; Set your Public IP address (@IPAddress1) here.
  16.     ;   Local $szServerPC = @ComputerName
  17.     ;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
  18.  
  19.     While 1
  20.         Local $szIPADDRESS = "127.0.0.1"
  21.         Local $nPORT = 81
  22.         Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
  23.         Local $msg, $recv
  24.         ; Start The TCP Services
  25.         ;==============================================
  26.         TCPStartup()
  27.  
  28.         ; Create a Listening "SOCKET".
  29.         ;   Using your IP Address and Port 33891.
  30.         ;==============================================
  31.         $MainSocket = TCPListen($szIPADDRESS, $nPORT)
  32.  
  33.         ; If the Socket creation fails, exit.
  34.         If $MainSocket = -1 Then Exit
  35.  
  36.  
  37.         ; Create a GUI for messages
  38.         ;==============================================
  39.         $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
  40.         $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
  41.         GUISetState()
  42.  
  43.  
  44.         ; Initialize a variable to represent a connection
  45.         ;==============================================
  46.         $ConnectedSocket = -1
  47.  
  48.  
  49.         ;Wait for and Accept a connection
  50.         ;==============================================
  51.         Do
  52.             $ConnectedSocket = TCPAccept($MainSocket)
  53.         Until $ConnectedSocket <> -1
  54.  
  55.  
  56.         ; Get IP of client connecting
  57.         $szIP_Accepted = SocketToIP($ConnectedSocket)
  58.  
  59.         ; GUI Message Loop
  60.         ;==============================================
  61.         While 1
  62.             $msg = GUIGetMsg()
  63.  
  64.             ; GUI Closed
  65.             ;--------------------
  66.             If $msg = $GUI_EVENT_CLOSE Then ExitLoop
  67.  
  68.             ; Try to receive (up to) 2048 bytes
  69.             ;----------------------------------------------------------------
  70.             $recv = TCPRecv($ConnectedSocket, 2048)
  71.  
  72.             ; If the receive failed with @error then the socket has disconnected
  73.             ;----------------------------------------------------------------
  74.             If @error Then ExitLoop
  75.  
  76.             ; Update the edit control with what we have received
  77.             ;----------------------------------------------------------------
  78.             If $recv <> "" Then GUICtrlSetData($edit, _
  79.                     $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
  80.         WEnd
  81.  
  82.  
  83.         If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
  84.  
  85.         TCPShutdown()
  86.         GUIDelete()
  87.     WEnd
  88. EndFunc   ;==>Example
  89.  
  90. ; Function to return IP Address from a connected socket.
  91. ;----------------------------------------------------------------------
  92. Func SocketToIP($SHOCKET)
  93.     Local $sockaddr, $aRet
  94.  
  95.     $sockaddr = DllStructCreate("short;ushort;uint;char[8]")
  96.  
  97.     $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
  98.             "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
  99.     If Not @error And $aRet[0] = 0 Then
  100.         $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
  101.         If Not @error Then $aRet = $aRet[0]
  102.     Else
  103.         $aRet = 0
  104.     EndIf
  105.  
  106.     $sockaddr = 0
  107.  
  108.     Return $aRet
  109. EndFunc   ;==>SocketToIP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement