Winsock problem?

Hi,

I wrote a socket program using ruby under windows, the main loop of this
program is:

begin
server = TCPServer.new(‘127.0.0.1’, port)
rescue Exception => e
puts e.message
exit
end
while session = server.accept do
Thread.new do
Client.new(session).run
end
end

It runs smoothly. However I found a strange problem. If I run another
program (not written in Ruby) using the same port, that program will
take control of the port, and my program is “over-ruled”! However, if
the other program runs first, and then I run my ruby one, the ruby
program print out error message that it can’t bind the port.

I don’t know why ruby’s socket is so polite? Wondering…

···


Xiangrong Fang xrfang@hotmail.com

Does your other program also bind to localhost, or does it bind to 0.0.0.0
(INADDR_ANY, meaning ‘all interfaces’)?

Maybe Windows is broken in allowing program A to bind to 127.0.0.1, then
when program B binds to 0.0.0.0 it takes over 127.0.0.1 as well. Just a
guess.

But try changing your Ruby program to:
server = TCPServer.new(‘0.0.0.0’, port)

Regards,

Brian.

···

On Mon, Jul 28, 2003 at 08:22:49PM +0900, Xiangrong Fang wrote:

begin
server = TCPServer.new(‘127.0.0.1’, port)
rescue Exception => e
puts e.message
exit
end
while session = server.accept do
Thread.new do
Client.new(session).run
end
end

It runs smoothly. However I found a strange problem. If I run another
program (not written in Ruby) using the same port, that program will
take control of the port, and my program is “over-ruled”! However, if
the other program runs first, and then I run my ruby one, the ruby
program print out error message that it can’t bind the port.

It runs smoothly. However I found a strange problem. If I run another
program (not written in Ruby) using the same port, that program will
take control of the port, and my program is "over-ruled"! However, if
the other program runs first, and then I run my ruby one, the ruby
program print out error message that it can't bind the port.

The other program use SO_REUSEADDR, you have the explanation in

Guy Decoux

Hi Brian,

···

On Mon, 28 Jul 2003 20:32:51 +0900 Brian Candler B.Candler@pobox.com wrote:

Maybe Windows is broken in allowing program A to bind to 127.0.0.1, then
when program B binds to 0.0.0.0 it takes over 127.0.0.1 as well. Just a
guess.

But try changing your Ruby program to:
server = TCPServer.new(‘0.0.0.0’, port)

Both the ruby program and the other program listen ONLY on 127.0.0.1.

Thanks.

Both the ruby program and the other program listen ONLY on 127.0.0.1.

Well, if I'm right, windows has an exclusive and non-exclusive socket
binding mode (SO_EXCLUSIVEADDRUSE)

Guy Decoux

Hi,

How can I use the binding mode in Ruby? I use TCPServer. which seems is
a very high level wrapper. I also tried to look for TCPServer.rb, but I
can’t find it. Seems that the TCPServer class is a build in module of
Ruby intepretor.

Thanks.
Shannon

···

On Mon, 28 Jul 2003 21:48:21 +0900 ts decoux@moulon.inra.fr wrote:

Both the ruby program and the other program listen ONLY on 127.0.0.1.

Well, if I’m right, windows has an exclusive and non-exclusive socket
binding mode (SO_EXCLUSIVEADDRUSE)

Guy Decoux


Xiangrong Fang xrfang@hotmail.com

How can I use the binding mode in Ruby? I use TCPServer. which seems is
a very high level wrapper. I also tried to look for TCPServer.rb, but I
can't find it. Seems that the TCPServer class is a build in module of
Ruby intepretor.

a call to setsockopt() before the call to bind()

Guy Decoux

I tried the following, but unfortunately does not work:

require ‘socket’
begin
server = Socket.new(Socket::PF_INET, Socket::SOCK_STREAM, 0)
server.setsockopt(Socket::SOL_SOCKET, -5, 1) #Socket::SO_EXCLUSIVEADDRUSE
n = Socket.gethostbyname(“localhost”)
sockaddr = [Socket::AF_INET, 3456, n[3], 0, 0].pack(“snA4NN”)
server.bind(sockaddr)
server.listen(0)
rescue Exception => e
puts e.message
puts e.backtrace.join(“\r\n”)
exit
end
while session = server.accept do
session.print “connected”
session.close
end

