V
Vladimir Fekete
Hi *,
I have following problem, I'm playing with socket class and I wanted to
test the example from
http://www.ruby-doc.org/stdlib/libdoc/socket/rdoc/index.html
e.g.:
#-------------------------------------------------------------------
require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )
client_fd, client_sockaddr = socket.sysaccept
client_socket = Socket.for_fd( client_fd )
puts "The client said, '#{client_socket.readline.chomp}'"
client_socket.puts "Hello from script one!"
socket.close
#-------------------------------------------------------------------
I changed the code slightly to :
#-------------------------------------------------------------------
require 'socket'
include Socket::Constants
$shutdown = false
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2201, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )
loop do
if (IO.select([socket],nil,nil,0.1)!=nil) then
client, client_sockaddr = socket.accept
puts "NEW CLIENT : " + client_sockaddr.inspect.to_s
Thread.start(client) do |a|
str = a.recv(100);
$shutdown = true if str=="shutdown"
a.close
puts str.to_s + " " + $shutdown.to_s
end
end
break if $shutdown==true
end
puts "SHutting down..."
socket.close
#--------------------------------------------------------------------
when I tested it it worked. But when I tried to run it again I've got error
message:
/t1.rb:10:in `bind': Address already in use - bind(2) (Errno::EADDRINUSE)
from ./t1.rb:10
I tried to find "related" bug/problem in google but withouth success. Could anybody
help me how to "unbind" socket ? Or explain me where I making mistake. I have
some experiences only with WinSock API and I don't remember any unbind function.
Many thanks!
Cheers,
V.
P.S. I'm running it on Debian Linux, Ruby version 1.8.
I have following problem, I'm playing with socket class and I wanted to
test the example from
http://www.ruby-doc.org/stdlib/libdoc/socket/rdoc/index.html
e.g.:
#-------------------------------------------------------------------
require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )
client_fd, client_sockaddr = socket.sysaccept
client_socket = Socket.for_fd( client_fd )
puts "The client said, '#{client_socket.readline.chomp}'"
client_socket.puts "Hello from script one!"
socket.close
#-------------------------------------------------------------------
I changed the code slightly to :
#-------------------------------------------------------------------
require 'socket'
include Socket::Constants
$shutdown = false
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2201, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )
loop do
if (IO.select([socket],nil,nil,0.1)!=nil) then
client, client_sockaddr = socket.accept
puts "NEW CLIENT : " + client_sockaddr.inspect.to_s
Thread.start(client) do |a|
str = a.recv(100);
$shutdown = true if str=="shutdown"
a.close
puts str.to_s + " " + $shutdown.to_s
end
end
break if $shutdown==true
end
puts "SHutting down..."
socket.close
#--------------------------------------------------------------------
when I tested it it worked. But when I tried to run it again I've got error
message:
/t1.rb:10:in `bind': Address already in use - bind(2) (Errno::EADDRINUSE)
from ./t1.rb:10
I tried to find "related" bug/problem in google but withouth success. Could anybody
help me how to "unbind" socket ? Or explain me where I making mistake. I have
some experiences only with WinSock API and I don't remember any unbind function.
Many thanks!
Cheers,
V.
P.S. I'm running it on Debian Linux, Ruby version 1.8.