The Socket Class

Figure 6-1. The Socket Class

getHostByAddr (class) method

>>--getHostByAddr(ipaddr)--------------------------------------><

This is a class method. It returns an instance of the HostInfo class.

getHostByName (class) method

>>--getHostByName(hostname)------------------------------------><

This is a class method. It returns an instance of the HostInfo class.

getHostId (class) method

>>--getHostId()------------------------------------------------><

This is a class method. It returns the dotted decimal host id of the local machine.

accept method

>>--accept()---------------------------------------------------><

This method returns a new socket class instance that is connected to a remote host that has requested a connection from a server socket.

bind method

>>--bind(address)----------------------------------------------><

This method binds a socket to a particular local ip address specified by an instance of the InetAddress class contained in the address argument.

close method

>>--close()----------------------------------------------------><

This method closes this socket instance.

connect method

>>--connect(address)-------------------------------------------><

This method connect the socket to a remote address specified by an instance of the InetAddress class contained in the address argument.

getOption method

>>--getOption(option)------------------------------------------><

This method returns the value of the options specified by the option argument.

The option argument must be one of the following:

SO_BROADCAST
SO_DEBUG
SO_DONTROUTE
SO_ERROR
SO_KEEPALIVE
SO_LINGER
SO_OOBINLINE
SO_RCVBUF
SO_RCVLOWAT
SO_RCVTIMEO
SO_REUSEADDR
SO_SNDBUF
SO_SNDLOWAT
SO_SNDTIMEO
SO_TYPE
SO_USELOOPBACK

getPeerName method

>>--getPeerName()----------------------------------------------><

This method returns the peer name of the remote connection.

getSockName method

>>--getSockName()----------------------------------------------><

This method returns an instance of the InetAddress class than is the name information of the remote machine.

new (class) method

>>--new(--+------------------------------------------+--)------><
          +--domain--+----------------------------+--+
                     +--, type--+--------------+--+
                                +--, protocol--+

This method returns a new instance of the Socket.

domain

If specified, this argument must be AF_INET.

type

If specified, this argument must be SOCK_STREAM, SOCK_DGRAM or SOCK_RAW. SOCK_STREAM is the default.

protocol

If specified, this argument must be 0, IPPROTO_UDP or IPPROTO_TCP. 0 is the default.

ioctl method

>>--ioctl(cmd, data)-------------------------------------------><

This method sends a special command to the socket. The cmd and the data are not checked for valid values.

listen method

>>--listen(backlog)--------------------------------------------><

This method turns the socket into a server listening socket. The backlog is the number of connection requests the socket should cache.

recv method

>>--recv(length)-----------------------------------------------><

This method recieves data on a socket connection. The length is the maximum number of bytes the socket should receive. This method returns the data received, which could be less than the maximum length specified.

recvFrom method

>>--recv(length, address)--------------------------------------><

This method recieves data on a socket connection from the specified address. The address must be an instance of the InetAddress class. The length is the maximum number of bytes the socket should receive. This method returns the data received, which could be less than the maximum length specified.

select method

>>--select(reads, writes, excepts, timeout)--------------------><

This method monitors activity on a set of sockets. It returns the number of sockets ready for activity. Upon return the input argument arrays will be reset to only the sockets that are ready.

reads

An array of socket instances to monitor for read activity.

writes

An array of socket instances to monitor for write activity.

excepts

An array of socket instances to monitor for exception activity.

timeout

The timeout in seconds. This must be a whole number (no fractions allowed).

Send method

>>--send(data)-------------------------------------------------><

This method sends the data on the socket. It returns the number of bytes sent, which could be less than the length of data.

setOption method

>>--setOption(name, value)-------------------------------------><

This method sets the option given by name with the data in value. See the method getOption for the list of valid names.

string method

>>--string()---------------------------------------------------><

This method returns the string representing the socket.