TCP/IP networking base class.
class Socket
lastError | Numeric value of system level error that has occoured on the socket. |
timedout | True if the last operation has timed out. |
dispose() | Closes a socket and frees system resources associated with it. |
getHost() | Gets the host associated with this socket. |
getPort() | Gets the port associated with this socket. |
getService() | Returns the service name (port description) associated with this socket |
getTimeout() | Returns the default timeout for this socket. |
readAvailable() | Checks if there is available data to be read on this socket. |
setTimeout() | Sets the default timeout for lengthy operations. |
writeAvailable() | Waits for the socket to be ready to write data. |
class Socket
TCP/IP networking base class.
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.
Numeric value of system level error that has occoured on the socket.
True if the last operation has timed out.
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.
Gets the host associated with this socket.
Socket.getHost( )
Returns: | The 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.
Gets the port associated with this socket.
Socket.getPort( )
Returns: | The 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.
Returns the service name (port description) associated with this socket
Socket.getService( )
Returns: | A 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").
Returns the default timeout for this socket.
Socket.getTimeout( )
Returns: | A numeric value (seconds or seconds fractions). |
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. | ||
Returns: | True if the next read operation would not block. | ||
Raises: |
|
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.
Sets the default timeout for lengthy operations.
Socket.setTimeout( timeout )
timeout | Timeout in seconds and fractions. |
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.
Waits for the socket to be ready to write data.
Socket.writeAvailable( [timeout] )
timeout | Optional wait in seconds. | ||
Returns: | True if the socket is writeable or becomes writeable during the wait. | ||
Raises: |
|
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.