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.
Class Socket
Inherits Stream
- class Socket(destination as Str, port as Int[, buffering as Constant])
-
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:
- write
- writeLn
- read
- readLn
- readLines
- flush
- eof
It also supports the following operations:
- close()
- Close the connection. Free any resources allocated to the connection.
- localAddress() as Str
- Return the local IP address as a string in dotted decimal format (e.g. "100.50.200.5").
- localPort() as Int
- Return the local port number.
- remoteAddress() as Str
- Return the remote IP address as a string in dotted decimal format (e.g. "100.50.200.5").
- remotePort() as Int
- Return the remote port number.
Functions
- GetHostByName(name as Str) as Str
- 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 as Str) as Str
- 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() as Str
- Return a string containing the hostname of the machine where the program is running.
Exceptions
- class NameError([message as Str])
- This exception is raised for DNS name lookup related errors. In particular, GetHostByName and GetHostByAddress may raise this exception. Inherits from std::Exception.