2.12.2Class Socket

TCP/IP networking base class.

Class Socket

The Socket class is the base class for both UDP and TCP socket. It provides common methods and properties, and so it should not be directly instantiated.

Properties
lastError Numeric value of system level error that has occoured on the socket.
timedout True if the last operation has timed out.
Methods
disposeCloses a socket and frees system resources associated with it.
getHostGets the host associated with this socket.
getPortGets the port associated with this socket.
getServiceReturns the service name (port description) associated with this socket
getTimeoutReturns the default timeout for this socket.
readAvailableChecks if there is available data to be read on this socket.
setTimeoutSets the default timeout for lengthy operations.
writeAvailableWaits for the socket to be ready to write data.

Properties

lastError

Numeric value of system level error that has occoured on the socket.

Numeric value of system level error that has occoured on the socket. Standard Function socketErrorDesc may be used to get a human-readable description of the error. The error is usually also written in the fsError field of the exceptions, if case they are caught.

timedout

True if the last operation has timed out.

True if the last operation has timed out. See Socket.setTimeout.

Methods

dispose

Closes a socket and frees system resources associated with it.

Socket.dispose()

Sockets are automatically disposed by garbage collector, but the calling program may find useful to free them immediately i.e. in tight loops while accepting and serving sockets.

getHost

Gets the host associated with this socket.

Socket.getHost()
ReturnThe host address.

For TCP sockets, this method will always return the address of the remote host, while in UDP sockets it will be the local address where the socket has been bound.

getPort

Gets the port associated with this socket.

Socket.getPort()
ReturnThe port address.

For TCP sockets, returns a numeric representation of the port to which the socket is connected. For UDP sockets, returns the local port from which the messages sent through this socket are generated, if an explicit bound request has been issued.

getService

Returns the service name (port description) associated with this socket

Socket.getService()
ReturnA string containing the service name.

For TCP sockets, returns the name of the service to which the socket is connected. For UDP sockets, returns the local service from which the messages sent through this socket are generated, if an explicit bound request has been issued. Returned values are a system-specific 1:1 mapping of numeric ports to service names. If the port has not an associated service name, the port number is returned as a string value (I.e. port 80 is returned as the string "80").

getTimeout

Returns the default timeout for this socket.

Socket.getTimeout()
ReturnA numeric value (seconds or seconds fractions).

See also: Socket.

readAvailable

Checks if there is available data to be read on this socket.

Socket.readAvailable( [timeout] )
timeout Wait for a specified time in seconds or fractions.
ReturnTrue if the next read operation would not block.
Raise
InterruptedError In case of asynchronous interruption.

This method can be used to wait for incoming data on the socket. If there are some data available for immediate read, the function returns immediately true, otherwise it returns false.

If timeout is not given, the function will return immediately, peeking for current read availability status. If it is given, the function will wait for a specified amount of seconds, or for some data to become available for read, whichever comes first.

If the timeout value is negative, the function will wait forever, until some data is available.

This wait will block the VM and all the coroutines.

Note: On Unix, this function respects the VirtualMachine interruption protocol, and can be asynchronously interrupted from other threads. This functionality is not implemented on MS-Windows systems yet, and will be provided in version 0.8.12.

See also: Socket.

setTimeout

Sets the default timeout for lengthy operations.

Socket.setTimeout( timeout )
timeout Timeout in milliseconds.

This function sets a default timeout for the read/write operations, or for other lengthy operations as connect. If -1 is set (the default at socket creation), blocking operation will wait forever, until some data is ready. In this case, readAvailable and writeAvailable methods can be used to peek for incoming data.

If 0 is set, read/write operations will never block, returning immediately if data is not available. If a value greater than zero is set, blocking functions will wait the specified amount of seconds for their action to complete.

Whenever an operation times out, the Socket.timedout member property is set to 1. This allows to distinguish between faulty operations and timed out ones.

Socket.readAvailable and Socket.writeAvailable methods do not use this setting.

writeAvailable

Waits for the socket to be ready to write data.

Socket.writeAvailable( [timeout] )
timeout Optional wait in seconds.
ReturnTrue if the socket is writeable or becomes writeable during the wait.
Raise
InterruptedError in case of asynchronous interruption.

This method checks for the socket to be ready for immediate writing. A socket may not be ready for writing if the OS system stack is full, which usually means that the other side has not been able to forward the received messages to the listening application.

The method will return true in case the socket is available for write, false otherwise.

An optional timeout may be specified; in this case, the function will return true if the socket is immediately available or if it becomse available before the wait expires, false otherwise. The wait blocks the VM, preventing other coroutines to be processed as well.

This function does not take into consideration overall timeout set by Socket.setTimeout.

Note: On Unix, this function respects the VirtualMachine interruption protocol, and can be asynchronously interrupted from other threads. This functionality is not implemented on MS-Windows systems yet, and will be provided in version 0.8.12.

See also: Socket.

Made with http://www.falconpl.org