# Struggling with a socket problem

**URL:** <https://www.ruby-forum.com/t/struggling-with-a-socket-problem/130625>\
**Category:** Rails\
**Created:** [February 29, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/struggling-with-a-socket-problem/130625 "2008-02-29T22:47:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [February 29, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/struggling-with-a-socket-problem/130625/1 "2008-02-29T22:47:21Z")

</div>

Hi,  
Please can someone tell me if this is even possible, I’ve been trying to  
come up with various solutions and none of them seem to work.

I have a model which can send UDP packets, these need to be sent over a  
specific port. Users using my rails application will be able to call  
the model and send a UDP packet.

## However, I also need to have a script running that listens for UDP packets arriving on the same port. This script makes calls to the same models as the Rails application and hence I am running the script via script/runner. The script looks something like:

## #listener script $socket = UDPSocket.open $socket.bind("",1810) while true udp\_packet = $socket.recvfrom(512) udp = UDP.new(udp\_packet) #This is the same model as the rails app uses end

## As I said, the UDP model needs to be able to send UDP packets over the same socket as the script is listening on. i.e. the code looks like

## class UDP def send $socket.send(“message”, 0, ip, port) end end

Hence, I need the an instance of socket to be bound to a particular port  
and for it to be available to the Rails application AND the script  
running via script/runner.  
Is this even possible? I can’t think of a way to do it. I tried  
looking into marshaling but it is not possible to marshal a socket.
