hi, i'm very new to ruby and so i'd be very grateful of any help on
this. I am writing a script that listens for UDP packets on a
particular port. This is very easy using 'socket'. I then want to make
a script that simulates lots of clients sending UDP packets to the
listener. These clients should send a UDP packet from a particular port
and then listen for a response on that same port. For this reason, I am
trying to run each simulated client in a different thread. A simplified
version of what i have done so far is shown below, however, when I run
this, the client stops sending packets after the 66th one. Can someone
explain this to me please? why is it stopping at 66? which limit am i
hitting?
Also, any suggestions on how to improve the code will be gratefully
received. Thanks
#### Listener.rb ####
require "socket"
socket = UDPSocket.open
socket.bind("",1500)
while true
message, details = socket.recvfrom(512)
p message
end
#### Clients.rb ####
require 'socket'
SERVER_IP = "127.0.0.1"
SERVER_PORT = 1500
packets=[]
threads =[]
# Build an array of packets
for i in 1..100
packets << "packet#{i}"
end
# For Packet to be sent, start a new thread
for packets_to_send in packets
sleep 0.1
threads << Thread.new(packets_to_send) do |packet|
#Generate a random port
port = rand(60000)
# Open a socket on that port
socket = UDPSocket.open
socket.bind("",port)
# Send the packet
puts "Sending #{packet}"
socket.send(packet, 0, SERVER_IP, SERVER_PORT)
while true
message, message_details = socket.recvfrom(512)
end
end
end
···
--
Posted via http://www.ruby-forum.com/.