0

I'm developing a Ruby application that functions as a remote control using a TCP connection. The application consists of a server that accepts commands from a remote client and executes them. One of the expected commands is to download a file from the server to the client.

The issue I'm facing is that the download seems to work correctly when the server and client are on different machines, but when I try to run both the server and client on the same machine, the process appears to hang during the download. Specifically, when I send the "download filename" command from the client, the download seems to stall, and the application becomes unresponsive until I close the client. After closing the client, the file appears to have been sent and saved.

I've verified that the file paths are correct both for the save directory and the full file path. Additionally, I've attempted to troubleshoot the issue with some code modifications, but so far, without success.

Here's a simplified version of my code: client-side:

elsif command.start_with?('download')
    client.puts(command)  # Invia il comando al server per avviare il download
    filename = client.gets.chomp  # Ricevi il nome del file dal server
  
    file_path = File.join(cartella_corrente, filename)  # Percorso completo per il salvataggio
    File.open(file_path, 'wb') do |file|
      loop do
        chunk = client.read(8000)
        break if chunk.nil? || chunk.empty? || chunk == "__END_OF_FILE__"
        file.write(chunk)
      end
    end

server-side:

elsif command.start_with?('download')
      client.puts("download")  # Invia il comando al client per avviare il download
      filename = command[9..-1]
      client.puts(filename)  # Invia il nome del file al server per iniziare il download
    
      save_path = File.join(SAVE_DIRECTORY, filename)
      if File.exist?(save_path)
        File.open(save_path, 'rb') do |file|
          loop do
            chunk = file.read(8000)
            break if chunk.nil? || chunk.empty?
            client.write(chunk)
            client.flush
          end
          client.puts("__END_OF_FILE__")  # Invia un indicatore alla fine
        end
        puts "Download completato. Salvato come #{filename}".colorize(:green)
      else
        client.puts("Errore: Il file #{filename} non esiste.")
      end
    else

I would like to understand what might be causing this behavior and how I can resolve this issue. Has anyone encountered a similar situation or has any suggestions on how I can proceed to troubleshoot and resolve the problem?

Thank you in advance for any assistance or suggestions!

Best regards, Alessandro.

I have attempted to perform file downloads, but the process hangs during the download. Alternatively, when attempting to modify actual files, they don't get copied as expected. In some cases, the download completes, but the resulting file is empty. Furthermore, I suspect, though I haven't yet tested, that the application might not work on different machines.

eglease
  • 2,445
  • 11
  • 18
  • 28
  • 1
    You assume that each `write` or `puts` matches exactly one `read` in the peer. This assumption is wrong. TCP is a byte stream, not a message protocol. This means your `__END_OF_FILE__` marker is likely appended to the last chunk of the file transfer and not received in a single `read` as you expect it. That you see this only on the same machine is maybe because the MTU is larger. – Steffen Ullrich Aug 21 '23 at 20:07
  • I've restructured the code as you mentioned Now, when I initiate the download using the command "download filename," the file is successfully saved to the desired folder of my choice. However, I'm encountering an issue where the downloaded file is consistently empty. I've taken various measures to debug the issue, including examining the chunk sizes and other aspects, but unfortunately, the problem persists – Alessandro Altobelli Aug 22 '23 at 14:10
  • What you describe is different problem then - which should be asked in a different question and also with the actual code you use now. – Steffen Ullrich Aug 22 '23 at 14:26
  • ok i post another question ifyou want to see the code you can go here https://stackoverflow.com/questions/76954439/issue-with-downloaded-files-being-empty – Alessandro Altobelli Aug 22 '23 at 14:44

0 Answers0