# TCP Sockets

**URL:** <https://www.ruby-forum.com/t/tcp-sockets/190001>\
**Category:** Rails\
**Created:** [June 11, 2010, 8:05am UTC](https://www.ruby-forum.com/t/tcp-sockets/190001 "2010-06-11T08:05:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 11, 2010, 8:05am UTC](https://www.ruby-forum.com/t/tcp-sockets/190001/1 "2010-06-11T08:05:28Z")

</div>

I am trying to interface to an API. I am instructed to use the server  
and port information provided to setup a TCP socket. I did that like  
this:

```
 require 'socket'
 my_socket=TCPSocket::open("ip","port")

```

Where “ip” and “port” are the provided ip and port of the server  
respectively.

I am told that once the connection is established I will receive a  
greeting from the server. I was expecting to find that greeting in  
my\_socket. Instead the contents of my\_socket looks like this:

#TCPSocket:0x9961c80

Can anyone please tell me what I am doing wrong and what I need to do  
to get at the greeting? Obviously I am very green at this. Thanks  
for any input.

```
         ... doug
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 11, 2010, 3:04pm UTC](https://www.ruby-forum.com/t/tcp-sockets/190001/2 "2010-06-11T15:04:02Z")

</div>

On Fri, Jun 11, 2010 at 3:04 AM, doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am told that once the connection is established I will receive a  
> greeting from the server. I was expecting to find that greeting in  
> my\_socket. Instead the contents of my\_socket looks like this:
> 
> #TCPSocket:0x9961c80
> 
> Can anyone please tell me what I am doing wrong and what I need to do  
> to get at the greeting? Obviously I am very green at this. Thanks  
> for any input.

You have to read from your socket.  
Can you post some more of the code?  
It looks like you’re doing  
puts my\_socket  
there.

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 11, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/tcp-sockets/190001/3 "2010-06-11T17:02:23Z")

</div>

> You have to read from your socket.

Ahh! I get it. (I said that I was green at this.) I now have it  
working except that it is REALLY slow.

> Can you post some more of the code?

Here is my code that is now working:

```
require 'socket'
socket=TCPSocket::open("ip","port")
out=File.open('/tmp/debug1','w')
while line=socket.gets
  out.puts(line.chop)
end
socket.close
out.close

```

Do you have any idea why this would be very slow? Thanks.

```
       ... doug
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 11, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/tcp-sockets/190001/4 "2010-06-11T19:17:03Z")

</div>

On Fri, Jun 11, 2010 at 12:00 PM, doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> socket=TCPSocket::open(“ip”,“port”)  
> out=File.open(‘/tmp/debug1’,‘w’)  
> while line=socket.gets  
> out.puts(line.chop)  
> end  
> socket.close  
> out.close
> 
> Do you have any idea why this would be very slow? Thanks.

It’s hard to say with no information, but it can be a slow connection.  
I know there’s a way to get the data asynchronously. I’ve even used it  
but a while ago, I should take a look at that code to point it to you,  
but this will avoid the while loop.

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 12, 2010, 2:02am UTC](https://www.ruby-forum.com/t/tcp-sockets/190001/5 "2010-06-12T02:02:42Z")

</div>

I think that I have the speed issue resolved. It appears to be an  
anomaly that is associated only with their sandbox server. The live  
server seems to work just fine. I can live with that. Thanks for  
your help.

```
      ... doug
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 11, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/tcp-sockets/190001/6 "2010-06-11T19:19:58Z")

</div>

On Fri, Jun 11, 2010 at 2:15 PM, Leonardo M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > require ‘socket’  
> > It’s hard to say with no information, but it can be a slow connection.  
> > I know there’s a way to get the data asynchronously. I’ve even used it  
> > but a while ago, I should take a look at that code to point it to you,  
> > but this will avoid the while loop.

You can check the code on one of my github repositories.  
It is an unfinished project but that part works.

> <https://github.com/kandalf/fluxy/blob/master/lib/fluxy_receiver.rb>

Take a look at the run method there.  
That’s for a server socket but might help you otu with this.

Hope it helps.

–  
Leonardo M…  
There’s no place like ~
