2017-04-17 27 views
2

ソケットプログラムであるAutoItを使用してTCPクライアント/サーバを作成しようとしています。しかし、私は例えばwelcomeのようなメッセージを送ってもそれを受け取ることはありません。なぜ私は分からない。コードは次のとおりです。TCPクライアント/サーバが動作しない

#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 

私は私のプログラムはHola que talに答えることはありませんWelcomeを送信します。どうして?私はこのような何かをWelcomeメッセージを送信し、受信したい:

enter image description here

そしてその結果、私は何かがある送信するとき:

enter image description here

私は唯一のTCPメッセージと私の接続停止を送信します私はドメインを使用しているためクラッシュします。私は127.0.0.1のようなローカルアドレスを使用してみましたが、メッセージを送信するときに決して答えを得ることはありません。

enter image description here

答えて

0

これを試してみてください:

スタートサーバ最初:

ポート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 
関連する問題