Ruby - 将消息发送到多个IP地址

时间:2015-04-09 22:33:08

标签: ruby sockets chat

我如何向多个接收者发送一条消息?

这基本上就是我想做的事情:

    require 'socket'
ip = ['IP 1', 'IP 2']
port = 18000
loop {
    message = gets.chomp()
    conn = TCPSocket.open(ip, port)
    conn.write(message)
    conn.close_write
}

1 个答案:

答案 0 :(得分:0)

尝试迭代ips数组:

ips = ['IP 1', 'IP 2']
port = 18000

loop do 
  message = gets.chomp()

  ips.each do |ip|
    conn = TCPSocket.open(ip, port)
    conn.write(message)
    conn.close_write
  end
end