2.12.5Class UDPSocket

UDP (datagram) manager.

Class UDPSocket( addrOrService, [service] ) from \
                 Socket( )
addrOrService Address at which this server will be listening.
service If an address is given, service or port number (as a string) where to listen.
Raise
NetError on system error.

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
broadcastActivates broadcasting and multicasting abilities on this UDP socket.
recvReads incoming data.
sendToSends a datagram to a given address.
Properties inherited from class Socket
lastError Numeric value of system level error that has occoured on the socket.
timedout True if the last operation has timed out.
Methods inherited from class Socket
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

remote

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

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

remoteService

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

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

Methods

broadcast

Activates broadcasting and multicasting abilities on this UDP socket.

UDPSocket.broadcast()
Raise
NetError on 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] )
buffer A pre-allocated buffer to fill.
size Maximum size in bytes to be read.
Returnthe amount of bytes actually read.
Raise
NetError on 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.

See also: Socket.

sendTo

Sends a datagram to a given address.

UDPSocket.sendTo( host, service, buffer, [size],[start] )
host Remote host where to send the datagram.
service Remote service or port number where to send the datagram.
buffer The buffer to be sent.
size Amount of bytes from the buffer to be sent.
start Begin position in the buffer.
Raise
NetError on 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.

See also: Socket.

Made with http://www.falconpl.org