TCP/IP Networking
Plywood provides a portable networking API for TCP/IP communication. The API supports both IPv4 and IPv6 addresses.
Before using any networking functions, you must call Network::initialize(). When finished, call Network::shutdown().
IPAddress
Represents an IP address (either IPv4 or IPv6).
u32 | netOrdered[] |
IPVersion | version() const |
bool | isNull() const |
static constexpr IPAddress | localHost(IPVersion ipVersion) |
static constexpr IPAddress | from_ipv4(u32 netOrdered) |
String | toString() const |
static IPAddress | fromString() |
u32 netOrdered[]The raw address bytes in network byte order. For IPv4, only
netOrdered[0]is used.IPVersion version() constReturns
IPVersion::V4orIPVersion::V6.bool isNull() constReturns
trueif this is a null/uninitialized address.static constexpr IPAddress localHost(IPVersion ipVersion)Returns the localhost address (127.0.0.1 for IPv4, ::1 for IPv6).
static constexpr IPAddress from_ipv4(u32 netOrdered)Creates an IPv4 address from a 32-bit value in network byte order.
String toString() constReturns a human-readable string representation of the address.
static IPAddress fromString()Parses an IP address from a string.
Network
The Network class provides static methods for network initialization and connection management.
static void | initialize(IPVersion ipVersion) |
static void | shutdown() |
static TCPListener | bindTcp(u16 port) |
static Owned<TCPConnection> | connectTcp(const IPAddress& address, u16 port) |
static IPAddress | resolveHostName(StringView hostName, IPVersion ipVersion) |
static IPResult | lastResult() |
static void initialize(IPVersion ipVersion)Initializes the networking subsystem. Must be called before any other networking functions. Specify
IPVersion::V4orIPVersion::V6.static void shutdown()Shuts down the networking subsystem and releases resources.
static TCPListener bindTcp(u16 port)Creates a TCP listener bound to the specified port. The listener can accept incoming connections.
static Owned<TCPConnection> connectTcp(const IPAddress& address, u16 port)Establishes a TCP connection to the specified address and port. Returns null on failure.
static IPAddress resolveHostName(StringView hostName, IPVersion ipVersion)Resolves a hostname (e.g., "example.com") to an IP address using DNS.
static IPResult lastResult()Returns the result code from the most recent network operation.
TCPConnection
Represents an established TCP connection to a remote host.
PipeWinsock | inPipe |
PipeWinsock | outPipe |
| - | |
| TCPConnection() |
| ~TCPConnection() |
const IPAddress& | remoteAddress() const |
u16 | remotePort() const |
SOCKET | getHandle() const |
Stream | createInStream() |
Stream | createOutStream() |
PipeWinsock inPipe
PipeWinsock outPipeThe underlying pipe objects for reading and writing. Typically, use
createInStream()andcreateOutStream()instead.TCPConnection()
~TCPConnection()Constructor and destructor. Connections are typically created via
Network::connectTcp()orTCPListener::accept().const IPAddress& remoteAddress() constReturns the IP address of the remote host.
u16 remotePort() constReturns the port number of the remote host.
SOCKET getHandle() constReturns the underlying socket handle. Use with care.
Stream createInStream()Creates a buffered stream for reading data from the connection.
Stream createOutStream()Creates a buffered stream for writing data to the connection.
TCPListener
A TCPListener listens for incoming TCP connections on a specific port.
| TCPListener(SOCKET listenSocket) |
| TCPListener(TCPListener&& other) |
| ~TCPListener() |
TCPListener& | operator=(TCPListener&& other) |
bool | isValid() |
void | endComm() |
void | close() |
Owned<TCPConnection> | accept() |
TCPListener(SOCKET listenSocket)Constructs a listener from a socket handle. Typically created via
Network::bindTcp().TCPListener(TCPListener&& other)Move constructor.
TCPListener& operator=(TCPListener&& other)Move assignment.
bool isValid()Returns
trueif the listener is bound to a valid socket.void endComm()Signals that no more connections will be accepted. Causes any blocking
accept()call to return.void close()Closes the listener socket.
Owned<TCPConnection> accept()Blocks until a client connects, then returns the new connection. Returns null if the listener was closed.
// Simple echo server
Network::initialize(IPVersion::V4);
TCPListener listener = Network::bindTcp(8080);
while (true) {
Owned<TCPConnection> conn = listener.accept();
if (!conn) break;
Stream in = conn->createInStream();
Stream out = conn->createOutStream();
String line = readLine(in);
out.write(line);
out.write("\n");
out.flush();
}
Network::shutdown();