Sockets-Windows code
Sockets Repository
Contains Socket, SocketListener, and SocketConnector classes
Quick Status
Code functions correctly
no known defects
Demonstration code
yes
Documentation
yes
Test cases
no, but planned
Static library
no, but planned
Build requires
C++17 option, Windows
Planned design changes
May eventually replace
with Boost::Asio
1.0 Concept
Sockets are an operating system facility for sending streams of bytes through the tcp stack to another
process or another machine. Their behavior is affected by buffers owned by each socket receiver.
If the sender sends N bytes, then:
-
if the receiver's buffer has space for the N bytes, then all are sent. The send function
returns N signalling that all bytes were transfered.
-
if the receiving buffer has M < N bytes available, then M bytes are sent and the send function
returns M signalling that N-M bytes were not sent.
-
if the receiving buffer is full, then the sender blocks until some space becomes available in
the buffer.
Sockets don't know about any data structure other than streams of bytes. So to send a message
you must frame its bytes with some additional decoration so the receiver can detect when an entire
message has been received.
Also, since both sending and receiving are allowed on the same socket connection, some talk protocol
is needed. Without that, both ends may send, eventually resulting in deadlock. Similarly, if both
receive then buffers on both ends will empty, resulting again in deadlock.
The socket library provided in this repository provides means to simplify handling both problems.
See the Resources listed below for ways to fully handle them.
2.0 Design
Fig 2. Sockets Class Diagram
Sockets-Windows provides four classes: Socket, SocketConnecter, SocketListener, and SocketSystem.
An instance of Socket is created by the SocketListener when it establishes a connection. Listener
creates a thread, passing it a client handler object and the new socket. Clients use SocketConnecter
to establish a connection with a listener and to communicate with the remote socket.
Each program that uses Sockets must declare an instance of SocketSystem that loads the windows
socket library on construction and unloads on destruction.
3.0 Resources
Win32Sockets.pdf
Discusses how Win32 Sockets operate.
CommunicationChannel.pdf
This document describes how sockets are used to build a reliable message-based asynchronous
communication channel.
CppCommWithFileXfer
This repository holds code that uses these techniques.