# Simple daemon which can speak and listen

**URL:** <https://www.ruby-forum.com/t/simple-daemon-which-can-speak-and-listen/161497>\
**Category:** Ruby\
**Created:** [February 26, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/simple-daemon-which-can-speak-and-listen/161497 "2009-02-26T23:16:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sai](https://www.ruby-forum.com/u/sai)\
**Post date:** [February 26, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/simple-daemon-which-can-speak-and-listen/161497/1 "2009-02-26T23:16:02Z")

</div>

Hi all. According to you, what could be the more sexy way to write a  
simple Ruby daemon (version 1.9 :)) which can speak and listen in the  
same time on a socket (UDP in my case)?

Thanks,  
Sai

---

<div class="post-metadata">

**Author:** ![sai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sai](https://www.ruby-forum.com/u/sai)\
**Post date:** [February 26, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/simple-daemon-which-can-speak-and-listen/161497/2 "2009-02-26T23:27:11Z")

</div>

sock = UDPSocket.new  
Thread.new do  
sock.write(“some string”)  
end  
puts sock.read

However, does it _really_ need to be at the same time? can it not just  
take turns? If you’re trying to do two things at the same time on the  
same object, it suggests you need two objects. Just a thought

Sai Hl wrote:

> Hi all. According to you, what could be the more sexy way to write a  
> simple Ruby daemon (version 1.9 :)) which can speak and listen in the  
> same time on a socket (UDP in my case)?
> 
> Thanks,  
> Sai

.

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![sai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sai](https://www.ruby-forum.com/u/sai)\
**Post date:** [February 27, 2009, 10:33am UTC](https://www.ruby-forum.com/t/simple-daemon-which-can-speak-and-listen/161497/3 "2009-02-27T10:33:10Z")

</div>

Sai Hl wrote:

> Hi all. According to you, what could be the more sexy way to write a  
> simple Ruby daemon (version 1.9 :)) which can speak and listen in the  
> same time on a socket (UDP in my case)?

More sexy than what - have you written something already? If so, post it  
and we’ll look.

Docs you need:  
[http://www.ruby-doc.org/stdlib/libdoc/socket/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/socket/rdoc/)  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib\_network.html](http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_network.html)

The first of these is unfortunately incomplete, e.g. UDPSocket#send is  
missing. However the second has everything you need under the UDPSocket  
header.

A normal UDP server would typically have a loop where it waits for a  
message, processes it, and then sends back a response to the sender.  
e.g.

require ‘socket’  
sock = UDPSocket.new  
sock.bind(Socket.gethostname, 12345)  
loop do  
packet, sender = sock.recvfrom(1500)  
response = “hello #{packet}”  
sock.send response, 0, sender[3], sender[1]  
end

Test like this in another window:

$ irb  
irb(main):001:0\> require ‘socket’  
=\> true  
irb(main):002:0\> s = UDPSocket.new  
=\> #UDPSocket:0xb7d5adec  
irb(main):003:0\> s.connect Socket.gethostname, 12345  
=\> 0  
irb(main):004:0\> s.send “world”, 0  
=\> 5  
irb(main):005:0\> s.recvfrom(1500)  
=\> [“hello world”, [“AF\_INET”, 12345, “[example.com](http://example.com)”, “192.0.2.1”]]

There’s nothing to stop you sending out packets asynchronously on the  
same UDPSocket though, if you want to, and this can even be done in  
another thread.

So if the processing of one particular packet might take a variable  
amount of time, and you still want to receive and process other packets  
in the mean time, you could do something like this (untested):

…  
sock.bind(Socket.gethostname, 12345)  
loop do  
d1, d2 = sock.recvfrom  
Thread.new(d1, d2) do |packet, sender|  
… process ‘packet’  
… use sock.send to send the response to ‘sender’  
end  
end

Note the use of arguments in Thread.new(…) to pass copies of the local  
variables to the thread. If you used d1 and d2 directly, you’d find that  
they changed as soon as another packet was received in the main loop,  
which leads to some nasty bugs when packets arrive at a fast rate.

So it’s probably safer to write it like this:

Thread.new(\*sock.recvfrom) do |packet, sender|  
… etc  
end

Setting Thread.abort\_on\_exception = true is helpful when debugging this  
sort of code, otherwise when an exception occurs in the thread it just  
dies silently.

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![sai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sai](https://www.ruby-forum.com/u/sai)\
**Post date:** [February 27, 2009, 2:14pm UTC](https://www.ruby-forum.com/t/simple-daemon-which-can-speak-and-listen/161497/4 "2009-02-27T14:14:56Z")

</div>

On 26 Feb 2009, at 22:15, Sai Hl wrote:

> Hi all. According to you, what could be the more sexy way to write a  
> simple Ruby daemon (version 1.9 :)) which can speak and listen in the  
> same time on a socket (UDP in my case)?
> 
> Thanks,  
> Sai

Follow the link in my sig and you’ll find a couple of presentations  
with examples of all kinds of Ruby socket fun, covering both UDP and  
TCP. They also include lots of code which may or may not be fit for  
production purposes 🙂

Ellie

## Eleanor McHugh Games With Brains [http://slides.games-with-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![sai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@sai](https://www.ruby-forum.com/u/sai)\
**Post date:** [February 27, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/simple-daemon-which-can-speak-and-listen/161497/5 "2009-02-27T22:56:17Z")

</div>

Totaly awsome :o  
Thank you all!
