The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
The secret arguments of TCPSocket

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
The secret arguments of TCPSocket Posted: Sep 26, 2006 12:04 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Daniel Berger.
Original Post: The secret arguments of TCPSocket
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
Did you know that TCPSocket.new actually accepts up to four arguments? Well, it does. The third and fourth arguments are a local host and port, respectively. I happened across it while hunting down a bug in http.rb reported on the Mongrel mailing list, and was looking at the socket.c source.

It turns out that Matz did mention this on the mailing list...once....in 2002. I must have missed the memo.

Anyway, it lets you bind to a remote host and port from a local host and port. For example:
require 'socket'

socket = TCPSocket.new('localhost', 23, 'localhost', 8885)
sleep 20
socket.close

With that running, run a quick 'netstat -a | grep 8885'. You'll see something like this:
localhost.8885       localhost.telnet     49152      0 49152      0 ESTABLISHED
localhost.telnet     localhost.8885       49152      0 49152      0 ESTABLISHED

Interesting, eh?

I did notice what I think is a bug, though. The remote connection doesn't seem to die on time - it goes into a TIME_WAIT, and I'm pretty sure it's the OS that cleans it up eventually. If you try to connect again while it's in this state, it gives you a Errno::EAFNOSUPPORT, which isn't right.

I think Nobu actually proposed a patch for this in 2002 (same thread) but I'm not sure that it was ever applied, probably because no one knew that you could pass four arguments, so (almost) no one complained.

Read: The secret arguments of TCPSocket

Topic: Ruby internals: a self-study guide to the sources Previous Topic   Next Topic Topic: Using GDB and the Ruby C API to debug a running Ruby process

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use