Jump to content

TCP/IP Send & Reveive


Recommended Posts

Hey guys,

I use the code from https://www.autoitscript.com/forum/topic/19411-sending-files-and-stuff-over-tcp-to-everyone/ to send and reveive files from client to server.

But I have a unknown problem - the first file will send but the second file won't send.

Would anyone help me? Here are my Codes.

Sender (I think here it's all okay)

Global $ServerIp = @IPAddress1
    Global $FilePath_folders_txt = @ScriptDir & "\folders.txt" ;
    Global $FilePath_files_txt = @ScriptDir & "\files.txt"

    ; Send file folders.txt to the receiver
    TCPStartup()
    Global $ServerPort = "50911"
    Global $Upload_Socket_folders_txt = TCPConnect($ServerIp, $ServerPort)
    Global $Upload_FileData_folders_txt = FileRead($FilePath_folders_txt, FileGetSize($FilePath_folders_txt))
    TCPSend($Upload_Socket_folders_txt, FileGetSize($FilePath_folders_txt) & "," & $Upload_FileData_folders_txt)
    While 1
        $Upload_SizeConfirm_folders_txt = TCPRecv($Upload_Socket_folders_txt, 128)
        If @error Or StringLen($Upload_SizeConfirm_folders_txt) Then
        TCPShutdown()
            ExitLoop
        EndIf
    WEnd
    
    ; Send file files.txt to the receiver
    TCPStartup()
    Global $ServerPort = "50911"
    Global $Upload_Socket_files_txt = TCPConnect($ServerIp, $ServerPort)
    Global $Upload_FileData_files_txt = FileRead($FilePath_files_txt, FileGetSize($FilePath_files_txt))
    TCPSend($Upload_Socket_files_txt, FileGetSize($FilePath_files_txt) & "," & $Upload_FileData_files_txt)
    While 1
        $Upload_SizeConfirm_files_txt = TCPRecv($Upload_Socket_files_txt, 128)
        If @error Or StringLen($Upload_SizeConfirm_files_txt) Then
        TCPShutdown()
            ExitLoop
        EndIf
    WEnd

Reveiver (problem is marked with ####################################### on the right side)

Dim $Upload_MainSocket_folders_txt = -1, $Upload_Socket_folders_txt = -1
Dim $Upload_Ret_folders_txt = -1
Dim $Upload_Buffer_folders_txt = ""
Dim $Upload_Bytes_folders_txt = -1

Dim $Upload_MainSocket_files_txt = -1, $Upload_Socket_files_txt = -1
Dim $Upload_Ret_files_txt = -1
Dim $Upload_Buffer_files_txt = ""
Dim $Upload_Bytes_files_txt = -1

Global $FilePath_path_txt = @ScriptDir & "\path.txt"

Global $ServerIp = @IPAddress1

;
TCPStartup()
Global $ServerPort = "50911"
$Upload_MainSocket_folders_txt = TCPListen($ServerIp, $ServerPort)
While 1
    If $Upload_Socket_folders_txt = -1 Then
        $Upload_Ret_folders_txt = TCPAccept($Upload_MainSocket_folders_txt)
        If Not @error Then $Upload_Socket_folders_txt = $Upload_Ret_folders_txt
    Else
        $Upload_Buffer_folders_txt &= TCPRecv($Upload_Socket_folders_txt, 4096)
        If $Upload_Bytes_folders_txt = -1 And StringInStr($Upload_Buffer_folders_txt,",") Then
            $Upload_Bytes_folders_txt = StringLeft($Upload_Buffer_folders_txt,StringInStr($Upload_Buffer_folders_txt,",")-1)
            $Upload_Buffer_folders_txt = StringTrimLeft($Upload_Buffer_folders_txt,StringInStr($Upload_Buffer_folders_txt,","))
        Else
            If StringLen($Upload_Buffer_folders_txt) = $Upload_Bytes_folders_txt Then
                Global $FilePath_folders_txt = @ScriptDir & "\folders.txt" ;
                TCPShutdown()
                ExitLoop
            EndIf
        EndIf
    EndIf
WEnd
FileDelete($FilePath_folders_txt)
FileWrite($FilePath_folders_txt, $Upload_Buffer_folders_txt)

TCPStartup()
Global $ServerPort = "50911"
$Upload_MainSocket_files_txt = TCPListen($ServerIp, $ServerPort)
While 1
    If $Upload_Socket_files_txt = -1 Then ; Here the script is whiling. But the first file will receive successful with the same code. #######################################
        $Upload_Ret_files_txt = TCPAccept($Upload_MainSocket_files_txt)
        If Not @error Then $Upload_Socket_files_txt = $Upload_Ret_files_txt
    Else
        $Upload_Buffer_files_txt &= TCPRecv($Upload_Socket_files_txt, 4096)
        If $Upload_Bytes_files_txt = -1 And StringInStr($Upload_Buffer_files_txt,",") Then
            $Upload_Bytes_files_txt = StringLeft($Upload_Buffer_files_txt,StringInStr($Upload_Buffer_files_txt,",")-1)
            $Upload_Buffer_files_txt = StringTrimLeft($Upload_Buffer_files_txt,StringInStr($Upload_Buffer_files_txt,","))
        Else
            MsgBox(4096,"","Server - 2 6")
            If StringLen($Upload_Buffer_files_txt) = $Upload_Bytes_files_txt Then
                Global $FilePath_files_txt = @ScriptDir & "\files.txt" ;
                TCPShutdown()
                ExitLoop
            EndIf
        EndIf
    EndIf
WEnd
FileDelete($FilePath_files_txt)
FileWrite($FilePath_files_txt, $Upload_Buffer_files_txt)

Thanks a lot :)

Link to comment
Share on other sites

Got no time to test the code here, but i'd suggest you do the usual troubleshooting, putting some consolewrite's with the variables, and check them out.

Maybe it's not that the code doesn't receive, could be that the server doesn't send.

One thing that i would try to do is a for loop for all the files that are to be sent, instead of multiple copies of the same code for each file.

Same for the client, the ideal would be only one loop to download and create the files.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

2 hours ago, careca said:

Got no time to test the code here, but i'd suggest you do the usual troubleshooting, putting some consolewrite's with the variables, and check them out.

Maybe it's not that the code doesn't receive, could be that the server doesn't send.

One thing that i would try to do is a for loop for all the files that are to be sent, instead of multiple copies of the same code for each file.

Same for the client, the ideal would be only one loop to download and create the files.

Hey careca,

thanks for your reply. :) I take your advice to heart and put all code in a for loop. The problem is still there. :( 

sender.au3 (I think still that the problem is in receive.au3)

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>

Global $FilePath_folders_txt = @ScriptDir & '\folders.txt' ;
Global $FilePath_files_txt = @ScriptDir & '\files.txt'


Global $ServerIp = @IPAddress1
Global $ServerPort = '50911'

UpdateDir()

Func UpdateDir()
    Local $UpdateDir_FilePaths = StringSplit($FilePath_folders_txt & '|' & $FilePath_files_txt, '|')
    
    For $i = 1 to $UpdateDir_FilePaths[0]
        Local $FileData = FileOpen($UpdateDir_FilePaths[$i], $FO_READ)
        If $FileData = -1 Then
            FileWrite($UpdateDir_FilePaths[$i], @DesktopDir)
            Local $FileData = FileOpen($UpdateDir_FilePaths[$i], $FO_READ)
        EndIf
        If $i = 1 Then
            Local $FileDataList = _FileListToArray($CurrentPath, Default, $FLTA_FOLDERS, False)
        ElseIf $i = 2 Then
            Local $FileDataList = _FileListToArray($CurrentPath, Default, $FLTA_FILES, False)
        EndIf
        _FileWriteFromArray($UpdateDir_FilePaths[$i], $FileDataList, 1)
        FileClose($FileData)

        TCPStartup()
        Local $UpdateDir_Socket = TCPConnect($ServerIp, $ServerPort)
        Local $UpdateDir_FileData = FileRead($UpdateDir_FilePaths[$i], FileGetSize($UpdateDir_FilePaths[$i]))
        TCPSend($UpdateDir_Socket, FileGetSize($UpdateDir_FilePaths[$i]) & ',' & $UpdateDir_FileData)

        While 1
            $UpdateDir_SizeConfirm = TCPRecv($UpdateDir_Socket, 128)
            If @error Or StringLen($UpdateDir_SizeConfirm) Then
            TCPShutdown()
                ExitLoop
            EndIf
        WEnd
        FileDelete($UpdateDir_FilePaths[$i])
    Next
EndFunc

receive.au3 (line is marked with a comment)

Global $FilePath_folders_txt = @ScriptDir & '\folders.txt' ;
Global $FilePath_files_txt = @ScriptDir & '\files.txt'

Global $ServerIp = @IPAddress1
Global $ServerPort = '50911'

UpdateDir()

Func UpdateDir()
    Local $UpdateDir_FilePaths = StringSplit($FilePath_folders_txt & '|' & $FilePath_files_txt, '|')
    
    For $i = 1 to $UpdateDir_FilePaths[0]
        Local $UpdateDir_MainSocket = -1, $UpdateDir_Socket = -1
        Local $UpdateDir_Ret = -1
        Local $UpdateDir_Buffer = ''
        Local $UpdateDir_Bytes = -1

        ;
        TCPStartup()
        Global $ServerPort = '50911'
        $UpdateDir_MainSocket = TCPListen($ServerIp, $ServerPort)
        While 1
            If $UpdateDir_Socket = -1 Then
                $UpdateDir_Ret = TCPAccept($UpdateDir_MainSocket)
                If Not @error Then $UpdateDir_Socket = $UpdateDir_Ret
            Else
                $UpdateDir_Buffer &= TCPRecv($UpdateDir_Socket, 4096)
                If $UpdateDir_Bytes = -1 And StringInStr($UpdateDir_Buffer,',') Then
                    $UpdateDir_Bytes = StringLeft($UpdateDir_Buffer,StringInStr($UpdateDir_Buffer,',')-1)
                    $UpdateDir_Buffer = StringTrimLeft($UpdateDir_Buffer,StringInStr($UpdateDir_Buffer,','))
                Else
                                                                                                                ; The second file execution is lingering here. StringLen is never reched.
                    If StringLen($UpdateDir_Buffer) = $UpdateDir_Bytes Then
                        TCPShutdown()
                        ExitLoop
                    EndIf
                EndIf
            EndIf
        WEnd
        FileDelete($UpdateDir_FilePaths[$i])
        FileWrite($UpdateDir_FilePaths[$i], $UpdateDir_Buffer)
    Next
EndFunc

 

Link to comment
Share on other sites

I have isolated the error code 10054 in sender.au3: An existing connection was forcibly closed by the remote host. So I use TCPStartup() and TCPShutdown() outside the for-loops. Now it's start and stop only one time for both files instead of each file, as well in the sender as in receiver. Then the script lingering already in the first file upload in the "_sizeConfirm"-While loop on sender.au3 ... So I made a rollback. I don't know why therefore is another problem...

Link to comment
Share on other sites

Sadly only have one pc, or i would try to troubleshoot it too.

On a side note, from other examples of this i've seen, the server/receiving part would idle and wait for the connection and then would download whatever was sent to it, in your script i see you have it different, you already expect a file with the list of files and folders.

I thought you simply wanted to send a couple of files, did i get it wrong?

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

5 hours ago, careca said:

Sadly only have one pc, or i would try to troubleshoot it too.

On a side note, from other examples of this i've seen, the server/receiving part would idle and wait for the connection and then would download whatever was sent to it, in your script i see you have it different, you already expect a file with the list of files and folders.

I thought you simply wanted to send a couple of files, did i get it wrong?

I use the scripts also with one PC, it's not recommended but it works - Unfortunately only for one file. :(

I know AutoIt about six years but I never Programm very active... I only found this simple example and try to use it different - The others are to complex for me.

Ouh. Maybe it gives a better way to do following:

server: Send a multiline file over TCP to a Client, wait for two multiline files, receive, read and work with them.

client: Wait for a multiline file over TCP, receive and read it, respond depending on content of the file and send two multiline files to the server.

Uh I'm an idiot. I think I could send an array instead of the files to communicate with the server? I need help! :D

But download whatever sent to the server is a code I must understand too. :lol:

Link to comment
Share on other sites

Hey, i've been messing with this during the day, and i think i got somewhere.

So the server is in standby, waiting for the client to connect, every file downloaded goes to script dir/downloaded.

When the client is opened, asks user to select a folder, then connects to the server and uploads all files in that folder, recursively, change flag to change behaviour. The server creates the folder tree inside the folder mentioned just like the source.

Let me know how it goes, i know there are improvements to be made but as a proof of concept, i think it's ok.

Server:

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <AutoItConstants.au3>

Global $CurrentPath = @ScriptDir
Global $ServerIp = @IPAddress1
Global $ServerPort = '50911'
Global $iOffset = 0
Global $i4KiB = 4096
Global $iFileSize, $Split

TCPStartup()
UpdateDir()
TCPShutdown()

Func UpdateDir()
    $UpdateDir_MainSocket = TCPListen($ServerIp, $ServerPort)
    ConsoleWrite('TCPListen: '&$UpdateDir_MainSocket&@CRLF)
    While 1
    Do
        $Socket = TCPAccept($UpdateDir_MainSocket)
        Sleep(100)
    Until $Socket <> -1
    ConsoleWrite('Connected! = ' & $Socket & @CRLF)
    Sleep(500)
    $File = TCPRecv($Socket, $i4KiB, 0)
    $Split = StringSplit(BinaryToString($File), '<Filename>', 1)
    $File = $Split[1]
    $SecSplit = StringToBinary($Split[2])
    ConsoleWrite('Recv File: ' & $File & @CRLF)
    Sleep(500)
    ;=============================================================================
    Local $bEOF = Binary(@CRLF & "{EOF}")
    Local $iEOFLen = BinaryLen($bEOF)
    Local $bData = $SecSplit&Binary("")
    Local $iDataLen = 0
    Local $bEOFReached = False
    Do
        $bData &= TCPRecv($Socket, $i4KiB, 1)
        ;ConsoleWrite('TCPRecv: '&TCPRecv($Socket, $i4KiB, 1) &@CRLF)
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Connection lost, Error code: " & $iError)
            Exit
            Return False
        EndIf
        $iDataLen = BinaryLen($bData)
        ConsoleWrite('$iDataLen: '&$iDataLen &@CRLF)
        If $iDataLen = 0 Then ContinueLoop
        If BinaryMid($bData, 1 + $iDataLen - $iEOFLen, $iEOFLen) = $bEOF Then
            $bData = BinaryMid($bData, 1, $iDataLen - $iEOFLen)
            $bEOFReached = True
        EndIf
        Sleep(30)
    Until $bEOFReached
        $FO = FileOpen('Downloaded\' & $File, 26)
        FileWrite($FO, $bData)
        FileClose($FO)
        TCPCloseSocket($Socket)
        $bData = ''
        $iDataLen = 0
        Sleep(500)
    WEnd
EndFunc   ;==>UpdateDir

Client:

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <AutoItConstants.au3>

Global $ServerIp = @IPAddress1
Global $ServerPort = '50911'
Global $UpdateDir_MainSocket = -1
Global $UpdateDir_Ret = -1
Global $UpdateDir_Buffer = ''
Global $UpdateDir_Bytes = 0
Global $i4KiB = 4096
Global $FO
Global $FolderSel = FileSelectFolder('Select Folder to Upload', @ScriptDir, 6, @ScriptDir)

TCPStartup()
UpdateDir()
TCPShutdown()

Func UpdateDir()
        Local $FileDataList = _FileListToArrayRec($FolderSel, Default, $FLTAR_FILES, 1, 0, 1)
        For $i = 1 To $FileDataList[0]
            Do
                Local $Socket = TCPConnect($ServerIp, $ServerPort)
                Sleep(100)
            Until $Socket <> -1
            ConsoleWrite('Connected! = ' & $Socket & @CRLF)
            ConsoleWrite('Sent File: '& $FileDataList[$i]&@CRLF)
            TCPSend($Socket, $FileDataList[$i]&'<Filename>')
            ;=============================================================================
            $iFileSize = FileGetSize($FolderSel & '\' & $FileDataList[$i])
            Local $hFile = FileOpen($FolderSel & '\' & $FileDataList[$i], $FO_BINARY)
            $iOffset = 0
            Do
                FileSetPos($hFile, $iOffset, $FILE_BEGIN)
                TCPSend($Socket, FileRead($hFile, $i4KiB))
                If @error Then
                    $iError = @error
                    MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not send the data, Error code: " & $iError)
                    TCPCloseSocket($Socket)
                    Return False
                EndIf
                $iOffset += $i4KiB
                ConsoleWrite($iOffset & ' > ' & $iFileSize & @CRLF)
                Sleep(30)
            Until $iOffset >= $iFileSize
            FileClose($hFile)
            TCPSend($Socket, @CRLF & "{EOF}")
            TCPCloseSocket($Socket)
            Sleep(1500)
        Next
EndFunc   ;==>UpdateDir

 

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

On 20.1.2019 at 9:50 PM, careca said:

Hey, i've been messing with this during the day, and i think i got somewhere.

So the server is in standby, waiting for the client to connect, every file downloaded goes to script dir/downloaded.

When the client is opened, asks user to select a folder, then connects to the server and uploads all files in that folder, recursively, change flag to change behaviour. The server creates the folder tree inside the folder mentioned just like the source.

Let me know how it goes, i know there are improvements to be made but as a proof of concept, i think it's ok.

Hey, sorry that I reporting you back only just now. (And besides sorry for my worst english :D I'm student in germany and I don't speak English beyond my english lessons in school. Denglish ;))

Thanks for your big creative power in this scripts! Wonderful examples of a good work. (Goddamn english knowledge. I note that I must speak and think much more of this.)

I will be in touch with you, when I've looked up your codes more exact. But at the end a couple of suggestions:

- Could the script send the data in more than 4kb packages?

- What you meant with "change flag to change behaviour"? I had the idea, that the script could send multiple files and folders at once. Like select folder 1, 2 and 4 and file 11 to 50 and 66 and send it. I mention this of course because I haven't the knowledge to do that. :( But I would learn it if I could. (Yes, if-clause type II :D)

- delete Thumbs.db in each folder you created or check if you've permissions to send a file. (Some files are used by other programs. This would disturb the process, did I get it wrong?

Regards

Link to comment
Share on other sites

12 hours ago, Kor_Sharoth said:

- Could the script send the data in more than 4kb packages?

"change flag to change behaviour"

- delete Thumbs.db in each folder

Hi, im not sure you can send bigger than 4Kb, help file states the max should be 2Kb, i've seen some example that used 4Kb so i went with that.

I think it's fast enough, specially if you reduce the sleep in the loop, the sleep is the main restrictor in speed, it seems.

What i mean about the flag, is the flag to make it recursive. You can change that behaviour and not make it recursive.

The script is a proof of concept as i said, meaning from there on you can do many things, like what you said, selecting specific folders and so on,

then you send the selected folders, that's possible.

As for the thumbs.db files, those are supposed to be hidden, i think.

They don't show up here, probably one of the settings in the explorer that i've set.

Anyway you can do it if you need, in the script and find all thumbs.db file and delete 'em.

You can check for permissions, but that would be more code to add that i didn't feel like adding at the moment, just wanted to make a functional file transfer script without a big overhead.

Let me know if you need any help with any of that, and what you'd like the end goal to be with your script.

PS: English is also my secondary language.

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

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...