Socket over UDP, sends and receive data without the need for a connection

Introduced properties

fun bind(address: nullable Text, port: Int)

socket :: UDPSocket :: bind

Bind this socket to an address, on port (to all addresses if null)
fun broadcast(port: Int, data: Text)

socket :: UDPSocket :: broadcast

Broadcast data on the network on port
fun enable_broadcast=(value: Bool)

socket :: UDPSocket :: enable_broadcast=

Enable broadcasting for this socket
fun error: nullable Error

socket :: UDPSocket :: error

Last error raised by this socket
protected fun error=(error: nullable Error)

socket :: UDPSocket :: error=

Last error raised by this socket
fun recv(length: Int): String

socket :: UDPSocket :: recv

Receive length bytes of data from any sender
fun recv_from(length: Int, sender: Ref[nullable SocketAddress]): String

socket :: UDPSocket :: recv_from

Receive length bytes of data from any sender and store the sender info in sender.item
fun send_to(dest_address: Text, port: Int, data: Text)

socket :: UDPSocket :: send_to

Send data to dest_address on port

Redefined properties

redef type SELF: UDPSocket

socket $ UDPSocket :: SELF

Type of this instance, automatically specialized in every class
redef init init

socket $ UDPSocket :: init

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun bind(address: nullable Text, port: Int)

socket :: UDPSocket :: bind

Bind this socket to an address, on port (to all addresses if null)
fun blocking=(value: Bool)

socket :: Socket :: blocking=

Set whether calls to accept are blocking
fun broadcast(port: Int, data: Text)

socket :: UDPSocket :: broadcast

Broadcast data on the network on port
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun closed: Bool

socket :: Socket :: closed

Is this socket closed?
protected fun closed=(closed: Bool)

socket :: Socket :: closed=

Is this socket closed?
fun enable_broadcast=(value: Bool)

socket :: UDPSocket :: enable_broadcast=

Enable broadcasting for this socket
fun error: nullable Error

socket :: UDPSocket :: error

Last error raised by this socket
protected fun error=(error: nullable Error)

socket :: UDPSocket :: error=

Last error raised by this socket
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun recv(length: Int): String

socket :: UDPSocket :: recv

Receive length bytes of data from any sender
fun recv_from(length: Int, sender: Ref[nullable SocketAddress]): String

socket :: UDPSocket :: recv_from

Receive length bytes of data from any sender and store the sender info in sender.item
fun send_to(dest_address: Text, port: Int, data: Text)

socket :: UDPSocket :: send_to

Send data to dest_address on port
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram socket::UDPSocket UDPSocket socket::Socket Socket socket::UDPSocket->socket::Socket core::Object Object socket::Socket->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class Socket

socket :: Socket

A general Socket, either TCP or UDP

Class definitions

socket $ UDPSocket
# Socket over UDP, sends and receive data without the need for a connection
class UDPSocket
	super Socket

	# Last error raised by this socket
	var error: nullable Error = null

	init do native = new NativeSocket.socket(
			new NativeSocketAddressFamilies.af_inet,
			new NativeSocketTypes.sock_dgram,
			new NativeSocketProtocolFamilies.pf_unspec)

	# Bind this socket to an `address`, on `port` (to all addresses if `null`)
	#
	# On error, sets `error` appropriately.
	fun bind(address: nullable Text, port: Int)
	do
		var addr_in = new NativeSocketAddrIn
		addr_in.port = port
		if address != null then
			# FIXME replace all use of gethostbyname with something not obsolete
			var hostent = sys.gethostbyname(address.to_cstring)
			if hostent.address_is_null then
				error = new IOError.from_h_errno
				addr_in.free
				return
			end

			addr_in.fill_from_hostent hostent
		else
			addr_in.family = new NativeSocketAddressFamilies.af_inet
			addr_in.address_any
		end

		if native.bind(addr_in) != 0 then error = new IOError.from_errno

		addr_in.free
	end

	# Receive `length` bytes of data from any sender
	#
	# On error, returns an empty string and sets `error` appropriately.
	fun recv(length: Int): String
	do
		var buf = new CString(length)
		var len = native.recvfrom(buf, length, 0, new NativeSocketAddrIn.nul)
		if len == -1 then
			error = new IOError.from_errno
			return ""
		end
		return buf.to_s_unsafe(len, copy=false)
	end

	# Receive `length` bytes of data from any sender and store the sender info in `sender.item`
	#
	# On error, returns an empty string and sets `error` appropriately.
	fun recv_from(length: Int, sender: Ref[nullable SocketAddress]): String
	do
		var src = new NativeSocketAddrIn
		var buf = new CString(length)

		var len = native.recvfrom(buf, length, 0, src)
		if len == -1 then
			error = new IOError.from_errno
			src.free
			return ""
		end

		sender.item = new SocketAddress(src)
		return buf.to_s_unsafe(len, copy=false)
	end

	# Send `data` to `dest_address` on `port`
	#
	# On error, sets `error` appropriately.
	fun send_to(dest_address: Text, port: Int, data: Text)
	do
		var hostent = sys.gethostbyname(dest_address.to_cstring)
		if hostent.address_is_null then
			error = new IOError.from_h_errno
			return
		end

		var dest = new NativeSocketAddrIn
		dest.fill_from_hostent hostent
		dest.port = port
		native.setsockopt(new NativeSocketOptLevels.socket, new NativeSocketOptNames.broadcast, 1)

		var buf = data.to_cstring
		if native.sendto(buf, data.length, 0, dest) == -1 then
			error = new IOError.from_errno
		end
		dest.free
	end

	# Enable broadcasting for this socket
	#
	# On error, sets `error` appropriately.
	fun enable_broadcast=(value: Bool) do
		var res = native.setsockopt(new NativeSocketOptLevels.socket, new NativeSocketOptNames.broadcast, value.to_i)
		if res == -1 then error = new IOError.from_errno
	end

	# Broadcast `data` on the network on `port`
	#
	# On error, sets `error` appropriately.
	#
	# Require: setting `enable_broadcast = true`
	fun broadcast(port: Int, data: Text)
	do
		var addr_in = new NativeSocketAddrIn
		addr_in.port = port
		addr_in.family = new NativeSocketAddressFamilies.af_inet
		addr_in.address_broadcast

		var buf = data.to_cstring
		if native.sendto(buf, data.length, 0, addr_in) == -1 then
			error = new IOError.from_errno
		end

		addr_in.free
	end
end
lib/socket/socket.nit:343,1--465,3