Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Unix Sockets SOCK_DGRAM
David Rodriguez
11/7/2008 9:33:00 AM
Hi
THis is my first post so I hope I do not break any rule with it.
I did not found how to send packets via a datagram UNIX sockets? I was
able to create a server using:
sock = Socket::new( Socket::AF_UNIX, Socket::SOCK_DGRAM, 0 )
sock.bind(Socket.pack_sockaddr_un($path))
for i in 1..10
p sock.recvfrom(124)
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 via
http://www.ruby-...
.
2 Answers
mail@msryan.co.uk
11/7/2008 3:49:00 PM
0
On Nov 7, 9:32 am, David Rodriguez
<david.francisco.rodrig...@gmail.com> wrote:
> Hi
>
> THis is my first post so I hope I do not break any rule with it.
>
> I did not found how to send packets via a datagram UNIX sockets? I was
> able to create a server using:
> sock = Socket::new( Socket::AF_UNIX, Socket::SOCK_DGRAM, 0 )
> sock.bind(Socket.pack_sockaddr_un($path))
> for i in 1..10
> p sock.recvfrom(124)
> 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 via
http://www.ruby-...
.
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
David Rodriguez
11/7/2008 6:09:00 PM
0
Mark
Thanks. It worked just fine.
I just had to do:
$path="home"
dgram_socket = Socket::new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
dgram_socket.send("HELLO RUBY", 0,Socket.pack_sockaddr_un($path))
Thanks a lot.
David
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Unix Sockets SOCK_DGRAM
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password