Class UDPSocket[in Socket]

UDP (datagram) manager.

class UDPSocket( addrOrService, [service] ) \ from Socket

more...

Summary

remoteContains the origin address of the last datagram received with the method.
remoteServiceContains the origin port of the last datagram received with the method.
broadcast()Activates broadcasting and multicasting abilities on this UDP socket.
recv()Reads incoming data.
sendTo()Sends a datagram to a given address.

Inherited properties

lastError from Socket Numeric value of system level error that has occoured on the socket.
timedout from Socket True if the last operation has timed out.

Inherited methods

dispose from Socket Closes a socket and frees system resources associated with it.
getHost from Socket Gets the host associated with this socket.
getPort from Socket Gets the port associated with this socket.
getService from Socket Returns the service name (port description) associated with this socket
getTimeout from Socket Returns the default timeout for this socket.
readAvailable from Socket Checks if there is available data to be read on this socket.
setTimeout from Socket Sets the default timeout for lengthy operations.
writeAvailable from Socket Waits for the socket to be ready to write data.

Detailed description

class UDPSocket( addrOrService, [service] ) \ from Socket

addrOrServiceAddress at which this server will be listening.
serviceIf an address is given, service or port number (as a string) where to listen.

UDP (datagram) manager.

The UDPSocket class provides support for UDP transmissions (datagrams).

The constructor reserves the needed system resources and return an UDPSocket object that can be used to send and receive datagrams.

Properties

remote

Contains the origin address of the last datagram received with the method.

remoteService

Contains the origin port of the last datagram received with the method.

Methods

broadcast()

Activates broadcasting and multicasting abilities on this UDP socket.

UDPSocket.broadcast( )

Raises:
NetErroron system error.

This is provided as a method separated from the socket constructor as, on some systems, this call requires administrator privileges to be successful.

recv()

Reads incoming data.

UDPSocket.recv( buffer, [size] )

bufferA pre-allocated buffer to fill.
sizeMaximum size in bytes to be read.
Returns:the amount of bytes actually read.
Raises:
NetErroron network error.

This method works as the TCPSocket.recv method, with the only difference that the incoming datagram is always completely read, provided that the specified size is enough to store the data.

Also, the UDPSocket.remote and UDPSocket.remoteService properties of the receiving object are filled with the address and port of the host sending the packet.

In case of system error, a NetError is raised.

sendTo()

Sends a datagram to a given address.

UDPSocket.sendTo( host, service, buffer, [size], [start] )

hostRemote host where to send the datagram.
serviceRemote service or port number where to send the datagram.
bufferThe buffer to be sent.
sizeAmount of bytes from the buffer to be sent.
startBegin position in the buffer.
Raises:
NetErroron network error.

This method works as the TCPSocket.send method, with the main difference that the outgoing datagram can be directed towards a specified host, and that a whole datagram is always completely filled before being sent, provided that the specified size does not exceed datagram size limits.

The host parameter may be an host name to be resolved or an address; if the UDPSocket.broadcast method has been successfully called, it may be also a multicast or broadcast address.

The service parameter is a string containing either a service name (i.e. "http") or a numeric port number (i.e. "80", as a string).

The buffer may be a byte-only string or a byte-wide MemBuf; it is possible to send also multibyte strings (i.e. strings containing international characters) or multi-byte memory buffers, but in that case the sent data may get corrupted as a transmission may deliver only part of a character or of a number stored in a memory buffer.

Note: If the buffer is a MemBuf item, size and start parameters are ignored, and the buffer MemBuf.position and MemBuf.limit are used to determine how much data can be received. After a successful receive, the value of MemBuf.position is moved forward accordingly.

If a size parameter is not specified, the method will try to send the whole content of the buffer, otherwise it will send at maximum size bytes. If a start parameter is specified, then the data sent will be taken starting from that position in the buffer (counting in bytes from the start).

This is useful when sending big buffers in several steps, so that it is not necessary to create substrings for each send, sparing both CPU and memory.

The returned value may be 0 in case of timeout, otherwise it will be a number between 1 and the requested size. Programs should never assume that a successful sendTo has sent all the data.

In case of error, a NetError is raised.


Made with faldoc 2.2.1