Sign in to follow this  
Followers 0
GoFor

Send File

15 posts in this topic

Hi guys ;/ i can't do to in TCP protocol client send a file to server :/ I looked to the examples codes and it was too complicated to me ;/ could you explain me how to do it? :/ I want create just a small code first. Just send and receive small file...

Share this post


Link to post
Share on other sites



You say TCP, but you're saying send a file to a server. Are you trying to use TCP to send a string of data, or do you need to send a file, perhaps with FTP?


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

I can send string from client to server using TCP but I want to send a file from client to server. I dont know how to do this

Share this post


Link to post
Share on other sites

Why not just look at example #2 of both the TCPSend/TCPRecv functions? It is about sending and receiving a file using these functions.

Have a look at those examples and give a try and post any questions you have. :)


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

I looked at this. And I cant understand it. I tried to compile it and when I select the file then the program doing anything... :/

Share this post


Link to post
Share on other sites

Are you running the server and the client in the same script?

Please, tell us what you have tried or post the script you have tried. :whistle:


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

I tried this from examples:

#include <FileConstants.au3>
#include <MsgBoxConstants.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 @error Then
        $iError = @error
        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 @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
    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 @error Then
            $iError = @error
            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

And even this doesn't work ;/

Share this post


Link to post
Share on other sites

The server and client cannot be running in the same script. They must be separate scripts.


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

What exactly doesn't work.  You need to be specific...like this function returns an error, or something.  How can we help without knowing what to help with.

Edited by jdelaney
1 person likes this

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

I told it. When I start this program (code) and when I select the file, then the program just "stop". Doesn't show any msgbox and it do nothing ;/

Share this post


Link to post
Share on other sites

Put this line at the top of your script. Then, when it just "stops", hover your mouse over the icon in the System Tray and tell us what line it is stuck at:

Opt("TrayIconDebug", 1)

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Still don't show anything :/ Ill give you my own code of client and server which send just sting.

Server "1":

TCPStartup()
$mainsocket = TCPListen("192.168.1.103", 32399)
While 1
   $acceptedSocket = TCPAccept($mainsocket)
   If $acceptedSocket <> -1 Then
 $receivedData = TCPRecv($acceptedSocket, 1024)
      MsgBox(64, "Dostałeś wiadomość!", $receivedData)
      TCPCloseSocket($acceptedSocket)
   EndIf
WEnd

Client:

#include <GUIConstants.au3>


TCPStartup()


Global $serwer1 = '192.168.1.103'
Global $serwer2 = '192.168.1.106'
Global $serwer3 = '192.168.1.100'
Global $port1 = '32399'
Global $port2 = '32398'
Global $port3 = '32397'


$logowanie=GUICreate("Klient", 250, 100)
$zaloguj=GUICtrlCreateButton('Przeslij',70,44,100,20)


$serwery=GUICtrlCreateCombo("serwer 1", 70, 22, 100, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData($serwery, "serwer 2|serwer 3", "serwer 1")
GuiSetState(@SW_SHOW,"Klient")


$panel=GUICreate("Klient - panel", 250, 100)
$przeslij=GUICtrlCreateButton('Przeslij',70,44,100,20)
$rozlacz=GUICtrlCreateButton('Rozłącz',70,64,100,20)




Do
   $msg = GUIGetMsg()
   Switch GUICtrlRead($serwery)
      Case "serwer 1"
$wybral = $serwer1
EndSwitch
   if $msg = $zaloguj Then
 $wiadomosc=InputBox ( "Klient-serwer", "Wpisz wiadomosc ktora chcesz przekazac" , "" , "" , 300 , 300 , 0 , 0)
 if $wiadomosc = "" Then
MsgBox(0,'klient','nie mozesz wyslac pustej wiadomosci')
 Else
$serwer=GUICtrlRead($serwery)
if $serwer = 'serwer 1' Then
 $socket = TCPConnect($serwer1, $port1)
   ElseIf $serwer = 'serwer 2' Then
 $socket = TCPConnect($serwer2, $port2)
   ElseIf $serwer = 'serwer 3' Then
 $socket = TCPConnect($serwer3, $port3)
 EndIf
If $socket = -1 Then
MsgBox(16, "Błąd:", "Połączenie z serwerem nie można ustalić!")
EndIf
 $sendedBytes = TCPSend($socket, $wiadomosc)
 If $sendedBytes = 0 Then
MsgBox(16, "Error", "Pakiet nie może być wysłany.")
 EndIf
 TCPCloseSocket($socket)
 TCPShutdown()
   EndIf
   EndIf


   if $msg = $przeslij Then
 $wiadomosc=InputBox ( "Klient-serwer", "Wpisz wiadomosc ktora chcesz przekazac" , "" , "" , 300 , 300 , 0 , 0)
 if $wiadomosc = "" Then
MsgBox(0,'klient','nie mozesz wyslac pustej wiadomosci')
 Else
 $socket = TCPConnect("127.0.0.1", 65432) ;próbuje połączyć się z serwerem i zapisuje nr gniazda w $socked
If $socket = -1 Then ;jeśli $socket = -1, komunikat o błędach
MsgBox(16, "Błąd:", "Połączenie z serwerem nie można ustalić!")
EndIf
 $sendedBytes = TCPSend($socket, $wiadomosc) ;wysyła wiadomość do połączonego gniazda
 If $sendedBytes = 0 Then ;jeśli wartość zwracana TCPSend(...) = 0 to wystąpił błąd
MsgBox(16, "Error", "Pakiet nie może być wysłany.") ;komunikat błędu
 Else
MsgBox(64, "Klient", "Pakiet zostal dostarczony")
 EndIf
   EndIf
   EndIf




   if $msg = $rozlacz Then
 TCPCloseSocket($socket) ;zamknięcie otwartego połączenia
 TCPShutdown() ;koniec usługi TCP
 Exit
   EndIf


Until $msg = $GUI_EVENT_CLOSE

Sorry guys for polish strings. I think you can understand this.

Do you know how do to send a file not string?

Share this post


Link to post
Share on other sites

So, guys? Could you help me?:/

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

For one, your ports aren't the same. In the client you have port: 65432 and in the server you have port: 32399. ;)

They should be the same port. Give that a try and let me know how that goes.

EDIT: You can always make the server port: 0.0.0.0 so that it will listen on all ports, that way you can use any port in the client that you would need to use.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

But port 65432 isnt use in this script because he is "in" the button "$przeslij" which is deacitave. This part of code is unnecessary but im too lazy to delete this :P button which start TCP and sending is "$zaloguj". This code work good, but i want to rewrite it to that can send the file too but i don't have any idea how to do this ;/

Share this post


Link to post
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
Sign in to follow this  
Followers 0