在Ruby中处理TCP/IP通信并发可以通过多种方式实现,包括使用线程、异步IO和事件驱动库等。下面是一些常见的方法:
线程是Ruby中最基本的并发机制。你可以创建多个线程来同时处理多个TCP连接。
require 'socket'
server = TCPServer.new('localhost', 8080)
threads = []
server.each_connection do |client|
threads << Thread.new do
client.puts "Hello from thread #{Thread.current.object_id}"
client.close
end
end
threads.each(&:join)
Ruby的IO
模块提供了异步IO支持,可以使用IO.select
或IO.wait
来处理多个连接。
require 'socket'
require 'io/select'
server = TCPServer.new('localhost', 8080)
inputs = [server]
outputs = []
while inputs.any?
readable, writable, exceptional = IO.select(inputs, outputs, inputs)
readable.each do |sock|
if sock == server
client = server.accept
inputs << client
puts "New connection from #{client.peerhost}:#{client.peerport}"
else
data = sock.gets
puts "Received: #{data}"
outputs << sock
end
end
writable.each do |sock|
sock.puts "Message received"
end
exceptional.each do |sock|
puts "Exception on #{sock.path}: #{sock.error}"
inputs.delete(sock)
sock.close
end
end
Ruby中有许多事件驱动库,如EventMachine
和Celluloid
,它们可以帮助你更高效地处理并发连接。
require 'eventmachine'
class MyServer < EventMachine::Connection
def post_init
@data = ''
@send_data = false
end
def receive_data(data)
@data += data
if @data == 'hello'
@send_data = true
end
end
def send_data
if @send_data
send_data("Hello from EventMachine\n")
@send_data = false
end
end
def connection_closed
@data = nil
@send_data = false
end
end
EventMachine.run do
EventMachine.start_server('localhost', 8080, MyServer)
end
require 'celluloid'
require 'socket'
class MyServer
include Celluloid::IO
def initialize(host, port)
@server = TCPServer.new(host, port)
end
def start
@server.each_connection do |client|
handle_connection(client)
end
end
def handle_connection(client)
client.puts "Hello from Celluloid"
client.close
end
end
MyServer.run('localhost', 8080)
选择哪种方法取决于你的具体需求和应用场景。线程适用于简单的并发场景,异步IO适用于需要高性能的场景,而事件驱动库则适用于需要处理大量并发连接的场景。