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

Demonstrates cross-platform monitoring of both socket events and console I/O. More...

#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <algorithm>
#include <sys/select.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include "logger.hpp"

Namespaces

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

Macros

#define PORT   12345
 The port number on which the TCP server listens.
 

Functions

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

Detailed Description

Demonstrates cross-platform monitoring of both socket events and console I/O.

This example creates a simple TCP server that listens on port 12345 for incoming connections and monitors console input for commands. The example is designed to run on both Windows and POSIX systems, using platform-specific mechanisms:

  • On POSIX systems, the select() system call is used to monitor both the server socket and the standard input (STDIN_FILENO).
  • On Windows, Winsock’s select() function monitors the server socket, while _kbhit() is used in a polling loop to check for console input.

Typing "quit" at the console terminates the server loop.

Note
For Windows, Winsock must be initialized before any socket functions are used, and cleaned up upon termination.

Macro Definition Documentation

◆ PORT

#define PORT   12345

The port number on which the TCP server listens.