Permalink
Browse files

accept

  • Loading branch information...
1 parent ebf6808 commit 34f503a148af16ce93d862d5ac47b4f6a8c3e408 @cocoa committed Jun 12, 2013
Showing with 39 additions and 0 deletions.
  1. +28 −0 accept-connection-class.rb
  2. +11 −0 listen.rb
@@ -0,0 +1,28 @@
+require 'socket'
+
+# Create the server socket
+# create
+server = Socket.new :INET, :STREAM
+addr = Socket.pack_sockaddr_in(4481, '0.0.0.0')
+# bind
+server.bind(addr)
+# listen
+server.listen(Socket::SOMAXCONN)
+
+# Accept a new connection
+connection, _ = server.accept
+
+print 'Connection class: '
+p connection.class
+
+print 'Server fileno: '
+p server.fileno
+
+print 'Connection fileno: '
+p connection.fileno
+
+print 'Local address: '
+p connection.local_address
+
+print 'Remote address: '
+p connection.remote_address
View
@@ -0,0 +1,11 @@
+require 'socket'
+
+# Create a socket and bind it to port 4481.
+socket = Socket.new(:INET, :STREAM)
+addr = Socket.pack_sockaddr_in(4481, '0.0.0.0')
+socket.bind(addr)
+
+# Tell it to listen for incoming connections
+socket.listen(5)
+
+puts Socket::SOMAXCONN

0 comments on commit 34f503a

Please sign in to comment.