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.