···

On Mon, 28 Jul 2003 22:01:56 +0900 ts decoux@moulon.inra.fr wrote:

How can I use the binding mode in Ruby? I use TCPServer. which seems is
a very high level wrapper. I also tried to look for TCPServer.rb, but I
can’t find it. Seems that the TCPServer class is a build in module of
Ruby intepretor.

a call to setsockopt() before the call to bind()

Guy Decoux


Xiangrong Fang xrfang@hotmail.com

I tried the following, but unfortunately does not work:

it do the same ?

require 'socket'
begin
  server = Socket.new(Socket::PF_INET, Socket::SOCK_STREAM, 0)
  server.setsockopt(Socket::SOL_SOCKET, -5, 1) #Socket::SO_EXCLUSIVEADDRUSE

-5 ? Microsoft is very strange ...

  n = Socket.gethostbyname("localhost")
  sockaddr = [Socket::AF_INET, 3456, n[3], 0, 0].pack("snA4NN")

ruby has Socket::sockaddr_in, ::pack_sockaddr_in, ::unpack_sockaddr_in

Guy Decoux

I tried the following, but unfortunately does not work:

it do the same ?

Similar, this is a test program.

require ‘socket’
begin
server = Socket.new(Socket::PF_INET, Socket::SOCK_STREAM, 0)
server.setsockopt(Socket::SOL_SOCKET, -5, 1) #Socket::SO_EXCLUSIVEADDRUSE

-5 ? Microsoft is very strange …

SO_REUSEADDR = 4 and SO_EXCLUSIVEADDRUSE = ! SO_REUSEADDR, but it is not
defined in ruby, so I have to use number.

n = Socket.gethostbyname(“localhost”)
sockaddr = [Socket::AF_INET, 3456, n[3], 0, 0].pack(“snA4NN”)

ruby has Socket::sockaddr_in, ::pack_sockaddr_in, ::unpack_sockaddr_in

Does this only available in v1.8? I did not see it in the PickAxe.

Thanks.

···


Xiangrong Fang xrfang@hotmail.com

I am looking for a webapp framework that nicely seperates out View
control from Command control - ie something that allows uri’s like

http://example.com/do/some-command/go/some-
view?propertyone=a&propertytwo=b

such that the control over selection, property population, and
execution of a command is handled by one logical controller, and
forwarding to a view renderer by another, and both are keyable from the
URI (such as one like the above). I have done this in Java via
servlets, but as I want it for a personal project - I would prefer to
find a Ruby solution, and before I make one… best to make sure I am
not duplicating effort.

The command and view controllers are metadata driven (config file or
script (really a script I guess, this is a scripting language after
all, no recompilation needed =)

Anyone know of anything that already does this in the Ruby world? i
hate to be implementing yet-another-web-application-framework.

-Brian

Does this only available in v1.8?

yes,

Guy Decoux

I am looking for a webapp framework that nicely seperates out View
control from Command control - ie something that allows uri’s like

http://example.com/do/some-command/go/some-
view?propertyone=a&propertytwo=b

such that the control over selection, property population, and
execution of a command is handled by one logical controller, and
forwarding to a view renderer by another, and both are keyable from the
URI (such as one like the above).

Like Struts, you mean? How about:
http://raa.ruby-lang.org/list.rhtml?name=ruby-waf

···


Education is the process of casting false pearls before real swine.
– Irsin Edman
Rasputin :: Jack of All Trades - Master of Nuns

Except that Struts won’t do what I want - it keys the View control off
of results of Action execution instead of the URI, hacking it pull
effectively two actions (in its parlance) out of the URI isn’t worth
the effort, so I use Cocoon or a home rolled servlet to do this in the
Java world.

Will take a look at ruby-waf, but it seems to be advertised as a Struts
clone =/

-Brian

···

On Tuesday, July 29, 2003, at 05:31 AM, Rasputin wrote:

Like Struts, you mean?