Kore
Loading...
Searching...
No Matches
socket.h File Reference

Provides low-level network-communication via UDP or TCP-sockets. More...

#include <kinc/global.h>

Go to the source code of this file.

Data Structures

struct  kinc_socket
struct  kinc_socket_options

Typedefs

typedef enum kinc_socket_protocol kinc_socket_protocol_t
typedef enum kinc_socket_family kinc_socket_family_t
typedef struct kinc_socket kinc_socket_t
typedef struct kinc_socket_options kinc_socket_options_t

Enumerations

enum  kinc_socket_protocol { KINC_SOCKET_PROTOCOL_UDP , KINC_SOCKET_PROTOCOL_TCP }
enum  kinc_socket_family { KINC_SOCKET_FAMILY_IP4 , KINC_SOCKET_FAMILY_IP6 }

Functions

KINC_FUNC void kinc_socket_options_set_defaults (kinc_socket_options_t *options)
 Initializes a socket-options-object to the default options.
KINC_FUNC void kinc_socket_init (kinc_socket_t *socket)
 Initializes a socket-object. To set the host and port use kinc_socket_set. Host will be localhost Port will be 8080 Family will be IPv4 Protocol will be TCP.
KINC_FUNC bool kinc_socket_set (kinc_socket_t *socket, const char *host, int port, kinc_socket_family_t family, kinc_socket_protocol_t protocol)
 Sets the sockets properties.
KINC_FUNC void kinc_socket_destroy (kinc_socket_t *socket)
 Destroys a socket-object.
KINC_FUNC bool kinc_socket_open (kinc_socket_t *socket, kinc_socket_options_t *options)
 Opens a socket-connection.
KINC_FUNC bool kinc_socket_select (kinc_socket_t *socket, uint32_t waittime, bool read, bool write)
 For use with non-blocking sockets to try to see if we are connected.
KINC_FUNC bool kinc_socket_bind (kinc_socket_t *socket)
KINC_FUNC bool kinc_socket_listen (kinc_socket_t *socket, int backlog)
KINC_FUNC bool kinc_socket_accept (kinc_socket_t *socket, kinc_socket_t *new_socket, unsigned *remote_address, unsigned *remote_port)
KINC_FUNC bool kinc_socket_connect (kinc_socket_t *socket)
KINC_FUNC int kinc_socket_send (kinc_socket_t *socket, const uint8_t *data, int size)
KINC_FUNC int kinc_socket_send_address (kinc_socket_t *socket, unsigned address, int port, const uint8_t *data, int size)
KINC_FUNC int kinc_socket_send_url (kinc_socket_t *socket, const char *url, int port, const uint8_t *data, int size)
KINC_FUNC int kinc_socket_receive (kinc_socket_t *socket, uint8_t *data, int maxSize, unsigned *from_address, unsigned *from_port)
KINC_FUNC unsigned kinc_url_to_int (const char *url, int port)
 Resolves a DNS-entry to an IP and returns its integer representation.

Detailed Description

Provides low-level network-communication via UDP or TCP-sockets.

Function Documentation

◆ kinc_socket_destroy()

KINC_FUNC void kinc_socket_destroy ( kinc_socket_t * socket)

Destroys a socket-object.

Parameters
socketThe socket to destroy

◆ kinc_socket_init()

KINC_FUNC void kinc_socket_init ( kinc_socket_t * socket)

Initializes a socket-object. To set the host and port use kinc_socket_set. Host will be localhost Port will be 8080 Family will be IPv4 Protocol will be TCP.

Parameters
socketThe socket to initialize

◆ kinc_socket_open()

KINC_FUNC bool kinc_socket_open ( kinc_socket_t * socket,
kinc_socket_options_t * options )

Opens a socket-connection.

Parameters
socketThe socket-object to use
protocolThe protocol to use
portThe port to use
optionsThe options to use
Returns
Whether the socket-connection could be opened

◆ kinc_socket_options_set_defaults()

KINC_FUNC void kinc_socket_options_set_defaults ( kinc_socket_options_t * options)

Initializes a socket-options-object to the default options.

Parameters
optionsThe new default options

◆ kinc_socket_select()

KINC_FUNC bool kinc_socket_select ( kinc_socket_t * socket,
uint32_t waittime,
bool read,
bool write )

For use with non-blocking sockets to try to see if we are connected.

Parameters
socketThe socket-object to use
waittimeThe amount of time in seconds the select function will timeout.
readCheck if the socket is ready to be read from.
writeCheck if the socket is ready to be written to.
Returns
Whether the socket-connection can read or write or checks both.

◆ kinc_socket_set()

KINC_FUNC bool kinc_socket_set ( kinc_socket_t * socket,
const char * host,
int port,
kinc_socket_family_t family,
kinc_socket_protocol_t protocol )

Sets the sockets properties.

Parameters
socketThe socket-object to use
hostThe host to use as IP or URL
portThe port to use
familyThe IP-family to use
protocolThe protocol to use
Returns
Whether the socket was set correctly

◆ kinc_url_to_int()

KINC_FUNC unsigned kinc_url_to_int ( const char * url,
int port )

Resolves a DNS-entry to an IP and returns its integer representation.

Parameters
url
port
Returns