Advertisement
Guest User

Codigo servidor

a guest
Oct 27th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.77 KB | None | 0 0
  1. #include <MsgBoxConstants.au3>
  2. #include <Process.au3>
  3.  
  4.  
  5. iniciarGUI()
  6.  
  7. Func iniciarGUI()
  8.  
  9. ;inicializamos servicio tcp
  10. TCPStartup()
  11.  
  12. ;si termina de ejecutarse el codigo que haga lo siguiente
  13. OnAutoItExitRegister("OnAutoItExit")
  14.  
  15.  
  16. Local $sIPAddress = "127.0.0.1"
  17. Local $iPort      = 65432
  18.  
  19.  
  20. #include <ButtonConstants.au3>
  21. #include <EditConstants.au3>
  22. #include <GUIConstantsEx.au3>
  23. #include <StaticConstants.au3>
  24. #include <WindowsConstants.au3>
  25. #Region ### START Koda GUI section ### Form=
  26. $Form1 = GUICreate("Networking - Servidor", 613, 429, 192, 132)
  27. Global $txtMensajes = GUICtrlCreateEdit("", 40, 160, 513, 217)
  28. GUICtrlSetData(-1, "txtMensajes")
  29. $btnServidor = GUICtrlCreateButton("Iniciar Servidor", 40, 40, 225, 49)
  30. $Label1 = GUICtrlCreateLabel("Mensajes del cliente: ", 40, 120, 311, 40)
  31. GUICtrlSetFont(-1, 18, 800, 0, "MS Sans Serif")
  32. GUISetState(@SW_SHOW)
  33. #EndRegion ### END Koda GUI section ###
  34.  
  35. While 1
  36.     $nMsg = GUIGetMsg()
  37.     Switch $nMsg
  38.         Case $GUI_EVENT_CLOSE
  39.             Exit
  40.         Case $btnServidor
  41.             GUICtrlSetState($btnServidor, $GUI_DISABLE)
  42.             MyTCP_Server($sIPAddress, $iPort)
  43.  
  44.     EndSwitch
  45. WEnd
  46.  
  47. EndFunc ; =>Termina iniciarGUI()
  48.  
  49.  
  50. Func MyTCP_Server($sIPAddress, $iPort)
  51.  
  52.     ;Nos ponemos a la escucha en la ip y puerto seleccionado con un maximo establecido de 100
  53.     ;conecciones pendientes
  54.     Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
  55.     Local $iError = 0
  56.  
  57.     If @error Then
  58.         ;Alguien ya esta escuchando esa direccion ip y puerto o el script ya esta corriendo
  59.         $iError = @error
  60.         MsgBox(0, "Error", "Servidor no pudo escuchar, codigo de error: " & $iError)
  61.         Return False
  62.     EndIf
  63.  
  64.     ;Asignar una variable para ser usada por el socket del cliente
  65.     Local $iSocket = 0
  66.  
  67.     Do
  68.         ;Aceptamos conexiones entrantes, el socket cierra cuando termina, un socket por cliente
  69.         $iSocket = TCPAccept($iListenSocket)
  70.  
  71.         if @error Then
  72.             msgbox(0, "Error", "No se puede aceptar conecciones entrantes " & $iError)
  73.             return False
  74.         EndIf
  75.  
  76.     If GUIGetMsg() = $GUI_EVENT_CLOSE then return False
  77.  
  78.     Until $iSocket <> -1 ;si es diferente de -1 un cliente se pudo conectar
  79.  
  80.     TCPCloseSocket($iListenSocket)
  81.  
  82.     Local $sReceived = TCPRecv($iSocket, 2048)
  83.  
  84.     Switch $sReceived
  85.  
  86.         case "calculadora"
  87.             _RunDos("start calc.exe")
  88.             GUICtrlSetData($txtMensajes, "Abriendo Calculadora")
  89.         case "blockNotas"
  90.             _RunDos("start notepad.exe")
  91.             GUICtrlSetData($txtMensajes, "Abriendo block de notas")
  92.         case "paint"
  93.             _RunDos("start mspaint.exe")
  94.             GUICtrlSetData($txtMensajes, "Abriendo paint")
  95.         case else
  96.  
  97.             GUICtrlSetData($txtMensajes, $sReceived)
  98.     EndSwitch
  99.  
  100.     TCPCloseSocket($iSocket)
  101.  
  102.  
  103.     MyTCP_Server($sIPAddress, $iPort)
  104.  
  105.  
  106.     EndFunc ; => termina MyTCP_Server
  107.  
  108.  
  109.  
  110. ;Cerramos el servicio TCP
  111. Func OnAutoItExit()
  112.     TCPShutdown()
  113. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement