Jump to content

tcp server try to understund


Recommended Posts

hi guy i try to understund   a  server  for  exchange  data  by tcp  ... so .

i  created a  script  like  this 

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

; Start First clicking on "1. Server"
; Then start a second instance of the script selecting "2. Client"

Global $sIPAddress, $iPort

Opt("GuiOnEventMode", 1)
Opt("GUICloseOnESC", 0)
Opt("GUIResizeMode", 128)


Example()

Func Example()
    TCPStartup() ; Start the TCP service.

    ; Register OnAutoItExit to be called when the script is closed.
    OnAutoItExitRegister("OnAutoItExit")

    ; Assign Local variables the loopback IP Address and the Port.
     $sIPAddress = "0.0.0.0" ; This IP Address only works for testing on your own computer.
     $iPort = 3356 ; Port used for the connection.

    Local $sTitle = "TCP Start"
    Local $hGUI = GUICreate($sTitle, 250, 70)
    Local $idBtnServer = GUICtrlCreateButton("1. Server", 65, 10, 130, 22)

    GUISetState(@SW_SHOW, $hGUI)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_close")
    GUICtrlSetOnEvent($idBtnServer, "_call_server")


EndFunc   ;==>Example

func _call_server()
   MyTCP_Server($sIPAddress, $iPort)


EndFunc

Func MyTCP_Server($sIPAddress, $iPort)
    ; Assign a Local variable the socket and bind to the IP Address and Port specified with a maximum of 100 pending connexions.
    Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
    Local $iError = 0
MsgBox (0,'socket',$iListenSocket)
    If @error Then
        ; Someone is probably already listening on this IP Address and Port (script already running?).
        $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not listen, Error code: " & $iError)
        Return False
    EndIf

    ; Assign a Local variable to be used by the Client socket.
    Local $iSocket = 0

    Do ; Wait for someone to connect (Unlimited).
        ; Accept incomming connexions if present (Socket to close when finished; one socket per client).
        $iSocket = TCPAccept($iListenSocket)

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not accept the incoming connection, Error code: " & $iError)
            Return False
        EndIf

        If GUIGetMsg() = $GUI_EVENT_CLOSE Then Return False
    Until $iSocket <> -1 ;if different from -1 a client is connected.

    ; Close the Listening socket to allow afterward binds.
    ;TCPCloseSocket($iListenSocket)
Do
    ; Assign a Local variable the data received.
    Local $sReceived = TCPRecv($iSocket, 10000) ;we're waiting for the string "tata" OR "toto" (example script TCPRecv): 4 bytes length.
    ;  TCPSend($iSocket, "eccolo ci sono ")
    ; Notes: If you don't know how much length will be the data,
    ; use e.g: 2048 for maxlen parameter and call the function until the it returns nothing/error.

    ; Display the string received.
    If $sReceived = '' Then

       MsgBox (0,'','non è arrivato nulla aspetto ancora')
    Else

    MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "Received: " & $sReceived)

    ; Close the socket.
    ;TCPCloseSocket($iSocket)
 EndIf
Until $sReceived = 'quit'

EndFunc   ;==>MyTCP_Server


Func _close()


    Exit

EndFunc   ;==>_close

;===============================================================

;Keep the GUI alive

;===============================================================

While 1
    Sleep(100)
WEnd


Func _stop() ; chiudo il file  che grabba
    ProcessClose($iPID)
EndFunc   ;==>_stop

and  when i connect a client and  try to send  somthing    go  all ok .

But  if  i  close  client  and reopen  and  try   to send message again  not  work , i must close server and reopen

the part in client   is 

Func _uscita_server()

TCPSend ( $iSocket,'Esco, ciao')
sleep(2000)
TCPSend ( $iSocket,'quit')

EndFunc


Func _test_socket()

TCPSend ( $iSocket,'Ciao')


EndFunc
Func _Scan_IP()
     $iSocket
    Local $IP_Start = GUICtrlRead($Input3)
    Local $IP_End = GUICtrlRead($Input4)
    Local $z = 0
    Local $Range = $IP_Adrss[1] & "." & $IP_Adrss[2] & "." & $IP_Adrss[3] & "."
    Local $Address[$IP_End]


    ; Register OnAutoItExit to be called when the script is closed.
    OnAutoItExitRegister("OnAutoItExit")



    For $i = $IP_Start To ($IP_End)
        TCPStartup() ; Start the TCP service.
        $Address[$z] = $Range & $i
        $iSocket = TCPConnect($Address[$z], $IP_Port)
        Local $iError = 0

        ; If an error occurred display the error code and return False.
        If @error Then
            ; The server is probably offline/port is not opened on the server.
            ;$iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not connect, Error code: " & $iError)
            ;Return False
            TCPCloseSocket($iSocket)

        Else

            Local $hImage = _GUIImageList_Create(16, 16, 5, 3)
            _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 18)
            _GUICtrlListView_SetImageList($List1, $hImage, 1)
            _GUICtrlListView_AddItem($List1, $Address[$z], 0)
            ;TCPCloseSocket($iSocket)

        EndIf

        $z += 1
    Next
    _ArrayDisplay($Address)
    Local $aArrayUnique = _ArrayUnique($Address) ; Use default parameters to create a unique array.
    _ArrayDisplay($aArrayUnique, "$aArray Unique")
    ; Close the socket.
    ;TCPCloseSocket($iSocket)

    _Display()
EndFunc   ;==>_Scan_IP

some one  have  idea??  thankz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...