Forum: Ruby UDP Socket problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A5d5aaea336cd4c20ef8ba310f697d27?d=identicon&s=25 xrfang (Guest)
on 2005-11-24 16:38
(Received via mailing list)
Hi there,

I try to write a comm program based on udp broadcasting protocol. I did
the
following:

SERVER:

require 'socket'
server = UDPSocket.open
server.bind('0.0.0.0', 4321)
while true do
  p server.recvfrom(10)
end

CLIENT

require 'socket'
socket = UDPSocket.open
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
socket.send("sample", 0, '255.255.255.255', 4321)

Now there are 2 problems:

1) the server will print out 2 line of data e.g.:
["sample", ["AF_INET", 1339, "XRFANG-OTM", "192.168.1.101"]]
["sample", ["AF_INET", 1339, "XRFANG-OTM", "192.168.1.101"]]

but the client only sent one packet.

2) if the client send data more than 10 bytes, the server will crash! I
don't know how to set the "recvfrom" so that it can receive ANY byte of
data. i.e. when the client supplied more than the maximum wanted data
for
the server, the rest should be received on the next call to recvfrom, or
at
least, it should simply discard the excessive data, it must not crash
the
server anyway.

Thank you very much!

Shannon
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 decoux (Guest)
on 2005-11-24 17:02
(Received via mailing list)
>>>>> "S" == Shannon Fang <xrfang@hotmail.com> writes:


 I can't reproduce

S> CLIENT

moulon% ruby
require 'socket'
socket = UDPSocket.open
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
socket.send("sample", 0, '255.255.255.255', 4321)
socket.send("1234567890123456789012345678901234567890123456789012345678901234567890",
0, '255.255.255.255', 4321)
moulon%


S> SERVER

moulon% ruby
require 'socket'
server = UDPSocket.open
server.bind('0.0.0.0', 4321)
while true do
 p server.recvfrom(10)
end
["sample", ["AF_INET", 47842, "moulon.inra.fr", "138.102.114.1"]]
["1234567890", ["AF_INET", 47842, "moulon.inra.fr", "138.102.114.1"]]
^C
-:5:in `recvfrom': Interrupt
	from -:5
moulon%


Guy Decoux
A5d5aaea336cd4c20ef8ba310f697d27?d=identicon&s=25 xrfang (Guest)
on 2005-11-25 02:15
(Received via mailing list)
I run the program on winxp, is that causing problems???

I will test on linux, if anything interesting I will report.

thanks,
shannon
A5d5aaea336cd4c20ef8ba310f697d27?d=identicon&s=25 xrfang (Guest)
on 2005-11-25 04:12
(Received via mailing list)
Hi,

I have tested this on my linux, it worked fine, but this problem do
exist on
both win2k (pro.) and winxp (home).

Any ideas?

thanks a lot!
shannon
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 vjoel (Guest)
on 2005-11-25 09:26
(Received via mailing list)
Shannon Fang wrote:
> while true do
> Now there are 2 problems:
>
> 1) the server will print out 2 line of data e.g.:
> ["sample", ["AF_INET", 1339, "XRFANG-OTM", "192.168.1.101"]]
> ["sample", ["AF_INET", 1339, "XRFANG-OTM", "192.168.1.101"]]
>
> but the client only sent one packet.

Can't reproduce that. Does it happen repeatably? Does it happen if you
do
   server.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
after opening the server socket?

I'm on WinXP, with ruby-1.8.2 from the installer.

> 2) if the client send data more than 10 bytes, the server will crash! I
> don't know how to set the "recvfrom" so that it can receive ANY byte of
> data. i.e. when the client supplied more than the maximum wanted data
> for the server, the rest should be received on the next call to
> recvfrom, or at least, it should simply discard the excessive data, it
> must not crash the server anyway.

I see that too:

server.rb:6:in `recvfrom': A message sent on a datagram socket was
larger than the internal message buffer or some other network limit, or
the buffer used to receive a datagram into was smaller than the datagram
itself. - recvfrom(2) (Errno::EMSGSIZE)
         from server.rb:6

That doesn't seem like winsock has correctly implemented UDP. Shouldn't
recvfrom discard extra bytes?

Googling for "winsock udp broadcast" shows that others have found
similar problems. Particularly:

http://forum.java.sun.com/thread.jspa?threadID=680...

I guess this means the receiver needs to (a) know the max size of
legitimate data and (b) handle EMSGSIZE (at least logging an error, but
maybe not exiting).

Useful to know. Thanks!
22b74ae3adfc74ff5c7c0b0a94b78a48?d=identicon&s=25 kozlov.y (Guest)
on 2005-11-25 10:10
(Received via mailing list)
yuray@yuray:~/r$ ruby --version
ruby 1.8.2 (2004-12-25) [i686-linux]
yuray@yuray:~/r$ cat c.rb
require 'socket'
socket = UDPSocket.open
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
socket.send("sample1234567890", 0, '255.255.255.255', 4321)   ### more
then 10

yuray@yuray:~/r$
yuray@yuray:~/r$ cat s.rb
require 'socket'
server = UDPSocket.open
server.bind('0.0.0.0', 4321)
while true do
  p server.recvfrom(10)
end

yuray@yuray:~/r$ ruby s.rb
["sample1234", ["AF_INET", 32783, "yuray.####.ru.", "172.20.1.33"]]

Works for me. Client and server was running on single machine.
What is ruby version?
Can you run the test client and server on single machine?
Can you test with a new clear ruby installation without any packages ?

Regards,
Yuri Kozlov
A5d5aaea336cd4c20ef8ba310f697d27?d=identicon&s=25 xrfang (Guest)
on 2005-11-26 00:47
(Received via mailing list)
This problem confirmed to be a winsock problem, not ruby's. tks! btw, I
run
ruby 1.8.2 on winxp.

shannon
This topic is locked and can not be replied to.