NetworkUtilities 1.4.2
A small compiled library for C network utility functions.
Loading...
Searching...
No Matches
Port Creation

Functions related to initializing network ports and setting their properties. More...

Functions

struct sockaddr_in initSocketAddress (char *ip_address, uint16_t port)
 Initializes a socket address.
 
int initializeWinsock ()
 Initializes Winsock library.
 
int initializeUDPClientSocket (SOCKET *udpSocket, struct sockaddr_in *serverAddr, char *serverIP, uint16_t port)
 Initializes a UDP socket and configures server address.
 
int initializeUDPServerSocket (SOCKET *udpSocket, struct sockaddr_in *serverAddr, uint16_t port)
 Initializes a UDP socket and configures server address.
 
void setBlocking (SOCKET socket)
 Sets the given socket to blocking mode.
 
void setNonBlocking (SOCKET socket)
 Sets the given socket to non-blocking mode.
 
void updateServerIPandPort (struct sockaddr_in *serverAddr, char *serverIP, uint16_t serverPort)
 Re-configure server address and port.
 

Detailed Description

Functions related to initializing network ports and setting their properties.

Function Documentation

◆ initializeUDPClientSocket()

int initializeUDPClientSocket ( SOCKET * udpSocket,
struct sockaddr_in * serverAddr,
char * serverIP,
uint16_t port )

Initializes a UDP socket and configures server address.

This function initializes a UDP socket and configures the server address using the provided parameters. It creates a UDP socket, sets it up with the specified server address and port, and assigns the socket to the provided pointer. If any errors occur during socket creation or address configuration, the appropriate error message is printed to stderr, Winsock resources are cleaned up, and the function returns EXIT_FAILURE.

Parameters
udpSocketA pointer to an integer where the UDP socket descriptor will be stored.
serverAddrPointer to the address structure of the server connection (struct sockaddr_in) to configure the socket with.
serverIPThe IP address of the connection (char*; IPv4).
portThe port on the server that will be bound by this UDP socket.
Returns
EXIT_SUCCESS on success, EXIT_FAILURE on failure.

◆ initializeUDPServerSocket()

int initializeUDPServerSocket ( SOCKET * udpSocket,
struct sockaddr_in * serverAddr,
uint16_t port )

Initializes a UDP socket and configures server address.

This function initializes a UDP socket and configures the server address using the provided parameters. It creates a UDP socket, sets it up with the specified server address and port, and assigns the socket to the provided pointer. If any errors occur during socket creation or address configuration, the appropriate error message is printed to stderr, Winsock resources are cleaned up, and the function returns EXIT_FAILURE.

Parameters
udpSocketA pointer to an integer where the UDP socket descriptor will be stored.
serverAddrPointer to the address structure of the server connection (struct sockaddr_in) to configure the socket with.
portThe port on the server that will be bound by this UDP socket.
Returns
EXIT_SUCCESS on success, EXIT_FAILURE on failure.

◆ initializeWinsock()

int initializeWinsock ( )

Initializes Winsock library.

Returns
0 on success, EXIT_FAILURE on failure.

This function initializes the Winsock library. It must be called before any other Winsock functions. If initialization fails, an error message is printed to stderr and the function returns EXIT_FAILURE code.

◆ initSocketAddress()

struct sockaddr_in initSocketAddress ( char * ip_address,
uint16_t port )

Initializes a socket address.

Parameters
ip_addressString containing the allowed connection IP address, or NULL to allow any connection.
portPort over which socket messages will be exchanged for this address structure.
Returns
struct sockaddr_in a data structure containing the namespace-formatted port and IP address.

◆ setBlocking()

void setBlocking ( SOCKET socket)

Sets the given socket to blocking mode.

This function sets the specified socket to blocking mode.

Parameters
socketThe socket to set to blocking mode.

◆ setNonBlocking()

void setNonBlocking ( SOCKET socket)

Sets the given socket to non-blocking mode.

This function sets the specified socket to non-blocking mode.

Parameters
socketThe socket to set to non-blocking mode.

◆ updateServerIPandPort()

void updateServerIPandPort ( struct sockaddr_in * serverAddr,
char * serverIP,
uint16_t serverPort )

Re-configure server address and port.

This function re-configures the server address and port using the provided input IPv4 IP address and port number.

Parameters
serverAddrPointer to the address structure of the server connection (struct sockaddr_in) to configure the socket with.
serverIPThe IP address of the connection (char*; IPv4).
serverPortThe port on the server that will be bound by this UDP socket.