S
Shannon Fang
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
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