2017-04-17 1 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 
관련 문제