C++ class: Socket¶
Description¶
Noncopyable owner of an IPv4 TCP/UDP socket.
Qualified name : gaspardos::Socket.
Header : bindings/cpp/gaspardos.hpp. C++98.
When to use it¶
Exchange network data while preserving native errors.
Simple program example¶
#include "gaspardos.hpp"
#include <cstdio>
#include <cstring>
int main()
{
gaspardos::Socket socket;
if (socket.open(GO64_DGRAM) < 0)
return 1;
GO64_SocketAddress address = gaspardos::ipv4(127, 0, 0, 1, 19002);
const char message[] = "hello";
int64_t sent = socket.sendTo(message, sizeof(message) - 1, &address);
int64_t closed = socket.close();
return sent == static_cast<int64_t>(sizeof(message) - 1) && closed == 0 ? 0 : 1;
}
Building from Windows¶
Save the program above as build/api-examples/Socket.cpp from the repository root. Create the directory if necessary.
PowerShell — from the repository root:
New-Item -ItemType Directory -Force build/api-examples | Out-Null
& .\build-x64-install\bin\x86_64-gaspardos-gcc.exe -std=gnu1x -O2 -mno-red-zone -c bindings/native/gaspardos_x64_syscall.c -o build/api-examples/syscall.o
if ($LASTEXITCODE -ne 0) { throw "C compilation failed" }
& .\build-x64-install\bin\x86_64-gaspardos-g++.exe -std=c++98 -O2 -Ibindings/cpp build/api-examples/Socket.cpp build/api-examples/syscall.o -o build/api-examples/Socket-x64.elf
if ($LASTEXITCODE -ne 0) { throw "C++ compilation/link failed" }
Run the ELF on GaspardOS, not Windows. The driver supplies CRT and libraries. Prepare the required volumes/services; examples bound their waits and may exit on backpressure rather than retry. Successful compilation does not validate kernel execution.
The headers, implementation files, native wrappers and complete examples are available under Source files.
Class methods¶
Socket()¶
Initial handle is -1.
~Socket()¶
Try close once.
int64_t handle() const¶
Raw handle or -1.
int64_t close()¶
Retain ownership on failure.
int64_t open(uint64_t type = GO64_STREAM, uint64_t protocol = 0)¶
AF_INET, STREAM by default, protocol zero; handle or error.
int64_t bind(const GO64_SocketAddress& a)¶
Bind an 8-byte address; network byte order port.
int64_t listen(int64_t backlog)¶
Listen with the given backlog.
int64_t accept(Socket& peer, GO64_SocketAddress* address = 0)¶
Transfer the new handle to peer; -16 if peer is open; optional address.
int64_t connect(const GO64_SocketAddress& a)¶
Connect to an IPv4 address without DNS.
int64_t send(const void* p, uint64_t n)¶
Bytes sent or error; short transfers remain visible.
int64_t receive(void* p, uint64_t n)¶
Bytes received or error; zero at stream end according to kernel semantics.
int64_t sendTo(const void* p, uint64_t n, const GO64_SocketAddress* a)¶
Datagram to an optional address; zero flags.
int64_t receiveFrom(void* p, uint64_t n, GO64_SocketAddress* a = 0)¶
Receive and optionally fill source; reset length on each call.
int64_t localAddress(GO64_SocketAddress& a)¶
Local address; reset length to sizeof(a).
int64_t peerAddress(GO64_SocketAddress& a)¶
Peer address; reset length to sizeof(a).
Native contract¶
LP64 ABI, 32-bit wchar_t, no packing. Native negative results are not errno. Handle -11 with yield and an application limit. Referenced objects must outlive their users; do not copy handle owners. File uses stdio/errno and invalidates its stream on close, even on error.