In Files

  • socket/socket.c

TCPServer

Public Class Methods

new(p1, p2 = v2) click to toggle source
 
               static VALUE
tcp_svr_init(argc, argv, sock)
    int argc;
    VALUE *argv;
    VALUE sock;
{
    VALUE arg1, arg2;

    if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2)
        return init_inetsock(sock, arg1, arg2, Qnil, Qnil, INET_SERVER);
    else
        return init_inetsock(sock, Qnil, arg1, Qnil, Qnil, INET_SERVER);
}
            

Public Instance Methods

accept() click to toggle source
 
               static VALUE
tcp_accept(sock)
    VALUE sock;
{
    OpenFile *fptr;
    struct sockaddr_storage from;
    socklen_t fromlen;

    GetOpenFile(sock, fptr);
    fromlen = sizeof(from);
    return s_accept(rb_cTCPSocket, fileno(fptr->f),
                    (struct sockaddr*)&from, &fromlen);
}
            
accept_nonblock => tcpsocket click to toggle source

Accepts an incoming connection using accept(2) after O_NONBLOCK is set for the underlying file descriptor. It returns an accepted TCPSocket for the incoming connection.

Example

require 'socket'
serv = TCPServer.new(2202)
begin
  sock = serv.accept_nonblock
rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
  IO.select([serv])
  retry
end
# sock is an accepted socket.

Refer to Socket#accept for the exceptions that may be thrown if the call to #accept_nonblock fails.

#accept_nonblock may raise any error corresponding to accept(2) failure, including Errno::EAGAIN.

See

 
               static VALUE
tcp_accept_nonblock(sock)
    VALUE sock;
{
    OpenFile *fptr;
    struct sockaddr_storage from;
    socklen_t fromlen;

    GetOpenFile(sock, fptr);
    fromlen = sizeof(from);
    return s_accept_nonblock(rb_cTCPSocket, fptr,
                             (struct sockaddr *)&from, &fromlen);
}
            
listen( int ) => 0 click to toggle source

Listens for connections, using the specified int as the backlog. A call to listen only applies if the socket is of type SOCK_STREAM or SOCK_SEQPACKET.

Parameter

  • backlog - the maximum length of the queue for pending connections.

Example 1

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 )

Example 2 (listening on an arbitary port, unix-based systems only):

require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
socket.listen( 1 )

Unix-based Exceptions

On unix based systems the above will work because a new sockaddr struct is created on the address ADDR_ANY, for an arbitrary port number as handed off by the kernel. It will not work on Windows, because Windows requires that the socket is bound by calling bind before it can listen.

If the backlog amount exceeds the implementation-dependent maximum queue length, the implementation's maximum queue length will be used.

On unix-based based systems the following system exceptions may be raised if the call to listen fails:

  • Errno::EBADF - the socket argument is not a valid file descriptor

  • Errno::EDESTADDRREQ - the socket is not bound to a local address, and the protocol does not support listening on an unbound socket

  • Errno::EINVAL - the socket is already connected

  • Errno::ENOTSOCK - the socket argument does not refer to a socket

  • Errno::EOPNOTSUPP - the socket protocol does not support listen

  • Errno::EACCES - the calling process does not have approriate privileges

  • Errno::EINVAL - the socket has been shut down

  • Errno::ENOBUFS - insufficient resources are available in the system to complete the call

Windows Exceptions

On Windows systems the following system exceptions may be raised if the call to listen fails:

  • Errno::ENETDOWN - the network is down

  • Errno::EADDRINUSE - the socket's local address is already in use. This usually occurs during the execution of bind but could be delayed if the call to bind was to a partially wildcard address (involving ADDR_ANY) and if a specific address needs to be commmitted at the time of the call to listen

  • Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the service provider is still processing a callback function

  • Errno::EINVAL - the socket has not been bound with a call to bind.

  • Errno::EISCONN - the socket is already connected

  • Errno::EMFILE - no more socket descriptors are available

  • Errno::ENOBUFS - no buffer space is available

  • Errno::ENOTSOC - socket is not a socket

  • Errno::EOPNOTSUPP - the referenced socket is not a type that supports the listen method

See

  • listen manual pages on unix-based systems

  • listen function in Microsoft's Winsock functions reference

 
               static VALUE
sock_listen(sock, log)
    VALUE sock, log;
{
    OpenFile *fptr;
    int backlog;

    rb_secure(4);
    backlog = NUM2INT(log);
    GetOpenFile(sock, fptr);
    if (listen(fileno(fptr->f), backlog) < 0)
        rb_sys_fail("listen(2)");

    return INT2FIX(0);
}
            
sysaccept() click to toggle source
 
               static VALUE
tcp_sysaccept(sock)
    VALUE sock;
{
    OpenFile *fptr;
    struct sockaddr_storage from;
    socklen_t fromlen;

    GetOpenFile(sock, fptr);
    fromlen = sizeof(from);
    return s_accept(0, fileno(fptr->f), (struct sockaddr*)&from, &fromlen);
}