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.

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. See shutdown(2).


TCPsocket

The class for the Inernet domain 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"
s = TCPsocket.open("localhost", 4444)
while gets()
  s.write($_)
  print(s.read)
end

SuperClass:

BasicSocket

Class Methods:

getaddress(host)

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

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.

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 to the ones which addr returns.


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(4444)
socks = [gs]
while TRUE
  nsock = select(socks)
  next if nsock == nil
  for s in nsock[0]
    if s == gs
      socks.push(s.accept)
    else
      if s.eof?
        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.


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 to the ones which addr returns.


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. UDP sockets are only avilable through this class.

Normally, the socket programming are done by the high level socket classes like TCPsocet 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.

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.

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.

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. 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".

listen(backlog)

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

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.

recvfrom(len[, flags])

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

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.