# Establishing new TCP Socket connection

**URL:** https://rubytalk.org/t/establishing-new-tcp-socket-connection/19359
**Category:** ruby-talk
**Created:** [5 July 2005 21:03 UTC](https://rubytalk.org/t/establishing-new-tcp-socket-connection/19359 "2005-07-05T21:03:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)
#### Post date: [5 July 2005 21:03 UTC](https://rubytalk.org/t/establishing-new-tcp-socket-connection/19359/1 "2005-07-05T21:03:11Z")

</div>

Hi,

If I want to establish a new TCP connection every time I need to  
access data (the server doesn't want to keep old connetions around),  
is there any reason why I can't do something like:

def send\_message\_and\_recv\_response  
&nbsp;&nbsp;server = TCPSocket.new @server\_ip, @server\_port  
&nbsp;&nbsp;server.puts "the message"  
&nbsp;&nbsp;result = server.recv  
end

---

<div class="post-metadata">

### Author: ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)
#### Post date: [5 July 2005 23:39 UTC](https://rubytalk.org/t/establishing-new-tcp-socket-connection/19359/2 "2005-07-05T23:39:55Z")

</div>

No response needed on this... it all seems to work ok.

> **···**
>
> On 7/5/05, Joe Van Dyk \<joevandyk@gmail.com\> wrote:
> 
> > Hi,
> > 
> > If I want to establish a new TCP connection every time I need to  
> > access data (the server doesn't want to keep old connetions around),  
> > is there any reason why I can't do something like:
> > 
> > def send\_message\_and\_recv\_response  
> > &nbsp;&nbsp;server = TCPSocket.new @server\_ip, @server\_port  
> > &nbsp;&nbsp;server.puts "the message"  
> > &nbsp;&nbsp;result = server.recv  
> > end

---

<div class="post-metadata">

### Author: ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)
#### Post date: [5 July 2005 23:49 UTC](https://rubytalk.org/t/establishing-new-tcp-socket-connection/19359/3 "2005-07-05T23:49:07Z")

</div>

You could be more friendly to the other end by adding an explicit close:

def do\_the\_stuff  
&nbsp;&nbsp;&nbsp;sock = TCPSocket.new @ip, @port  
&nbsp;&nbsp;&nbsp;sock.puts "message"  
&nbsp;&nbsp;&nbsp;return sock.recv  
ensure  
&nbsp;&nbsp;&nbsp;sock.close unless sock.nil?  
end

> **···**
>
> On 05 Jul 2005, at 14:03, Joe Van Dyk wrote:
> 
> > If I want to establish a new TCP connection every time I need to  
> > access data (the server doesn't want to keep old connetions around),  
> > is there any reason why I can't do something like:
> > 
> > def send\_message\_and\_recv\_response  
> > &nbsp;&nbsp;server = TCPSocket.new @server\_ip, @server\_port  
> > &nbsp;&nbsp;server.puts "the message"  
> > &nbsp;&nbsp;result = server.recv  
> > end
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://segment7.net](http://segment7.net)  
> FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

---

<div class="post-metadata">

### Author: ![Adam\_P\_Jenkins1](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Adam\_P\_Jenkins1](https://rubytalk.org/u/Adam_P_Jenkins1)
#### Post date: [6 July 2005 00:10 UTC](https://rubytalk.org/t/establishing-new-tcp-socket-connection/19359/4 "2005-07-06T00:10:46Z")

</div>

Joe Van Dyk wrote:

> Hi,
> 
> If I want to establish a new TCP connection every time I need to  
> access data (the server doesn't want to keep old connetions around),  
> is there any reason why I can't do something like:
> 
> def send\_message\_and\_recv\_response  
> &nbsp;&nbsp;server = TCPSocket.new @server\_ip, @server\_port  
> &nbsp;&nbsp;server.puts "the message"  
> &nbsp;&nbsp;result = server.recv  
> end

Sure, except you should explicitly close the connection as well at the end of your block rather than letting the socket object's finalizer do it whenever the GC gets around to calling it. So, something like:

def send\_message\_and\_recv\_response  
&nbsp;&nbsp;&nbsp;server = nil  
&nbsp;&nbsp;&nbsp;server = TCPSocket.new @server\_ip, @server\_port  
&nbsp;&nbsp;&nbsp;server.puts "the message"  
&nbsp;&nbsp;&nbsp;result = server.recv  
ensure  
&nbsp;&nbsp;&nbsp;server.close if server  
end
