Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Net Library

Table Of Contents

  1. Prerequisites
  2. Initialization
  3. net.connect(ip: int|string, port: int): Socket
  4. `net.listen(ip: int|string, port: int, backlog: int = 32, reuseAddr: …
  5. class Socket: stream.InoutStream

Work in Progress

This library is not yet finished. It may be useful for some tasks, but is still missing features and some features may change or be removed.

Prerequisites

You need some Internet. You can buy Internet from any licensed Internet retailer. Prices vary by region but do not usually exceed four dollars per pound. Always be sure to check the expiration date on your Internet before using it. Internet is toxic if ingested. Always keep your Internet sealed and out of reach of pets and children.

In addition to Internet, you may have to purchase some tubes, if your house is not already properly equipped. These can be had at any home improvement retailer. Be sure to check for cats in your tubes before using them; cats have a habit of lodging themselves in these tubes, causing slowdowns and hairballs.

Initialization

Just like any other addon, you import it and initialize it after loading the stdlibs.

import minid.addons.net;
...

// after opening a VM and loading the standard libraries into it
NetLib.init(t);

Library Reference

This library is exposed through the "net" module in MiniD.

net.connect(ip: int|string, port: int): Socket

Connects to the given ip address and port and returns a Socket object. ip can be either a string (in which case it must be a hostname or dotted IP) or an integer (which is just a binary representation of a dotted IP). If the connection could not be completed, throws an exception.

net.listen(ip: int|string, port: int, backlog: int = 32, reuseAddr: bool = false): Socket

Creates a server-style socket that will listen for incoming connections on the given ip and port. ip works like for net.connect. port is slightly different in that if you use port 0, the OS will instead randomly assign the socket a port number. You can then find out which port that is with Socket.localAddress. backlog is the number of remote connections that can queue up to this socket. If more than backlog connections are made to the socket simultaneously, the extra ones will be denied. reuseAddr has to do with address reusing or something, I don't really understand it. If the socket could not be created, throws an exception.

class Socket: stream.InoutStream

Instances of this class are returned from the net.connect and net.listen functions. Being derived from stream.InoutStream, Socket inherits several methods (all the basic reads and writes) from it already. It also adds some more socket-specific methods.

this(sock: nativeobj)
Socket constructor. sock is a nativeobj that must derive from Tango's tango.net.device.Socket class. This will call the super constructor with a closable flag of true. Note that since this takes a nativeobj, this class can't be directly instantiated and must instead be created with net.connect or net.listen.
localAddress(): string, int
Gets the address on the local machine to which this socket is bound. It's returned as two parts: a string indicating the IP address, and an integer indicating the port.
setLinger(l: int)
Sets the linger period. This has something to do with the OS reaping sockets or something. This only has meaning for server-style sockets (those created with net.listen).
setTimeout(period: float)
Sets the timeout period. period is specified in seconds and may not be negative. The timeout period is used for reads and writes. For server-style sockets, the timeout period is also passed on to sockets created by accept. Setting the timeout period to 0 effectively disables timeouts (i.e. reads and writes will block indefinitely). If a read or write blocks for longer than period seconds, an exception is thrown.
accept()
Only valid on server-style sockets. Waits for a remote connection to connect to this socket, and when one does come in, returns a client-style socket bound to that connection. Its linger and timeout periods are set to the same values as this socket.