Event-Driven Programming in C++ 1.0
A case study on event-driven programming in C++
Loading...
Searching...
No Matches
io_and_sockets Namespace Reference

Provides cross-platform functionality for monitoring I/O and socket events. More...

Functions

void close_socket (int s)
 Closes a socket on POSIX systems.
 
int runDemo ()
 Run demo for the I/O and socket monitoring example.
 

Detailed Description

Provides cross-platform functionality for monitoring I/O and socket events.

This namespace contains functions and classes that enable the creation and management of a TCP server socket along with the monitoring of console input. It is designed to work on both Windows and POSIX systems.

On POSIX systems, the implementation uses the standard socket APIs and the select() system call to monitor both the server socket and the standard input (STDIN_FILENO). On Windows, Winsock’s select() function is used to monitor the server socket while console input is handled either by polling with _kbhit() or, when in test mode (TEST_MODE defined), by using standard input functions like std::getline().

The primary function provided in this namespace is runDemo(), which sets up the server, enters an event loop, and gracefully terminates when the user inputs the "quit" command.

Function Documentation

◆ close_socket()

void io_and_sockets::close_socket ( int  s)

Closes a socket on POSIX systems.

This function wraps the close() system call to close a file descriptor.

Parameters
sThe socket file descriptor to close.

◆ runDemo()

int io_and_sockets::runDemo ( )

Run demo for the I/O and socket monitoring example.

This function sets up a TCP server socket, binds it to port 12345, and begins listening for incoming connections. It then enters a loop that monitors:

  • The server socket for incoming connection requests.
  • Console input for user commands.

On Windows, the server socket is monitored using Winsock’s select() while console input is polled using _kbhit(). On POSIX systems, the select() system call monitors both the server socket and the standard input file descriptor.

Typing "quit" in the console will terminate the loop and shut down the server.

Returns
int Returns 0 upon successful termination.