# Unix Sockets SOCK\_DGRAM

**URL:** <https://www.ruby-forum.com/t/unix-sockets-sock-dgram/152775>\
**Category:** Ruby\
**Created:** [November 7, 2008, 10:34am UTC](https://www.ruby-forum.com/t/unix-sockets-sock-dgram/152775 "2008-11-07T10:34:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [November 7, 2008, 10:34am UTC](https://www.ruby-forum.com/t/unix-sockets-sock-dgram/152775/1 "2008-11-07T10:34:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [November 7, 2008, 4:50pm UTC](https://www.ruby-forum.com/t/unix-sockets-sock-dgram/152775/2 "2008-11-07T16:50:59Z")

</div>

On Nov 7, 9:32 am, David R.  
[removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [November 7, 2008, 7:10pm UTC](https://www.ruby-forum.com/t/unix-sockets-sock-dgram/152775/3 "2008-11-07T19:10:32Z")

</div>

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
