UDPSocket in ruby

I am new to ruby ​​and follow the book "Ruby Programming Language", I am trying to learn some Socket in ruby, and the following is my simple server / client:

## server

require 'socket'

server= UDPSocket.new
server.bind('localhost', 3000)
loop do
    data,address=server.recvfrom(1024)
    server.send(data.reverse,0,address[3],address[1])  ############ My problem #########
    puts "get #{data} from #{address[3]}"
end

##client
require 'socket'

ds = UDPSocket.new
#ds.connect('localhost', 3000)
while line=gets
    ds.send(line.chomp, 0,'localhost', 3000)
    response,address = ds.recvfrom(1024)
    puts response
end

Pay attention to the line

server.send(data.reverse,0,address[3],address[1])

If I comment on this line, it seems that the server will hold on and will no longer respond to the client.

I wonder why?

Does this mean that UDPSocket must execute some kind of response to the client to continue?

+3
source share
1 answer

Since you write "recvfrom" on the server side, if you comment on this, it will not block and will continue to send data to the client side. However, in a real situation, peers in communication must exchange information.

+2
source

All Articles