Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The UDPSocket in ruby

Tags:

ruby

sockets

udp

I am new in ruby,and follow the book "The 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

Note the line

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

If I comment this line,it seems that the server will hold on ,and do not response to the client anymore.

I wonder why?

Does it mean that the UDPSocket must do some response to the client to continue?

like image 596
hguser Avatar asked May 18 '26 17:05

hguser


1 Answers

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

like image 127
wuliang Avatar answered May 20 '26 14:05

wuliang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!