Method: Socket#accept
- Defined in:
- socket.c
#accept ⇒ Array
Accepts an incoming connection returning an array containing a new Socket object and a string holding the struct sockaddr information about the caller.
Example
# In one script, start this first require ‘socket’ include Socket::Constants socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) sockaddr = Socket.pack_sockaddr_in( 2200, ‘localhost’ ) socket.bind( sockaddr ) socket.listen( 5 ) client, client_sockaddr = socket.accept puts “The client said, ‘#Socket.clientclient.readlineclient.readline.chomp’” client.puts “Hello from script one!” socket.close
# In another script, start this second require ‘socket’ include Socket::Constants socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) sockaddr = Socket.pack_sockaddr_in( 2200, ‘localhost’ ) socket.connect( sockaddr ) socket.puts “Hello from script 2.” puts “The server said, ‘#Socket.socketsocket.readlinesocket.readline.chomp’” socket.close
Unix-based Exceptions
On unix-based based systems the following system exceptions may be raised if the call to accept fails:
-
Errno::EAGAIN - O_NONBLOCK is set for the
socketfile descriptor and no connections are parent to be accepted -
Errno::EWOULDBLOCK - same as Errno::EAGAIN
-
Errno::EBADF - the
socketis not a valid file descriptor -
Errno::ECONNABORTED - a connection has been aborted
-
Errno::EFAULT - the socket’s internal address or address length parameter cannot be access or written
-
Errno::EINTR - the accept method was interrupted by a signal that was caught before a valid connection arrived
-
Errno::EINVAL - the
socketis not accepting connections -
Errno::EMFILE - OPEN_MAX file descriptors are currently open in the calling process
-
Errno::ENOBUFS - no buffer space is available
-
Errno::ENOMEM - there was insufficient memory available to complete the operation
-
Errno::ENOSR - there was insufficient STREAMS resources available to complete the operation
-
Errno::ENFILE - the maximum number of file descriptors in the system are already open
-
Errno::ENOTSOCK - the
socketdoes not refer to a socket -
Errno::EOPNOTSUPP - the socket type for the calling
socketdoes not support accept connections -
Errno::EPROTO - a protocol error has occurred
Windows Exceptions
On Windows systems the following system exceptions may be raised if the call to accept fails:
-
Errno::ECONNRESET - an incoming connection was indicated, but was terminated by the remote peer prior to accepting the connection
-
Errno::EFAULT - the socket’s internal address or address length parameter is too small or is not a valid part of the user space address
-
Errno::EINVAL - the listen method was not invoked prior to calling accept
-
Errno::EINPROGRESS - a blocking Windows Sockets 1.1 call is in progress or the service provider is still processing a callback function
-
Errno::EMFILE - the queue is not empty, upong etry to accept and there are no socket descriptors available
-
Errno::ENETDOWN - the network is down
-
Errno::ENOBUFS - no buffer space is available
-
Errno::ENOTSOCK -
socketis not a socket -
Errno::EOPNOTSUPP -
socketis not a type that supports connection-oriented service. -
Errno::EWOULDBLOCK -
socketis marked as nonblocking and no connections are present to be accepted
See
-
accept manual pages on unix-based systems
-
accept function in Microsoft’s Winsock functions reference
3039 3040 3041 |
# File 'socket.c', line 3039 static VALUE sock_accept(sock) VALUE sock; |