Permalink
Browse files

listen

  • Loading branch information...
1 parent 19a27b6 commit ebf6808252ef3cb1cd740f2d98a3d78d21c669d1 @cocoa committed Jun 11, 2013
Showing with 34 additions and 0 deletions.
  1. +10 −0 bind.rb
  2. +24 −0 loopback-binding.rb
View
10 bind.rb
@@ -0,0 +1,10 @@
+require 'socket'
+
+# Create a new TCP IP4 Socket.
+socket = Socket.new :INET, :STREAM
+
+# Create a C struct to hold the address for listening.
+addr = Socket.pack_sockaddr_in(4481, '127.0.0.1')
+
+# Bind the socket to the address
+socket.bind(addr)
View
@@ -0,0 +1,24 @@
+require 'socket'
+
+# This socket will bind to the loopback interface and will
+# only be listening for clients from localhost.
+local_socket = Socket.new(:INET,:STREAM)
+local_addr = Socket.pack_sockaddr_in(4481, '127.0.0.1')
+local_socket.bind(local_addr)
+
+
+# This socket will bind to any of the known interfaces and
+# will be listening for any client that can route messages
+# to it.
+any_socket = Socket.new(:INET,:STREAM)
+any_addr = Socket.pack_sockaddr_in(4482, '0.0.0.0')
+any_socket.bind(any_addr)
+
+
+# This socket attempts to bind to an unkown interface
+# and raises Errno::EADDRNOTAVAIL.
+error_socket = Socket.new(:INET,:STREAM)
+error_addr = Socket.pack_sockaddr_in(4483, '1.2.3.4')
+error_socket.bind(error_addr)
+
+# ...loopback-binding.rb:22:in `bind': Can't assign requested address - bind(2) (Errno::EADDRNOTAVAIL)

0 comments on commit ebf6808

Please sign in to comment.