TCP Socket Server

I am working on learning ruby and I am presently going over TCPSocket using the required 'socket'.

I have successfully opened a server, and gotten a client to connect, but I am still running into some issues. The issue I am running into is if I need to drop the server for whatever reason, the TCP Socket gets put on hold, so that when I try to reopen the server I get:

"
server.rb:18:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
  from server.rb:18:in `open'
  from server.rb:18:in `<main>'
imac:Server donvalverde$ ruby server.rb
"

My question is, is there a way to securely and safely close a TCPSocket Server?

Here is my present server.rb
"
#!/usr/bin/ruby

=begin

···

-------------
Created By: Donald R. Valverde(AKA Cavious)
server.rb
Version: 1.0-PRE-ALPHA
=end

require 'socket'

def connection_time()
  connect_time = (Time.now)
  return connect_time
end

server = TCPServer.open(35504) #default localhost

loop {
  Thread.start(server.accept) do |client|

    puts( "#{connection_time}::#{client.peeraddr[2]} has connected to the server.")
    client.puts("#{connection_time}::You've connected to the server")
    puts( "#{connection_time}::#{client.peeraddr[2]} has disconnected from the server.")
    client.close
  end
}
"

and my client.rb:

"
require 'socket'

def connection_time()
  connect_time = (Time.now)
  return connect_time
end

hostname = 'localhost'
port = 35504

s = TCPSocket.open(hostname, port)
puts "#{connection_time()} ::Connected to Server @ #{hostname}:#{port}."
while inbound_text = s.gets
  puts inbound_text.chop
end
puts "#{connection_time()} ::Disconnected from Server @ #{hostname}:#{port}."

s.close
"

Any assistance would be appreciated,

Cavious

PS; I apologize for any lack of fortmatting on my post, I haven't had the time to look at the BBcode or html syntax offers.

--
Posted via http://www.ruby-forum.com/.

You should look into socket options SO_LINGER and SO_REUSEADDR. See
here for explanations:

You find those constants in Socket::Constants. You can set them with

Kind regards

robert

···

On Mon, Jan 13, 2014 at 11:55 PM, Donald Valverde <lists@ruby-forum.com> wrote:

I am working on learning ruby and I am presently going over TCPSocket using the required 'socket'.

I have successfully opened a server, and gotten a client to connect, but I am still running into some issues. The issue I am running into is if I need to drop the server for whatever reason, the TCP Socket gets put on hold, so that when I try to reopen the server I get:

"
server.rb:18:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
  from server.rb:18:in `open'
  from server.rb:18:in `<main>'
imac:Server donvalverde$ ruby server.rb
"

My question is, is there a way to securely and safely close a TCPSocket Server?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

I need to drop the server for whatever reason, the TCP Socket gets put
on hold, so that when I try to reopen the server I get:

@socket_server.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR,
true)

···

--
Posted via http://www.ruby-forum.com/\.