Files
CgeNet/README.md

107 lines
2.9 KiB
Markdown
Raw Normal View History

2026-08-08 16:00:39 +03:00
# CgeNet - Network Access Library for C
A lightweight, dependencyfree C library providing a consistent interface for
socket operations across POSIX platforms. The library abstracts the lowlevel
socket API and offers a unified, easytouse set of functions for creating,
configuring, and using TCP/UDP sockets.
## Features
- Crossplatform support (Linux, macOS, Windows)
- IPv4 and IPv6 address handling
- TCP and UDP socket types
- Nonblocking and blocking mode control
- Simple polling interface for readable / writable events
- Comprehensive error handling with explicit error codes
- Context pointer attached to each socket for user data
- No dynamic memory allocation required by the user (library manages its own
resources)
## Usage Example
```c
#include "CgeNet.h"
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stddef.h>
int main(void) {
int result;
#ifdef SIGPIPE
signal(SIGPIPE, SIG_IGN);
#endif
if (!CgeNetInit()) {
fprintf(stderr, "Failed to initialize network");
return 1;
}
CgeNetSocket *sock = CgeNetOpen(CGE_NET_IPV4, CGE_NET_TCP, &result);
if (!sock) {
fprintf(stderr, "Failed to open socket: %d\n", result);
CgeNetCleanup();
return 1;
}
CgeNetAddr addr;
if (!CgeNetAddrFromStr("127.0.0.1", 8080, &addr)) {
fprintf(stderr, "Invalid address\n");
CgeNetClose(sock);
CgeNetCleanup();
return 1;
}
if (!CgeNetConnect(sock, &addr)) {
fprintf(stderr, "Connect failed: %d\n", CgeNetErrorCode(sock));
CgeNetClose(sock);
CgeNetCleanup();
return 1;
}
const char *msg = "Hello, server!";
size_t written;
if (!CgeNetWrite(sock, msg, strlen(msg), &written)) {
fprintf(stderr, "Write failed: %d\n", CgeNetErrorCode(sock));
CgeNetClose(sock);
CgeNetCleanup();
return 1;
}
char buffer[256];
size_t actual;
if (CgeNetRead(sock, buffer, sizeof(buffer) - 1, &actual)) {
buffer[actual] = '\0';
printf("Received: %s\n", buffer);
}
CgeNetClose(sock);
CgeNetCleanup();
return 0;
}
```
## Build Systems
- **CMake**: supports Linux, macOS, Windows (MSVC, MinGW)
- **Makefile.posix**: for GCC/Clang on POSIX systems
- **Makefile.mingw**: for MinGW on Windows
- **Makefile.win32**: for MSVC with NMake
Builds a static library. No shared library or external dependencies
## Portability
- **Standard compliance**: written in C89 + `stdint.h`
- **No external dependencies**: only standard C and system socket headers and
libraries are used
- **Signal handling**: Users of the library should ignore `SIGPIPE` to prevent
the process from being terminated when writing to a closed socket. This can be
done once in the application, e.g. `signal(SIGPIPE, SIG_IGN);`
## License
0BSD - a permissive license with no attribution required.