UDPSocket question

J

Joe Van Dyk

I'm having problems creating a UDP socket that listens for messages on
a port and (sometimes) sends messages back to the original sender of
the message.

I can receive a message over the UDP socket fine, I'm just not sure
about how to know where to send the message back, and how to properly
send it back.

Thanks,
Joe
 
J

Joe Van Dyk

What would be great if someone could fill in the blanks for me, or
point me to somewhere that does:

if fork=20
# UDP Server
server =3D UDPSocket.open
server.bind 'localhost', 12345
while (message, sender =3D server.recvfrom(64))
puts "We received: <#{message}> from #{sender[2]}"
=20
return_message =3D "Hi there client!"
# Send response message here
end
else
=20
# UDP Client
client =3D UDPSocket.open
client.connect 'localhost', 12345
client.puts "Hello Server!"
# assert that we got "Hi there client!" back
end
 
G

Guillaume Marcais

What would be great if someone could fill in the blanks for me, or
point me to somewhere that does:
Untested:

if fork
# UDP Server
server = UDPSocket.open
server.bind 'localhost', 12345
while (message, sender = server.recvfrom(64))
puts "We received: <#{message}> from #{sender[2]}"

return_message = "Hi there client!"
# Send response message here
server.send(return_message, 0, sender[2], sender[1])
end
else

# UDP Client
client = UDPSocket.open
client.connect 'localhost', 12345
client.puts "Hello Server!"
# assert that we got "Hi there client!" back
end

See UDPSocket#send in the Pickax.

Guillaume.
 
J

Joe Van Dyk

Seems to work great, thanks!

What would be great if someone could fill in the blanks for me, or
point me to somewhere that does: =20
Untested:
=20
if fork
# UDP Server
server =3D UDPSocket.open
server.bind 'localhost', 12345
while (message, sender =3D server.recvfrom(64))
puts "We received: <#{message}> from #{sender[2]}"

return_message =3D "Hi there client!"
# Send response message here
server.send(return_message, 0, sender[2], sender[1])
end
else

# UDP Client
client =3D UDPSocket.open
client.connect 'localhost', 12345
client.puts "Hello Server!"
# assert that we got "Hi there client!" back
end
=20
See UDPSocket#send in the Pickax.
=20
Guillaume.
=20=20
=20
 
J

Joe Van Dyk

Related question:

If I need to send a message that's greater than 64KB back, is there
some standard Ruby way of doing so? (64KB seems to be the maximum
packet size)

Seems to work great, thanks!
=20
What would be great if someone could fill in the blanks for me, or
point me to somewhere that does:
Untested:

if fork
# UDP Server
server =3D UDPSocket.open
server.bind 'localhost', 12345
while (message, sender =3D server.recvfrom(64))
puts "We received: <#{message}> from #{sender[2]}"

return_message =3D "Hi there client!"
# Send response message here
server.send(return_message, 0, sender[2], sender[1])
end
else

# UDP Client
client =3D UDPSocket.open
client.connect 'localhost', 12345
client.puts "Hello Server!"
# assert that we got "Hi there client!" back
end

See UDPSocket#send in the Pickax.

Guillaume.
I'm having problems creating a UDP socket that listens for messages= on
a port and (sometimes) sends messages back to the original sender o= f
the message.

I can receive a message over the UDP socket fine, I'm just not sure
about how to know where to send the message back, and how to proper= ly
send it back.

Thanks,
Joe
 
Y

Yohanes Santoso

Joe Van Dyk said:
Related question:

If I need to send a message that's greater than 64KB back, is there
some standard Ruby way of doing so? (64KB seems to be the maximum
packet size)

No. That's the maximum size of a UDP packet, and since UDP is a
datagram protocol, there is no mechanism to say 'this packet is a
continuation of that packet' at the UDP level. You'd have to do so at
a higher level.

YS.
 
J

Joe Van Dyk

Are there any 'tricks' that I can use to make sending data messages
larger than 64KB back over UDP? (i.e. any Ruby functions that will
break up a message into smaller chunks and then reassemble)
 
Y

Yohanes Santoso

Joe Van Dyk said:
Are there any 'tricks' that I can use to make sending data messages
larger than 64KB back over UDP? (i.e. any Ruby functions that will
break up a message into smaller chunks and then reassemble)

No. UDP really does not have the necessary features needed for
establishing a virtual-circuit (which is what you want).

You'd need to add the following at the very minimum:

1. Message fragmentation/defragmentation
2. Packet ordering
3. Packet lost detection
4. Packet resending

Do the above, and you'll have TCP minus flow control, which begs the
question of why not use TCP in the first place. I don't know what your
requirements are, so I can't say, 'use TCP, don't use udp'.

There is a plethora of protocols over UDP that tries to emulate some
of TCP functionalities, like LEAP (lightweight & efficient application
protocols) (disclaimer: i was involved in development of LEAP
protocols), or a darling among the media industry: RTSP (Real Time
Streaming Protocol) Usually they were created due to the need of a
protocol that is 'lighter' than TCP but provides the reliability of a
virtual circuit; a compromise.

So, either use those protocols, or you'd have to make one yourself.

YS.
 
B

Bill Kelly

From: "Joe Van Dyk said:
Are there any 'tricks' that I can use to make sending data messages
larger than 64KB back over UDP? (i.e. any Ruby functions that will
break up a message into smaller chunks and then reassemble)

Are you sure TCP is inappropriate for your application?

Off-hand I'm not aware of Ruby libraries to do what you're
asking, but I just wanted to mention, if you tackle it
yourself: Remember UDP packets may arrive at the destionation
out of sequence; the same packet may arrive more than once;
or not at all.


Regards,

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top