# Reassignement TCPSocket::new

**URL:** <https://www.ruby-forum.com/t/reassignement-tcpsocket-new/171563>\
**Category:** Rails\
**Created:** [July 5, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/reassignement-tcpsocket-new/171563 "2009-07-05T14:59:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 5, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/reassignement-tcpsocket-new/171563/1 "2009-07-05T14:59:44Z")

</div>

in my model, I have:

attr\_accessor :socket

in my controller I have

def toServer(s)  
begin  
if(@socket == nil)  
@socket = TCPSocket::new(@channel.serverip, @channel.port)  
end  
@socket.send(s,0)  
rescue  
return  
end  
end

I have a dropdown list in the view that lets the user select a  
different channel, thus, the socket is created in the view based on  
the ip and port of the selected channel.

Usually, however, the channel info will remain the same. So I am  
wondering how I can make the socket persist from one POST/GET  
operation to the other, unless the channel has changed.

And my second question is, if I someone does tell me how I can make it  
persist, do I run the risk of some sort of memory leaks here with  
this? I wonder because I have a solitary TCPSocket object which would  
then be reassigned – does the un-assigned old reference that socket  
contained go automatically out of scope and get garbage collected?  
Thanks, Janna B  
repeatedly in my controller, naturally,

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 5, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/reassignement-tcpsocket-new/171563/2 "2009-07-05T17:21:42Z")

</div>

On Jul 5, 8:59 am, JannaB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> contained go automatically out of scope and get garbage collected?  
> Thanks, Janna B

If you reassign a variable then its old contents will eventually be  
garbage collected (assuming no other references to it).  
You are likely to run into other problems though here: have you  
thought about concurrent users using the app at the same time, or what  
happens when you have multiple mongrels / passenger instances ?

Lastly it looks like @socket is an instance variable of your  
controller, since every request is served by a new controller instance  
@socket will always be nil.

Fred

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 5, 2009, 8:01pm UTC](https://www.ruby-forum.com/t/reassignement-tcpsocket-new/171563/3 "2009-07-05T20:01:52Z")

</div>

Thanks Fred,

Yes, multiple users are not a problem as part of what is passed  
through the socket is the reference to the user. I have numerous  
channels that can be run, however, each with a different port  
assignement, by various users. Thus, it would seem I want to create a  
hash of

channels =\> sockets

but how can I make that be persistent?

-Janna B

On Jul 5, 11:11 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 6, 2009, 2:54pm UTC](https://www.ruby-forum.com/t/reassignement-tcpsocket-new/171563/4 "2009-07-06T14:54:00Z")

</div>

Then would the following be a problem in a controller if concurrent  
users use the app at the same time ?

```
  socket = TCPSocket::new(@channel.serverip,

```

@channel.port)  
socket.send(s+"\n",0)  
socket.close

(multiple servers I am not concerned about – one glassfish gem  
instance roars with all the horsepower this particular app ever needs  
– but what WOULD happen if I ran this under multiple mongrel  
instances? Can’t the same port be shared by multiple instances?)

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 6, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/reassignement-tcpsocket-new/171563/5 "2009-07-06T18:15:00Z")

</div>

On Jul 6, 1:53 pm, JannaB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Then would the following be a problem in a controller if concurrent  
> users use the app at the same time ?
> 
> ```
> socket = TCPSocket::new(@channel.serverip,
> 
> ```
> 
> @channel.port)  
> socket.send(s+“\n”,0)  
> socket.close

> (multiple servers I am not concerned about – one glassfish gem  
> instance roars with all the horsepower this particular app ever needs  
> – but what WOULD happen if I ran this under multiple mongrel  
> instances? Can’t the same port be shared by multiple instances?)

The various instances would just be completely unaware that another  
instance already had a socket.

if you had multiple instances inside a single glassfish process and if  
you were storing socket in somewhere that persisted across requests  
then it could potentially go wrong. No one’s going to be very happy if  
two different threads try to write to the same socket at the same  
time.

Fred
