Link Search Menu Expand Document

Socket

Table of contents

  1. Socket
    1. Constants
    2. Socket.create(number: family, number: type)
    3. socket.bind(string, number)
    4. socket.connect(string, number)
    5. socket.listen(number: backlog -> optional)
    6. socket.accept()
    7. socket.write(string)
    8. socket.recv(number)
    9. socket.close()
    10. socket.setsocketopt(number: level, number: option)

Socket

To make use of the Socket module an import is required.

import Socket;

Constants

Constant Description
Socket.AF_INET AF_INET protocol family
Socket.SOCK_STREAM SOCK_STREAM protocol type
Socket.SOL_SOCKET SOL_SOCKET option level
Socket.SO_REUSEADDR SO_REUSEADDR allow socket reuse

Socket.create(number: family, number: type)

Create a new socket object given a socket type and socket family. This will return a Result and unwrap to a new socket object in which the rest of the methods are ran on.

var result = Socket.create(Socket.AF_INET, Socket.SOCK_STREAM);
if (!result.success()) {
    print(result.unwrapError());
    // ...
}

var socket = result.unwrap();

socket.bind(string, number)

This will bind a given socket object to an IP and port number. Returns a Result type and on success will unwrap to nil.

var result = socket.bind("127.0.0.1", 1000);
if (!result.success()) {
    print(result.unwrapError());
    // ...
}

socket.connect(string, number)

This will connect to a socket on a given host and IP. Returns a Result type and on success will unwrap to nil.

var result = socket.connect("127.0.0.1", 1000);
if (!result.success()) {
    print(result.unwrapError());
    // ...
}

socket.listen(number: backlog -> optional)

This will enable connections to be made. The backlog parameter specifies how many pending connections can be queued before they begin to get rejected. If left unspecified a reasonable value is chosen. Returns a Result type and on success will unwrap to nil.

var result = socket.listen();
if (!result.success()) {
    print(result.unwrapError());
    // ...
}

socket.accept()

This will accept incoming connections. The socket must be bound to an address an listening for incoming connections before .accept() can be used. Returns a Result type that wraps a list of two values where the first value is a new socket object and the second is the address connecting to the socket as a string.

var [client, address] = socket.accept().unwrap();
print(address); // 127.0.0.1

socket.write(string)

This will write data to the remote client socket. Returns a Result type and on success will unwrap to a number (amount of chars written).

var result = socket.write("Some Data");
if (!result.success()) {
    print(result.unwrapError());
    // ...
}

socket.recv(number)

This will receive data from the client socket. The maximum amount of data to be read at a given time is specified by the argument passed to recv(). Returns a Result type and on success will unwrap to a string.

Note: The argument passed to recv should be a relatively small power of 2, such as 2048 or 4096.

var result = socket.recv(2048);
if (!result.success()) {
    print(result.unwrapError());
    // ...
}

socket.close()

Closes a socket.

socket.close();

socket.setsocketopt(number: level, number: option)

Set a socket option on a given socket. Returns a Result type and on success will unwrap to nil.

var result = socket.setsockopt(Socket.SOL_SOCKET, Socket.SO_REUSEADDR);
if (!result.success()) {
    print(result.unwrapError());
    // ...
}

This site uses Just The Docs, with modifications.