代码之家  ›  专栏  ›  技术社区  ›  Jesse Jashinsky

通过Ruby的tcpserver发送较大文件时出现问题

  •  3
  • Jesse Jashinsky  · 技术社区  · 14 年前

    我已经建立了一个简单的单服务服务器,它可以很好地处理小的测试文件,但是当我尝试运行更大、更实用的文件时,事情会出错。

    发送一个5.2 MB的文件可以正常工作。发送一个30.3MB的文件是可行的,但需要很长的时间(比如15分钟左右)。发送38.5 MB文件时,服务器接收到该文件,但随后抛出错误: msconvert_server.rb:20:in 'write': Invalid argument - 179.raw (Errno::EINVAL) (179.raw是文件名)。

    我不知道为什么会这样。 This forum 似乎有答案,虽然它确实加快了发送和接收时间,也得到了第20行,但在另一个点上失败了。我不相信TCP有任何文件大小限制,这让我相信问题在于Ruby代码。有没有人看到这个问题,或者对这里可能发生的事情有什么想法?

    这是密码。

    服务器 :

    require 'socket'
    
    server = TCPServer.open(2000)
    loop {
      client = server.accept
    
      filename = client.gets.chomp
    
      puts "Reading contents of #{filename}.raw"
      raw_data = client.gets("\r\r\n\n").chomp("\r\r\n\n")
    
      (Line 20, where error occurs) File.open(filename + ".raw", 'wb') {|out| out.print raw_data}
    
      puts "Converting #{filename}"
    
      #It's lame to have a script run a script, but it's the only way to get this to work.
      system "scriptit.bat " + filename + ".raw"
    
      puts "Sending contents of #{filename}.mzML"
      client.print IO.read(filename + ".mzML")
      client.print "\r\r\n\n"
      puts "Done"
      client.close
    }
    

    顾客 :

        host = config_value("//Host/@ip")
        port = 2000
    
        client = TCPSocket.open(host, port)
    
        fileName = @file.split("/")[-1].chomp(File.extname(@file))
    
        puts "Sending raw file"
        client.puts fileName
        client.print(File.open("#{@file}", "rb") {|io| io.read})
        client.print("\r\r\n\n")  #This is the delimiter for the server
    
        puts "Receiving mzML file"
        File.open("#{$path}../data/spectra/#{fileName}.mzML", 'wb') {|io| io.print client.gets("\r\r\n\n")}
        client.close
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   Community CDub    8 年前

    我找到了解决这个问题的办法。通过应用来自 a previous question ,我将代码改为:

    服务器 : 需要“socket”

    server = TCPServer.open(2000)
    loop {
      client = server.accept
    
      filename = client.gets.chomp
    
      puts "Reading contents of #{filename}.raw"
      raw_data = client.gets("\r\r\n\n").chomp("\r\r\n\n")
      file = File.open(filename + ".raw", 'wb')
      file.print raw_data
      file.close
    
      puts "Converting #{filename}"
      #It's lame to have a script run a script, but it's the only way to get this to work.
      system "scriptit.bat " + filename + ".raw"
    
      puts "Sending contents of #{filename}.mzML"
      data = IO.read(filename + ".mzML")
      client.print data
      client.print "\r\r\n\n"
    
      puts "Done"
      client.close
    }
    

    顾客 :

    host = config_value("//Host/@ip")
    port = 2000
    
    client = TCPSocket.open(host, port)
    
    fileName = @file.split("/")[-1].chomp(File.extname(@file))
    
    puts "Sending raw file"
    client.puts fileName
    data = IO.read("#{@file}")
    client.print data
    client.print "\r\r\n\n"  #This is the delimiter for the server
    
    puts "Receiving mzML file"
    file = File.open("#{$path}../data/spectra/#{fileName}.mzML", 'wb')
    data = client.gets("\r\r\n\n")
    file.print data
    
    client.close
    

    似乎扩展IO一行程序可以解决我的大部分大型文件Ruby问题。