Tag: tcp

在RoR中,如何从“无法打开TCP连接…(一般SOCKS服务器故障)”错误中恢复?

我正在使用Rails 4.2.7。 目前我正在通过SOCKS代理请求网页 begin … res1 = Net::HTTP.SOCKSProxy(‘127.0.0.1’, 50001).start(uri.host, uri.port) do |http| puts “launching #{uri}” resp = http.get(uri) status = resp.code content = resp.body content_type = resp[‘content-type’] content_encoding = resp[‘content-encoding’] end … rescue OpenURI::HTTPError => ex … rescue SocketError, Net::OpenTimeout, Zlib::BufError => e … end 偶尔我会得到以下错误.. Error during processing: Failed to open TCP connection to […]

基本的Ruby TCP服务器演示在启动时失败:`bind’:已经在使用的地址,Errno :: EADDRINUSE

我在Ruby中展示了一个简单的TCP服务器演示。 我的第一个演示是使用经典的C风格的bind-listen-accept-read-write-close方法。 这段代码第一次运作良好: require ‘socket’ class Server def start(bind: ‘127.0.0.1’, port: nil, backlog: 1) Socket.new(:INET, :STREAM).tap do |sock| sock.bind Addrinfo.tcp bind, port sock.listen backlog # the codes of client connecting with it here is for reproducing the issue more easily c = Socket.new(:INET, :STREAM) c.connect Addrinfo.tcp ‘127.0.0.1’, port client, client_addr = sock.accept puts “connected from […]

EventMachine的优点是什么?

这是我的测试用例,我发现EM并不比普通的TCP服务器快 EM服务器: require ‘rubygems’ require ‘benchmark’ require ‘eventmachine’ class Handler < EventMachine::Connection def receive_data(data) operation = proc do # simulate a long running request a = [] n = 5000 for i in 1..n a << rand(n) a.sort! end end # Callback block to execute once the request is fulfilled callback = proc do |res| […]

具有非阻塞或multithreadingfunction的Ruby Tcp Server类

找不到任何可以帮助创建非阻塞/multithreading服务器的gem或类。 哪里可以找到?

使用Ruby的TCPSocket-Class发送原始字节数组的最简单方法

我想使用Rubys TCPSocket-Class发送原始字节。 有人举个好榜样吗? 我已经尝试过这种方式,但它不起作用:( require ‘socket’ host = ‘192.168.0.80’ port = 102 s = TCPSocket.new(host, port) s.write [0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0xC1, 0x02, 0x02, 0x02, 0xC2, 0x02, 0x02, 0x02, 0xC0, 0x01, 0x0A ].pack(‘C’) puts s.read s.close puts “exit” 谢谢 :)

在gets()中从Ruby中损坏的TCP套接字恢复

我正在读取TCP套接字上的输入行,类似于: class Bla def getcmd @sock.gets unless @sock.closed? end def start srv = TCPServer.new(5000) @sock = srv.accept while ! @sock.closed? ans = getcmd end end end 如果端点在getline()运行时终止连接,则gets()挂起。 我该如何解决这个问题? 是否有必要进行非阻塞或定时I / O?

Ruby TCPSocket写不起作用,但是put呢?

我正在使用GServer和TCPSocket处理Ruby TCP客户端/服务器应用程序。 我遇到了一个我不明白的问题。 我的TCPSocket客户端成功连接到我的GServer,但我只能使用puts发送数据。 调用TCPSocket.send或TCPSocket.write什么都不做。 我缺少一些魔法吗? tcp_client = TCPSocket.new( ipaddr, port ) tcp_client.puts( ‘Z’ ) # -> GServer receives “Z\n” 但如果我使用写或发送… tcp_client = TCPSocket.new( ipaddr, port ) tcp_client.write( ‘Z’ ) # -> nothing is received tcp_client.send( ‘Z’ ) # -> nothing is received 谢谢您的帮助 附加信息: Linux和Windows上的行为是相同的。 写入后刷新套接字不会改变行为。

Heroku worker dyno上的进程之间的TCP套接字通信

我想知道如何在Heroku工作者dyno上的进程之间进行通信。 我们希望Resque工作者读取队列并将数据发送到在同一个dyno上运行的另一个进程。 “其他进程”是一种现成的软件,通常使用TCP套接字(端口xyz)来监听命令。 它被设置为在Resque工作程序启动之前作为后台进程运行。 但是,当我们尝试本地连接到该TCP套接字时,我们无处可去。 我们设置队列的Rake任务执行此操作: task “resque:setup” do # First launch our listener process in the background `./some_process_that_listens_on_port_12345 &` # Now get our queue worker ready, set up Redis backing store port = 12345 ENV[‘QUEUE’] = ‘*’ ENV[‘PORT’] = port.to_s Resque.redis = ENV[‘REDISTOGO_URL’] # Start working from the queue WorkerClass.enqueue end 这样做 – 我们的侦听器进程运行,Resque尝试处理排队的任务。 […]