The SockUtil structure
Author: John Reppy
Keywords: Standard ML, SML, SML/NJ Library

The SML of NJ Library
The `SockUtil` structure

The `SockUtil` structure provides a collection of utility functions for programming with the Basis Library `Socket` structure

## Synopsis

    signature SOCK_UTIL
    structure SockUtil : SOCK_UTIL

## Interface

    datatype port = PortNumber of int | ServName of string

    datatype hostname = HostName of string | HostAddr of NetHostDB.in_addr

    val scanAddr : (char, 'a) StringCvt.reader
          -> ({host : hostname, port : port option}, 'a) StringCvt.reader
    val addrFromString : string -> {host : hostname, port : port option} option

    exception BadAddr of string

    val resolveAddr : {host : hostname, port : port option}
          -> {host : string, addr : NetHostDB.in_addr, port : int option}

    type 'a stream_sock = ('a, Socket.active Socket.stream) Socket.sock

    val connectINetStrm : {addr : NetHostDB.in_addr, port : int}
          -> INetSock.inet stream_sock

    val recvVec : ('a stream_sock * int) -> Word8Vector.vector
    val recvStr : ('a stream_sock * int) -> string

    val sendVec : ('a stream_sock * Word8Vector.vector) -> unit
    val sendStr : ('a stream_sock * string) -> unit
    val sendArr : ('a stream_sock * Word8Array.array) -> unit

## Description

`datatype port = PortNumber of int | ServName of string`

  specifies a port identifier, which either be a number (`Port`) or the name of a service (`ServName`).

`datatype hostname = HostName of string | HostAddr of NetHostDB.in_addr`

  something

`val scanAddr : (char, 'a) StringCvt.reader -> ({host : hostname, port : port option}, 'a) StringCvt.reader`

  `scanAddr getc` returns an address reader. An address is a string of the form \(\mathit{addr}\,[\,\mathtt{:}\,\mathit{port}\,\)], where \(\mathit{addr}\) may either be a numeric or symbolic host name and the optional port is either a decimal port number or alphanumeric service name. Legal host names must begin with a letter, and may contain any alphanumeric character, the minus sign (`-`) and period (`.`), where the period is used as a domain separator.

`val addrFromString : string -> {host : hostname, port : port option} option`

  `addrFromString addr` converts the string `addr` to a host-port address specifier. The syntax of addresses is as described for `scanAddr`.

`exception BadAddr of string`

  This exception is raised by `resolveAddr`.

`val resolveAddr : {host : hostname, port : port option} -> {host : string, addr : NetHostDB.in_addr, port : int option}`

  `resolveAddr {host, port}` resolves the hostname and optional port in the host and service databases. If either the host or service name is not found, then the `BadAddr` exception is raised.

`type 'a stream_sock = ('a, Socket.active Socket.stream) Socket.sock`

  A type abbreviation for active stream sockets.

`val connectINetStrm : {addr : NetHostDB.in_addr, port : int} -> INetSock.inet stream_sock`

  `connectINetStrm {addr, port}` establishs a client-side connection to an INET domain stream socket.

`val recvVec : ('a stream_sock * int) -> Word8Vector.vector`

  `recvVec (sock, n)` reads `n` bytes from the stream socket `sock`; fewer than `n` bytes is returned when the stream is closed at the other end of the connection. It raises the `Size` exception when `n` is negative.

`val recvStr : ('a stream_sock * int) -> string`

  `recvStr (sock, n)` reads `n` characters from the stream socket `sock`; fewer than `n` characters is returned when the stream is closed at the other end of the connection. It raises the `Size` exception when `n` is negative.

`val sendVec : ('a stream_sock * Word8Vector.vector) -> unit`

  `sendVec (sock, vec)` sends the vector `vec` on the stream socket `sock`.

`val sendStr : ('a stream_sock * string) -> unit`

  `sendStr (sock, s)` sends the string `s` on the stream socket `sock`.

`val sendArr : ('a stream_sock * Word8Array.array) -> unit`

  `sendArr (sock, arr)` sends the array `arr` on the stream socket `sock`.

## See Also

`Socket` (SML Basis), The INet Library

SML/NJ Version 110.99.9 (November 4, 2025)
Last updated 2025-11-04 15:48:25 UTC