socket: Network connections

This module supports creating TCP clients that create stream-based network connections to other computers or processes. In this context, the term socket refers to IPv4 stream sockets only.

See also: Use the serversocket module to create TCP servers.

The Socket class

class Socket(destination, port[, buffering])
Construct a TCP/IP connection stream to the specified port of the destination host. The destination may be either a host name such as "www.host.com" or a numeric IP address such as "100.50.200.5" (in decimal).

The buffering parameter specifies the buffering mode. If omitted, the connection is unbuffered. Valid values for the parameter are io::Buffered, io::LineBuffered and io::Unbuffered.

Socket is derived from io::Stream. Like File objects, Socket objects are narrow streams. Socket objects support both writing (sending) and reading (receiving) data.

Socket instances are also implicitly created by the serversocket::ServerSocket class.

Socket methods

Socket inherits most of the Stream operations unchanged, including:

It also supports the following operations:

close()
Close the connection. Free any resources allocated to the connection.
localAddress()
Return the local IP address as a string in dotted decimal format (e.g. "100.50.200.5").
localPort()
Return the local port number.
remoteAddress()
Return the remote IP address as a string in dotted decimal format (e.g. "100.50.200.5").
remotePort()
Return the remote port number.

Functions

GetHostByName(name)
Return the IP address of a host. The name argument may be a host name or a numeric IP address in dotted decimal format. If the host has multiple interfaces, only the address of the first interface is returned.
GetHostByAddress(address)
Return the primary host name of the machine with the given IP address. The IP address must be a string in dotted decimal format.
GetHostName()
Return a string containing the hostname of the machine where the program is running.

Exceptions

class NameError([message])
This exception is raised for DNS name lookup related errors. In particular, GetHostByName and GetHostByAddress may raise this exception. Inherits from std::Exception.