Permalink
Browse files

Low-level socket connection

  • Loading branch information...
1 parent 8bbf7f9 commit bbf1bec6ba96f93cfb86c0f06e20f7cadd8f7feb @weppos weppos committed Jan 7, 2010
Showing with 9 additions and 6 deletions.
  1. +0 −1 lib/whois/client.rb
  2. +9 −5 lib/whois/server/adapters/base.rb
View
@@ -14,7 +14,6 @@
#++
-require 'socket'
require 'timeout'
@@ -16,13 +16,15 @@
require 'whois/answer/part'
require 'whois/answer'
+require 'socket'
module Whois
class Server
module Adapters
class Base
+ include Socket::Constants
# Default Whois request port.
DEFAULT_WHOIS_PORT = 43
@@ -67,7 +69,7 @@ def with_buffer(&block)
# @buffer = []
# result
end
-
+
# Store an answer part in <tt>@buffer</tt>.
def append_to_buffer(response, host)
@buffer << ::Whois::Answer::Part.new(response, host)
@@ -80,11 +82,13 @@ def query_the_socket(qstring, host, port = nil)
private
def ask_the_socket(qstring, host, port)
- client = TCPSocket.open(host, port)
- client.write("#{qstring}\r\n") # I could use put(foo) and forget the \n
- client.read # but write/read is more symmetric than puts/read
+ socket = Socket.new(AF_INET, SOCK_STREAM, IPPROTO_IP)
+ sockaddr = Socket.pack_sockaddr_in(port, host)
+ socket.connect(sockaddr)
+ socket.write("#{qstring}\r\n") # I could use put(foo) and forget the \n
+ socket.read # but write/read is more symmetric than puts/read
ensure # and I really want to use read instead of gets.
- client.close if client # If != client something went wrong.
+ socket.close if socket # If != socket something went wrong.
end
end

0 comments on commit bbf1bec

Please sign in to comment.