Skip to content

Socket

Spice
import "std/net/socket";

InAddr struct

Represents an IPv4 address

In6Addr struct

Represents an IPv6 address

SockAddrIn struct

Socket address for an IPv4 (AF_INET) endpoint.

The field order and the field widths mirror struct sockaddr_in from the C headers one to one, so that the struct can be handed to the socket syscalls without any conversion in between.

SockAddrIn6 struct

Socket address for an IPv6 (AF_INET6) endpoint

SockAddrUn struct

Socket address for a Unix domain (AF_UNIX) endpoint

Socket struct

A network socket, wrapping the listening socket file descriptor and the current connection

Methods

acceptConnection

Spice
public f<Result<int>> Socket.acceptConnection()

Accept an incoming connection to the socket and save the connection file desceiptor to the socket object.

Returns: Result<int> — Connection file descriptor

write

Spice
public f<long> Socket.write(string message)

Write a raw string to the socket.

Parameters

Name Type Description
message string Content of the message

Returns: long — Number of bytes written

write

Spice
public f<long> Socket.write(byte* content, unsigned long size)

Write an array of bytes to the socket. Note: The given buffer needs to be at least of the given size.

Parameters

Name Type Description
content byte* Buffer of bytes to send
size unsigned long Number of bytes from the buffer to send

Returns: long — Number of bytes written

writeAll

Spice
public f<bool> Socket.writeAll(byte* content, unsigned long size)

Write an array of bytes to the socket, retrying until either all of them were handed over to the kernel or the connection breaks. A single write call is free to accept only a part of the buffer, so anything that must arrive in full has to go through this method. Note: The given buffer needs to be at least of the given size.

Parameters

Name Type Description
content byte* Buffer of bytes to send
size unsigned long Number of bytes from the buffer to send

Returns: bool — true if all bytes were written, false if the connection broke in between

read

Spice
public f<long> Socket.read(byte* buffer, long size)

Read n bytes from the socket to the given buffer. Note: The given buffer needs to be at least of the given size.

Parameters

Name Type Description
buffer byte* Buffer to write the result into
size long Number of bytes to read

Returns: long — Number of bytes written

setTimeout

Spice
public f<bool> Socket.setTimeout(unsigned long milliseconds)

Limits how long a single read or write on this socket may block. Without a timeout a peer that connects but never sends anything keeps the caller waiting forever. The timeout applies to the current connection, which for a client socket is the socket itself.

Parameters

Name Type Description
milliseconds unsigned long Timeout in milliseconds, 0 to block indefinitely again

Returns: bool — Setting the timeout was successful or not

closeConnection

Spice
public f<bool> Socket.closeConnection()

Closes the current connection of the socket, while keeping the socket itself open. This is what a server does after it has finished serving a single client.

Returns: bool — Closing the connection was successful or not

close

Spice
public f<bool> Socket.close()

Closes the socket. This method should always be called by the user before exiting the program.

Returns: bool — Closing the connection was successful or not

Functions

resolveHost

Spice
public f<Result<InAddrT>> resolveHost(string host)

Resolves a host given as either a dotted IPv4 address or a hostname to a raw IPv4 address in network byte order. Note: The DNS lookup is performed via gethostbyname, which is not thread-safe.

Parameters

Name Type Description
host string Dotted IPv4 address (e.g. "127.0.0.1") or hostname (e.g. "example.com")

Returns: Result<InAddrT> — IPv4 address in network byte order

openServerSocket

Spice
public f<Result<Socket>> openServerSocket(unsigned short port, int maxWaitingConnections = 5)

Opens a TCP server socket and exposes it to the given port. The maxWaitingConnections defines the maximum length to which the queue of pending connections may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol support retransmission, the request may be ignored so that a later reattempt at connection succeeds.

Parameters

Name Type Description
port unsigned short Port to open the socket on
maxWaitingConnections int Maximum size of the queue of pending client connections (default: 5)

Returns: Result<Socket> — Socket file descriptor

openClientSocket

Spice
public f<Result<Socket>> openClientSocket(string host, unsigned short port)

Opens a TCP client socket and tries to connect it to a server socket.

Parameters

Name Type Description
host string Host to connect to, either as dotted IPv4 address or as hostname
port unsigned short Post to connect to

Returns: Result<Socket> — Socket file descriptor

InAddrT alias

Alias for unsigned int.

InPortT alias

Alias for unsigned short.