TCP客户端/服务器不工作

时间:2017-04-17 17:30:39

标签: sockets tcp autoit

我使用AutoIt创建了一个TCP客户端和-server。但是,当我发送welcome时,它从未收到过。我不确定为什么。代码是:

#include <Array.au3>
Global $Socket
Global $Recibe
Global $IP
Global $sDnsAddr = "address"
Global $PORT = 1234

$IP = TCPNameToIP($sDnsAddr)

While 1 ;Reconeccion
    _Conectar()
    TCPSend($Socket, "Hola")
    While 1 ; Recibir y ejecutar
        $Recibe = ""
        While $Recibe = ""
            $Recibe = TCPRecv($Socket, 10000000)
            Sleep(100)
        WEnd
        $Dat = StringSplit($Recibe, "|||")
        Select
            Case $Dat[1] = "Welcome"
                TCPSend("Hola que tal")
        EndSelect
    WEnd
WEnd

Func _Conectar()
    While TCPStartup() = 0
        Sleep(10)
    WEnd
    While True
        $Socket = TCPConnect($IP, $PORT)
        If $Socket > 0 Then
            Ejemplo()
            ExitLoop
        EndIf
        Sleep(1000)
    WEnd
EndFunc

我想发送消息并收到回复。像这样:

enter image description here

但结果是:

enter image description here

发送第一条TCP消息会导致我的脚本崩溃。我尝试使用127.0.0.1,但这也不起作用:

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个:

首先启动服务器:端口33891

将客户端或服务器编译为exe。然后首先启动Server,然后启动客户端,例如来自Scite4Autoit。

val optionMonoid1 = new Monoid[Option[A]] {
  def combine(a1: Option[A], a2: Option[A2]) a1 orElse a2
  def identity = None
}

客户端

;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