In article <m21yupjz2r.fsf / zip.local.thomases.com>,
  Dave Thomas <Dave / PragmaticProgrammer.com> wrote:
>
>Here's a trivial server.
> .............
>
I have not read Stevens's book, but I learned how to construct
sockaddr_in from chapter 14 of Beginning Linux programming, 2nd edition,
by Richard Stones and Neil Mathew.

Your example helped me a lot about how to write sockaddr in Ruby, which
is not covered by your book.  I am interested in socket programs, since
I would like to know to what extent Ruby can replace and simplify C.

Following your example I revised my server and client programs in an
AF_INET version corresponding to the example in page 480.

#serverIN.rb
require 'socket'
sock = Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0)
n = Socket.gethostname("localhost")
sockaddr = [Socket::AF_INET, 3456, n[3], 0, 0].pack("snA4NN")
sock.bind(sockaddr)
sock.listen(4)
while true
  s1 = sock.accept
  p s1[0].recvfrom(124)
  s1{0}.close
end

#clientIN.rb
require 'socket'
client = Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0)
n = Socket.gethostname("localhost")
sockaddr = {Socket::AF_INET, 3456,n[3], 0, 0}.pack("snA4NN")
client.connect(sockaddr)
client.send("hello", 0)
client.close

You can compare them with similar examples written by C in Stones and
Mathew's book.

One problem in the above programs is that they produced
["hello", "\002\000\004\005\177\000\000\001\344\022\025\200\200tH\200"]




Sent via Deja.com
http://www.deja.com/