# Socket::Constants

  The modules that contains the constants (such as `AF_INET`) required for the socket operations.

# BasicSocket

  The abstract class for the sockets. The operations are defined in the subclasses. For instance `TCPSocket` for the Internet domain stream socket.

## SuperClass:

  `IO`

## Methods:

  `getsockname`

    Returns the information of the socket in the sockaddr structure packed in the string. See `getsockname(2)` for detail.

  `getsockopt(level, optname)`

    Get options of the socket. See `getsockopt(2)`. Returns the option data packed in the string.

  `getpeername`

    Returns the information of the peer connected to the socket. Returns the sockaddr structure packed in the string. See `getpeername(2)` for detail.

  `recv(len[, flags])`

    Receives data from the socket and returns as an string. The len is the maximum length of the receiving data. See `recv(2)`. The default value for the flags is 0. The constants for flags are defined in `Socket` class (ex. `Socket::SO_LINGER`). It bypasses stdio, so mixing this with other kinds of reads/eof checks may cause confusion.

  `send(mesg, flags[, to])`

    Sends the mesg through the socket. See `send(2)` for detail. You have to specify the to argument for the unconnected socket. Reurns the length of data sent.

  `setsockopt(level, optname, optval)`

    Sets socket options. See `setsockopt(2)` for detail.

  `shutdown([how])`

    Causes the connection of the socket to be shut down. If how is 0, further receives will be rejected. If how is 1, further sends will be rejected. If how is 2, further sends and receives will be rejected. The default value for the how is 2. See `shutdown(2)`.

# IPSocket

  The class for the Internet domain socket.

## SuperClass:

  `BasicSocket`

## Class Methods:

  `getaddress(host)`

    Returns the address of the host. The address is an octet decimal string.

## Methods:

  `addr`

    Returns the array contains socket connection information. The first element is the string `"AF_INET"`. The second element is the port number. The third element is the host representing string. The fourth element is the IP address of the host in the octet decimal string.

  `peeraddr`

    Returns the array contains the peer's socket information. The elements of the array is same as the ones which `addr` returns.

# TCPSocket

  The class for the Internet domain stream socket. The socket programming will be much easier using this class. For example, the socket client that sends the user input will be:

  >     require "socket"

  >     port = if ARGV.size > 0 then ARGV.shift else 4444 end
  >     print port, "\n"

  >     s = TCPSocket.open("localhost", port)

  >     while gets
  >       s.write($_)
  >       print(s.gets)
  >     end
  >     s.close

## SuperClass:

  `IPSocket`

## Class Methods:

  `open(host, service)`
  `new(host, service)`

    Creates and returns the socket connected to the specified service on the host. The host is the host name string. The service is the service name registered in the `/etc/services` (or in NIS), or the port number.

  `gethostbyname(host)`

    Returns the array conaining the host information from the host name or the IP address (32 bit integer or string such as `"127.0.0.1"`). The first element of the array is the hostname. The second element is the array of the host aliases (possibly empty). The third element is the address type. And sequence of the addresses follows. The addresses are octet decimal string (like `"127.0.0.1"`).

## Methods:

  `recvfrom(len[, flags])`

    Receives data from the socket and returns the pair of data and the address of the sender. Refer IPSocket#addr for the address format. For arguments, see `recv`.

# TCPServer

  The server side of the Internet stream socket. This class makes building server much easier. For example, the echo server will be:

  >     require "socket"

  >     gs = TCPServer.open(0)
  >     socks = [gs]
  >     addr = gs.addr
  >     addr.shift
  >     printf("server is on %d\n", addr.join(":"))

  >     while true
  >       nsock = select(socks)
  >       next if nsock == nil
  >       for s in nsock[0]
  >         if s == gs
  >           socks.push(s.accept)
  >           print(s, " is accepted\n")
  >         else
  >           if s.eof?
  >     	print(s, " is gone\n")
  >     	s.close
  >     	socks.delete(s)
  >           else
  >     	str = s.gets
  >     	s.write(str)
  >           end
  >         end
  >       end
  >     end

  Even shorter using thread:

  >     require "socket"

  >     gs = TCPServer.open(0)
  >     addr = gs.addr
  >     addr.shift
  >     printf("server is on %d\n", addr.join(":"))

  >     while true
  >       ns = gs.accept
  >       print(ns, " is accepted\n")
  >       Thread.start do
  >         s =	ns			# save to dynamic variable
  >         while s.gets
  >           s.write($_)
  >         end
  >         print(s, " is gone\n")
  >         s.close
  >       end
  >     end

