Working With TCP Sockets by Jesse Storimer

Working With TCP Sockets by Jesse Storimer

Author:Jesse Storimer
Language: eng
Format: epub, pdf
Publisher: Jesse Storimer
Published: 2012-01-17T16:00:00+00:00


Non-blocking Connect

Think you can guess what the connect_nonblock method does by now? Then you're in for a surprise! connect_nonblock behaves a bit differently than the other non-blocking IO methods.

Whereas the other methods either complete their operation or raise an appropriate exception, connect_nonblock leaves its operation in progress and raises an exception.

If connect_nonblock cannot make an immediate connection to the remote host, then it actually lets the operation continue in the background and raises Errno::EINPROGRESS to notify us that the operation is still in progress. In the next chapter we'll see how we can be notified when this background operation completes. For now, a quick example:

# ./code/snippets/connect_nonblock.rbrequire 'socket' socket = Socket.new(:INET, :STREAM) remote_addr = Socket.pack_sockaddr_in(80, 'google.com') begin # Initiate a nonblocking connection to google.com on port 80. socket.connect_nonblock(remote_addr) rescue Errno::EINPROGRESS # Operation is in progress. rescue Errno::EALREADY # A previous nonblocking connect is already in progress. rescue Errno::ECONNREFUSED # The remote host refused our connect. end



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.