Unix Sockets SOCK_DGRAM


#1

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


#2

On Nov 7, 9:32 am, David R.
removed_email_address@domain.invalid 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


#3

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