## SuperClass:

  `TCPSocket`

## Class Methods:

  `new([host, ]service)`
  `open([host, ]service)`

    Opens new server connection. The service is the service name registered in the `/etc/services` (or in NIS), or the port number. If host is given, only the connection from the specified host will be accepted. If host is not specified, connection from any host will be accepted.

## Methods:

  `accept`

    Accepts the request for the connection, and returns the `TCPSocket` connected to the client.

# UDPSocket

  The UDP/IP datagram socket class.

## SuperClass:

  `IPSocket`

## Class Methods:

  `open()`
  `new()`

    Returns new UDP socket.

## Methods:

  `bind(host, port)`

    Binds the socket to the port on the host.

  `connect(host, port)`

    Connects the socket to the port on the host.

  `recvfrom(len[, flags])`

    Receives data from the socket and returns the pair of data and the address of the sender. Refer IPSocket#addr for the address format. For arguments, see `recv`.

  `send(mesg, flags[, host, port])`

    Sends the mesg through the socket. See `send(2)` for detail. You have to specify the host and port arguments for the unconnected socket. Reurns the length of data sent.

# UNIXSocket

  The UNIX domain stream socket.

## SuperClass:

  `BasicSocket`

## Class Methods:

  `open(path)`
  `new(path)`

    The socket associated to the path.

## Methods:

  `addr`

    Returns the array contains socket connection information. The first element is the string `"AF_UNIX"`. The second element is the path associated to the socket.

  `path`

    Returns the path associated to the socket.

  `peeraddr`

    Returns the array contains the peer's socket information. The elements of the array is same as the ones which `addr` returns.

  `recvfrom(len[, flags])`

    Receives data from the socket and returns the pair of data and the path of the sender. For arguments, see `recv`.

# UNIXServer

  The server side of the UNIX stream socket.

## SuperClass:

  `UNIXSocket`

## Methods:

  `accept`

    Accepts the request for the connection, and returns the `UNIXSocket` connected to the client.

# Socket

  `Socket` provides the low level access to the socket features of the operating system. Its methods are about same level of the Perl's socket functions. The socket addresses are represented by the C structure packed into the string.

  Normally, the socket programming are done by the high level socket classes like `TCPSocket` and `TCPServer`.

## SuperClass:

  `BasicSocket`

## Class Methods:

  `open(domain, type, protocol)`
  `new(domain, type, protocol)`

    Creates new socket. domain, type, and protocol are specified by the constant found in the C header files. Most of the constants are defined as class constants in `Socket` class. domain and type can be specified by the string name. But all possible values may not available by the string.

  `for_fd(fd)`

    Creates new socket object corresponding the file discriptor fd.

  `pair(domain, type, protocol)`
  `socketpair(domain, type, protocol)`

    Returns the pair of the connected sockets. See `socketpair(2)`. The argument specification is same to `Socket.open`.

  `gethostbyname(host)`

    Returns the array conaining the host information from the host name or the IP address (32 bit integer or string such as `"127.0.0.1"`). The first element of the array is the hostname. The second element is the array of the host aliases (possibly empty). The third element is the address type. And sequence of the addresses follows. The addresses are packed string.

  `gethostbyaddr(host)`

    Returns the array conaining the host information from the packed `struct sockaddr`. Data in the array is as decribed in gethostbyname. host name or the IP address (32 bit integer or string such as `"127.0.0.1"`).

  `getservbyname(service[, proto])`

    Returns the port number correnspoinding service and proto. The default value for the proto is `"tcp"`.

## Methods:

  `accept`

    Accepts the connection and returns the pair of the socket for the new connection and address of the connection. See `accept(2)`.

  `bind(addr)`

    Binds the socket to the addr. See `bind(2)`. The addr is the sockaddr structure packed into the string.

  `connect(addr)`

    Connects the socket to the addr. The addr is the sockaddr structure packed into the string.

  `listen(backlog)`

    Specifies the connection queue limit for the socket. See `listen(2)`.

  `recvfrom(len[, flags])`

    Receives data from the socket and returns the pair of data and the address of the sender. For arguments, see `recv`.