# Weird thing with sockets

**URL:** <https://www.ruby-forum.com/t/weird-thing-with-sockets/78170>\
**Category:** Ruby\
**Created:** [December 3, 2006, 2:01pm UTC](https://www.ruby-forum.com/t/weird-thing-with-sockets/78170 "2006-12-03T14:01:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gemberkoekje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f14d63/32.png) [@gemberkoekje](https://www.ruby-forum.com/u/gemberkoekje)\
**Post date:** [December 3, 2006, 2:01pm UTC](https://www.ruby-forum.com/t/weird-thing-with-sockets/78170/1 "2006-12-03T14:01:05Z")

</div>

First off - i’m a n00b at ruby. This doesn’t mean i’m a noob at  
programming, but in case i’m about to ask a really stupid question,  
sorry in advance 😛

With that out of the way, i’m trying to connect to an IP-address  
(Specifically an ED2K server) and get information out of it. At first,  
simply nothing happened. When i tried to build in a control mechanism,  
something weird happened: It told me i was trying to connect to myself!

* * *

Code:

require ‘socket’  
t = TCPSocket.new(‘62.241.53.16’, ‘4242’)  
print "Connecting to " + t.addr[3].to\_s + “:” + t.addr[1].to\_s  
t.recv(100)  
print “Closing Connection”  
t.close

Result:

## Connecting to 192.168.2.6:4182

So am i misinterpreting the addr value, or is my program (or possibly  
the server i’m trying to connect to) doing some weird things?

Please don’t start with “I think you are going in over your head”, i  
know i am, but for me i think this is the best way to learn Ruby, inside  
out…

Thanks in advance!

Gemberkoekje

---

<div class="post-metadata">

**Author:** ![gemberkoekje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f14d63/32.png) [@gemberkoekje](https://www.ruby-forum.com/u/gemberkoekje)\
**Post date:** [December 3, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/weird-thing-with-sockets/78170/2 "2006-12-03T14:12:39Z")

</div>

IIRC:

For the peer’s address, you need  
t.peeraddr

t.addr is the address of _this_ socket “here” (in the system it was  
created),  
t.peeraddr is the address of the peer socket “there” (the one created to  
talk to _this one_)

Pedro

---

<div class="post-metadata">

**Author:** ![gemberkoekje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f14d63/32.png) [@gemberkoekje](https://www.ruby-forum.com/u/gemberkoekje)\
**Post date:** [December 3, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/weird-thing-with-sockets/78170/3 "2006-12-03T14:22:56Z")

</div>

Thank you, i was afraid it was something silly like that.

I’ll try some and another things myself, see if i can make another  
server ‘talk’, cause i’m not getting anything yet out of this one.

---

<div class="post-metadata">

**Author:** ![gemberkoekje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f14d63/32.png) [@gemberkoekje](https://www.ruby-forum.com/u/gemberkoekje)\
**Post date:** [December 3, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/weird-thing-with-sockets/78170/4 "2006-12-03T14:47:23Z")

</div>

Thanks to both of you, i know much more already now…

And i actually got a reply from the server, after sending a space!

[“AF\_INET”, 4242, “62.241.53.16”, “62.241.53.16”]  
“\343!\000\000\0008\036\000WARNING : This server is full.”  
Closing Connection  
Press ENTER to close the window…

Alright, not really the kind of reply i was hoping for, but hey 😉

Thanks to all involved, and more hints are always appreciated (Although  
finding out myself has it’s merits too :P)

---

<div class="post-metadata">

**Author:** ![gemberkoekje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f14d63/32.png) [@gemberkoekje](https://www.ruby-forum.com/u/gemberkoekje)\
**Post date:** [December 3, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/weird-thing-with-sockets/78170/5 "2006-12-03T14:36:40Z")

</div>

Arie Hofland wrote:

> ## Code: Connecting to 192.168.2.6:4182
> 
> So am i misinterpreting the addr value, or is my program (or possibly  
> the server i’m trying to connect to) doing some weird things?

You are misinterpreting: addr is your own address. After all you _know_  
the other address already - otherwise you would not have been able to  
connect.

Another small hint: preferably use the block form whenever possible:

require ‘socket’

TCPSocket.open(‘62.241.53.16’, 4242) do |t|  
p t.addr  
p t.recv(100)  
puts “Closing Connection”  
end

You can use t.getpeername to obtain a struct sockaddr which you need to  
unpack to get at the family, address and port of the remote machine:

irb(main):045:0\> t=TCPSocket.open(‘62.241.53.16’, 4242)  
=\> #TCPSocket:0x3883a8  
irb(main):046:0\> t.getpeername  
=\> “\002\000\020\222\>\3615\020\000\000\000\000\000\000\000\000”  
irb(main):047:0\> t.getpeername.unpack “S”  
=\> [2]  
irb(main):048:0\> t.getpeername.unpack “nnC4”  
=\> [512, 4242, 62, 241, 53, 16]

irb(main):064:0\> fam, port, \*addr = t.getpeername.unpack “nnC4”  
=\> [512, 4242, 62, 241, 53, 16]  
irb(main):065:0\> fam  
=\> 512  
irb(main):066:0\> port  
=\> 4242  
irb(main):067:0\> addr  
=\> [62, 241, 53, 16]  
irb(main):070:0\> addr.join ‘.’  
=\> “62.241.53.16”

HTH

Kind regards

```
robert
```
