winsock problem?

X

Xiangrong Fang

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...
 
B

Brian Candler

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.

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.
 
X

Xiangrong Fang

Hi Brian,

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.
 
T

ts

X> 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
 
T

ts

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

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


Guy Decoux
 
T

ts

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

The other program use SO_REUSEADDR, you have the explanation in

http://msdn.microsoft.com/library/d...winsock/winsock/using_so_exclusiveaddruse.asp


Guy Decoux
 
X

Xiangrong Fang

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
 
X

Xiangrong Fang

X" == Xiangrong Fang said:
X> I tried the following, but unfortunately does not work:

it do the same ?

Similar, this is a test program.
X> require 'socket'
X> begin
X> server = Socket.new(Socket::pF_INET, Socket::SOCK_STREAM, 0)
X> 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.
X> n = Socket.gethostbyname("localhost")
X> 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.
 
R

Rasputin

* Brian McCallister said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top