From: Bigmac Turdsplash Date: 2009-05-10T14:15:34+09:00 Subject: Re: File over tcp? with out using net/ftp Robert Klemme wrote: > On 09.05.2009 10:03, Philippe Chotard wrote: >> Bigmac Turdsplash wrote: >>> I have been doing a lot of google'n trying to find example code for >>> sending files threw the socket... I would really like to see some >>> exmaple code for the client and server... im clueless... >> >> Hi Bigmac, here is a simple exemple you can begin with. > > A few remarks: > >> * Client (sends the file) : >> require 'socket' >> file = File.new('file.jpg') >> fileContent = file.read > > The file handle is not closed properly. These two lines above can be > replaced by > > fileContent = File.read('file.jpg') > >> sock = TCPSocket.open('localhost', 2000) >> sock.print fileContent > > I'd rather use sock.write here as this is the more low level output > operation. > >> sock.close >> >> * Server (receives the file) : >> require 'socket' >> sock = TCPServer.open(2000) >> con = sock.accept >> msg = con.read >> destFile = File.new('file-received.jpg', 'w') >> destFile.print msg >> destFile.close > > Again, I'd rather use #write instead of #print here. And use the block > form of File.open. > > Some more general remarks: depending on operating systems involved files > should probably opened rather in binary mode than text mode. > > Then, as files can grow quite large, your approach (i.e. reading the > whole file into memory before writing it) is likely to fail for large > files. Usually a solution with small chunks is better. > > Kind regards > > robert This is vary helpful... will be easy for me to build functions off this example... i have tested with this and it works with text files but thats it... image files and executable's dont function after being sent threw the socket...you said i should open files in binary mode? how can i do this and then after words will the files function properly? i have been trying to get the client to receive files from the server... could i see one more example if its not to much to ask? -- Posted via http://www.ruby-forum.com/.