Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

Plik przez TCP


Rekomendowane odpowiedzi

Opublikowano

Owszem. Zaraz jakiś example naklepię.

//example jest w helpie pod TCPSend ;)

 

Serwer

 

; I am the server, start me first! (Start in second the example 2 of the TCPRecv function).

#include <MsgBoxConstants.au3>
#include <FileConstants.au3>

Example()

Func Example()
    ; Assign a Local variable the path of a file chosen through a dialog box.
    Local $sFilePath = FileOpenDialog("Select a file to send", @MyDocumentsDir, "All types (*.*)", BitOR($FD_FILEMUSTEXIST, $FD_PATHMUSTEXIST))
    Local $iError = 0

    ; Note: Choose a file bigger than 4 kiB otherwise the first example is enough.

    ; If an error occurred display the error code and return False.
    If @ Then
        $iError = @
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONEXCLAMATION), "", "Server:" & @CRLF & "Invalid file chosen, Error code: " & $iError)
        Return False
    EndIf

    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.
    Local $sIPAddress = "127.0.0.1" ; This IP Address only works for testing on your own computer.
    Local $iPort = 65432 ; Port used for the connection.

    ; 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)

    ; If an error occurred display the error code and return False.
    If @ Then
        ; Someone is probably already listening on this IP Address and Port (script already running?).
        $iError = @
        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 @ Then
            $iError = @
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not accept the incoming connection, Error code: " & $iError)
            Return False
        EndIf
    Until $iSocket <> -1 ;if different from -1 a client is connected.

    ; Close the Listening socket to allow afterward binds.
    TCPCloseSocket($iListenSocket)

    ; Assign a Local variable the size of the file previously chosen.
    Local $iFileSize = FileGetSize($sFilePath)

    ; Assign a Local variable the handle of the file opened in binary mode.
    Local $hFile = FileOpen($sFilePath, $FO_BINARY)

    ; Assign a Local variable the offset of the file being read.
    Local $iOffset = 0

    ; Assign a Local variable the number representing 4 KiB.
    Local Const $i4KiB = 4096

    ; Note: The file is send by parts of 4 KiB.

    ; Send the binary data of the file to the server.
    Do
        ; Set the file position to the current offset.
        FileSetPos($hFile, $iOffset, $FILE_BEGIN)

        ; The file is read from the position set to 4 KiB and directly wrapped into the TCPSend function.
        TCPSend($iSocket, FileRead($hFile, $i4KiB))

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

            ; Close the socket.
            TCPCloseSocket($iSocket)
            Return False
        EndIf

        ; Increment the offset of 4 KiB to send the next 4 KiB data.
        $iOffset += $i4KiB
    Until $iOffset >= $iFileSize

    ; Close the file handle.
    FileClose($hFile)

    ; Tell the client the file is fully sent with a code.
    TCPSend($iSocket, @CRLF & "{EOF}")

    ; Display the successful message.
    MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "File sent.")

    ; Close the socket.
    TCPCloseSocket($iSocket)
EndFunc   ;==>Example

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
EndFunc   ;==>OnAutoItExit

 

 

Klient

 

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

; If you select the client button, start this script after and select the server button on:
; the second instance of this script OR on the script TCPRecv and vice versa.

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.
    Local $sIPAddress = "127.0.0.1" ; This IP Address only works for testing on your own computer.
    Local $iPort = 65432 ; Port used for the connection.

    #region GUI
    Local $hGUI = GUICreate("TCPSend", 150, 70)

    Local $iBtnClient = GUICtrlCreateButton("2. Client", 10, 10, 130, 22)

    Local $iBtnServer = GUICtrlCreateButton("1. Server", 10, 40, 130, 22)

    GUISetState(@SW_SHOW, $hGUI)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $iBtnClient
                GUISetState(@SW_DISABLE, $hGUI)
                _TCPSend_Client($sIPAddress, $iPort)
                GUISetState(@SW_ENABLE, $hGUI)
            Case $iBtnServer
                GUISetState(@SW_DISABLE, $hGUI)
                _TCPSend_Server($sIPAddress, $iPort)
                GUISetState(@SW_ENABLE, $hGUI)
        EndSwitch

        Sleep(10)
    WEnd

    GUIDelete($hGUI)
    #endregion GUI
EndFunc   ;==>Example

Func _TCPSend_Client($sIPAddress, $iPort)
    ; Assign a Local variable the socket and connect to a listening socket with the IP Address and Port specified.
    Local $iSocket = TCPConnect($sIPAddress, $iPort)
    Local $iError = 0

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

    ; Send the string "tata" to the server.
    TCPSend($iSocket, "tata")

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

    ; Close the socket.
    TCPCloseSocket($iSocket)
EndFunc   ;==>_TCPSend_Client

Func _TCPSend_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

    ; If an error occurred display the error code and return False.
    If @ Then
        ; Someone is probably already listening on this IP Address and Port (script already running?).
        $iError = @
        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 @ Then
            $iError = @
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not accept the incoming connection, Error code: " & $iError)
            Return False
        EndIf
    Until $iSocket <> -1 ;if different from -1 a client is connected.

    ; Close the Listening socket to allow afterward binds.
    TCPCloseSocket($iListenSocket)

    ; Assign a Local variable the data received.
    Local $sReceived = TCPRecv($iSocket, 4) ;we're waiting for the string "tata" OR "toto" (example script TCPRecv): 4 bytes length.

    ; 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.
    MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "Received: " & $sReceived)

    ; Close the socket.
    TCPCloseSocket($iSocket)
EndFunc   ;==>_TCPSend_Server

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
EndFunc   ;==>OnAutoItExit

 

846331404756772371599.jpeg
Opublikowano

wysyłanie to po prostu odczytywanie fragmentu pliku i wysyłanie go normalnie po tcp.

FileRead($plik, 1024) to przykładowy pakiet 1kb, TCPSend wysyłasz, klient używa TCPRecv, protokół sam pilnuje żeby doszło (jakbyś używał udp to byłoby szybciej, ale bez sprawdzania, więc bezużytecznie), tyle.

846331404756772371599.jpeg
Opublikowano

bez serwera i tak  nie wyśle pliku a on go z góry wykreślił jako potrzebny

If you = stupid then

insert(foot.in.your.ass)

end if

licznik-54-96732-stat.png

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...