I have a simple program that reads from a socket, compresses the data
and writes it to a file. Because I'm using zlib, I must intercept
SIGINT so I can close the GzipWriter before exiting or the file will
be corrupted. Problem is, sometimes my handler gets called, and
sometimes it doesn't. When it doesn't, the program just hangs and I
have to kill the process from task manager.
This is with ruby 1.8.2 (2004-07-29) [i386-mswin32] on Windows XP. If
I perform some additional manipulation of the data before compressing
it (as shown in the commented lines in the loop), the problem occurs
much more frequently.
I really want to use ruby for this task, but this problem is a
showstopper. Are there any workarounds I might try?
require 'socket'
require 'zlib'
sock = UDPSocket.open
sock.bind('', 6161)
f = File.new('junk.gz', 'wb')
outFile = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION)
trap('INT') { puts 'exiting'; outFile.close; exit 0 }
interval = 5
channelId = 1
loop {
packet = sock.recv(8192)
outFile << packet
#outFile << [sprintf('%08i', interval), channelId, sprintf('%05i',
#packet.size), packet].pack('a8 c a5 a*') << "\n"
puts "got #{packet.size} bytes"
}