13.6 Networking
13.6.1 TCP
13.6.2 UDP
On this page:
TCP.Port
TCP.addresses
TCP.abandon
TCP.connect
TCPListener
TCP.listen
TCPListener.accept
TCPListener.accept_  ready
TCPListener.accept_  evt
TCPListener.addresses
TCPListener.close
8.16.0.2
13.6.1 TCP🔗ℹ

annotation

TCP.Port

 

function

fun TCP.addresses(port :: TCP.Port)

  :: values(String, PortNumber, String, ListenPortNumber)

 

function

fun TCP.abandon(port :: TCP.Port) :: Void

A TCP connection is represented by a part of ports, and an input port and an output port. Both of the ports are TCP.Ports, so they can be used with functions like Port.addresses and Port.abandon.

The TCP.addresses function returns a connection’s local IP address and port number followed by the connection peer’s IP address and port number.

The TCP.abandon function is almost the same as closing a port with Port.Input.close or Port.Output.close, but in in the case of closing an output port, the connection peer does not receive an end-of-file in its input stream.

function

fun TCP.connect(

  ~host: host :: String,

  ~port: port :: PortNumber,

  ~local_host: local_host :: maybe(String) = #false,

  ~local_port: local_port :: maybe(PortNumber) = #false,

  ~wait: wait :: NetworkWait = #'all

) :: values(Port.Input && Port.FileStream && TCP.Port,

            Port.Output && Port.FileStream && TCP.Port)

Connects as a client to a TCP server at host and port.

If local_host or local_port is not #false, it can determine the interface (at the operating-system level) on the local machine that is used for the TCP connection.

If wait is #'enable_break and breaks are currently disabled, then breaks are enabled while waiting for the connection to be created. In that case, either an Exn.Break exception is thrown or a connection and ports are created, but not both.

Since TCP.connect returns two ports, it can be used with Closeable.let.

class

class TCPListener():

  implements Closeable

  constructor (

    ~host: host :: maybe(String) = #false,

    ~port: port :: ListenPortNumber = 0,

    ~reuse: reuse = #false,

    ~max_allow_wait: max_allow_wait :: NonnegInt = 4

  )

 

function

fun TCP.listen(

    ~host: host :: maybe(String) = #false,

    ~port: port :: ListenPortNumber = 0,

    ~reuse: reuse = #false,

    ~max_allow_wait: max_allow_wait :: NonnegInt = 4

) :: TCPListener

A TCPListener represents a TCP listener for a server that can accept connections via TCPListener.accept.

If host is not #false, it determines the IP addresses and interfaces that accept connections for the listener. The reuse and max_allow_wait arguments similarly configure the created listener.

The TCP.listen function accepts the same arguments as the TCPListener constructor and returns a listener created with those arguments.

Since TCPListener implements Closeable, the TCPListener constructor or TCP.listen function can be used with Closeable.let.

method

method (lnr :: TCPListener).accept(

  ~wait: wait :: NetworkWait = #'all

) :: values(Port.Input && Port.FileStream && TCP.Port,

            Port.Output && Port.FileStream && TCP.Port)

 

method

method (lnr :: TCPListener).accept_ready() :: Boolean

 

method

method (lnr :: TCPListener).accept_evt() :: Evt

The TCPListener.accept method accepts a connection to a listener, blocking until a connection is available.

The TCPListener.accept_ready method reports whether a TCPListener.accept call will complete immediately because a connection is available.

The TCPListener.accept_evt method returns a synchronizable event that is ready for synchronization when a connection is available. The synchronization result is a PairList of containing two values, which are ports like the two results of TCPListener.accept.

Reports the local IP address and port number of lnr.

method

method (lnr :: TCPListener).close()

Closes the listener. Previously accepted connections are unaffected by closing a listener.