Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_bind_exclusive.c File Reference

Regression: a listening bind is EXCLUSIVE on every platform. More...

#include "nilorea/n_log.h"
#include "nilorea/n_network.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for ex_bind_exclusive.c:

Go to the source code of this file.

Functions

static int check_exclusive (int udp)
 Bind a listener, then try to bind the same port again; the second must fail.
int main (void)

Detailed Description

Regression: a listening bind is EXCLUSIVE on every platform.

netw_make_listening and netw_bind_udp set SO_REUSEADDR so a restarted listener can re-take the port it just released. That option means the opposite thing on Windows: it lets an UNRELATED process bind over a socket already holding the port, the second bind succeeds, and which socket receives a given connection is undefined. A program that reports "listening" while another process owns the port is worse than one that refuses to start, so the option is POSIX-only and this example holds that contract in place.

The port is taken from the OS rather than hardcoded, so the check cannot fail because some unrelated service happened to own a fixed number.

Author
Castagnier Mickael

Definition in file ex_bind_exclusive.c.

Function Documentation

◆ check_exclusive()

int check_exclusive ( int udp)
static

Bind a listener, then try to bind the same port again; the second must fail.

Parameters
udpnon-zero to test netw_bind_udp, zero for netw_make_listening.
Returns
0 when the contract holds, 1 otherwise.

Definition at line 28 of file ex_bind_exclusive.c.

References LOG_ERR, LOG_INFO, n_log, netw_bind_udp(), netw_close(), netw_make_listening(), NETWORK_IPV4, and port.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 98 of file ex_bind_exclusive.c.

References check_exclusive(), LOG_INFO, and set_log_level().

Here is the call graph for this function: