On Nov 7, 9:32 am, David R.
[email protected] wrote:
end
But I need the client to send packets. I can not change from SOCK_DGRAM
to SOCK_STREAM as it would be sufficient to use UNIXServer and
UNIXSocket.
So how can I do it?
David
Posted viahttp://www.ruby-forum.com/.
I am not sure if this will help or not. In the example code below the
“user” process sends a datagram message to the “alarm manager” and the
“parameter store” processes. The “user” process then receives an ack
back.
#Simple simulation using domain datagram sockets. Everything is
started off when the user
#sets a parameter.
#Note that ruby does not provide specific support for domain datagram
sockets and we must use the “c” like
#basic_socket and socket classes directly (socket inherits from
basic_socket).
#Note that recvfrom blocks until a message is received.
require ‘socket’
USER_SOCKET = “user.sock”
PARAM_STORE_SOCKET = “param_store.sock”
ALARM_MGR_SOCKET = “alarm_mgr.sock”
system(“rm #{USER_SOCKET}”)
system(“rm #{PARAM_STORE_SOCKET}”)
system(“rm #{ALARM_MGR_SOCKET}”)
fork {# The User.
dgram_socket = Socket::new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
dgram_socket.bind(Socket.pack_sockaddr_un(USER_SOCKET))#Without
this the receiver does not know who we are when using recvfrom.
sleep(5);#Don't do anything until system is up.
dgram_socket.send("x = 4", 0,
Socket.pack_sockaddr_un(PARAM_STORE_SOCKET))
dgram_socket.send(“x = 4”, 0,
Socket.pack_sockaddr_un(ALARM_MGR_SOCKET))
while(1)
msg = dgram_socket.recvfrom(26)
print "User: #{msg[0]} from #{msg[1]}\n"
end
}
fork {# The Parameter Store.
dgram_socket = Socket::new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
dgram_socket.bind(Socket.pack_sockaddr_un(PARAM_STORE_SOCKET))#Without
this the receiver does not know who we are when using recvfrom.
sleep(5);#Don't do anything until system is up.
while(1)
msg = dgram_socket.recvfrom(26)
print "Parameter Store: #{msg[0]} from #{msg[1]}\n"
dgram_socket.send("ACK " + msg[0], 0,
Socket.pack_sockaddr_un(USER_SOCKET))
end
}
fork {# The Alarm Manager.
dgram_socket = Socket::new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
dgram_socket.bind(Socket.pack_sockaddr_un(ALARM_MGR_SOCKET))#Without
this the receiver does not know who we are when using recvfrom.
sleep(5);#Don't do anything until system is up.
while(1)
msg = dgram_socket.recvfrom(26)
print "Alarm Mgr: #{msg[0]} from #{msg[1]}\n"
dgram_socket.send("ACK " + msg[0], 0,
Socket.pack_sockaddr_un(USER_SOCKET))
end
}
Process.wait#Without this have to start using kill to get rid of the
processes created with fork.
Mark