It is simple internet interface for easier working with networking in c/c++. The library is written in c. Useful and lightweight. Core idea was assignment at university with networking. All functions operate in non-blocking mode. Includes a simple chat example with a server, allowing many users to chat with each other.
- About
- TOC
- Screenshots
- Supported Platforms
- Installation
- Usage
- Code Snippets
- Full Example
- Features
- License
- Prerequisites
![]() |
| Platform | support |
|---|---|
| Linux | âś… Yes |
| Windows | âś… Yes |
| macOS | ❌ No |
| Android | ❌ No |
| iOS | ❌ No |
- clone repository
git clone https://github.com/Daynlight/Network.git- add subdirectory
add_subdirectory(Network)- add to your project
target_link_libraries(App Network)- clone repository
git clone https://github.com/Daynlight/Network.git- compile via cmake
# you need to change address in Example/Chat/Macro.h
# in default it uses my google cloud server so many
# users can access it
mkdir build
cd build/
cmake ..- run server
./../bin/Server- run client
./../bin/Client-
Server and Client works on non-blocking sockets. You need loop for receiving data.
-
For Video/Music etc. data are send as char buffer and on client side are used in proper way.
-
Best way to use it is making some custom format. for example make first byte 255 or more are always operations register,login etc. then you check this first byte and make operation based on this.
-
Good practice is to optimize buffer before sending it, SIGINT handling. Also good idea is compression and encryption CCrypt.
-
After client connect it sets his socket to non-blocking
void sigint_handler(int sig){
network_client_destroy(&network);
printf("Network destroyed!\n");
running = 0;
exit(EXIT_FAILURE);
};network_server_init- initialize servernetwork_server_destroy- destroy servernetwork_server_listen- tcp listen for users connectionsnetwork_server_read- tcp read data from clientnetwork_server_send- tcp send data to clientnetwork_get_client_ip- get ip from socketnetwork_server_read_from- udp read data from clientnetwork_server_send_to- tcp send data to client
network_client_init- initialize clientnetwork_client_destroy- destroy clientnetwork_client_connect- tcp connect client to servernetwork_client_read- tcp read data from servernetwork_client_send- tcp send data to servernetwork_client_read_from- udp read data from servernetwork_client_send_to- udp send data to servernetwork_client_send_request- udp combination of send and read
network_check_if_empty_message- check if message is not empty
TCP- tcp protocolUDP- udp protocol
- In tcp mode client and server creates connection via
listenandconnect listenreturns user socket you should implement your own client manager- Data are exchanged via
sendandreadfunctions forclientandserver - You need specify maximal message size
- If message is to long then send up to max size
- In udp mode client and server don't create connection so you don't use
listenandconnect - You also don't need to create client manager
- Data are exchanged via
send_toandread_from - For client You can run
requestthat is combination ofsend_toandread_from - For server when
read_fromYou add reference to client and then whensend_touse it - You need specify maximal message size
- If message is to long then send up to max size
sock- socket idserv_addr- sockaddr_in for servermode- contains TCP/UDP mode used in socket
NODATA- send or read no dataDISCONNECT- server or client is disconnectingNOCLIENT- no client to connectDNSERROR- can't find domainSOCKETERROR- can't create socketCONNECTERRORcan't connect to serverERRORCODE- error when function calledSUCCESS- function end up with success
switch (network_server_init(&network, UDP, PORT)){
case ERRORCODE:
printf("Can't Initialize Server\n");
network_server_destroy(&network);
exit(EXIT_FAILURE);
break;
default:
printf("Server Initialized\n");
printf("Server is running\n");
break;
}; network_server_destroy(&network); int socket = network_server_listen(network); char buffer[BUFFER_SIZE + NAMESIZE] = {0};
int valread = network_server_read(&clients.clientData[i].socket_id, buffer, BUFFER_SIZE + NAMESIZE); char message[NAMESIZE + BUFFER_SIZE] = "message";
network_server_send(&(clients->clientData[j].socket_id), message, BUFFER_SIZE + NAMESIZE); char ip[INET_ADDRSTRLEN];
network_get_client_ip(&socket, ip); struct sockaddr client_socket = {0};
char request[BUFFER_SIZE] = {0};
int val = network_server_read_from(&network, &client_socket, request, BUFFER_SIZE); char respond[BUFFER_SIZE] = "respond";
network_server_send_to(&network, &client_socket, respond, BUFFER_SIZE); switch (network_client_init(&network, UDP, ADDR, PORT)){
case CONNECTERROR:
printf("Can't init network\n");
exit(EXIT_FAILURE);
break;
case DNSERROR:
printf("Can't find ip from dns!\n");
exit(EXIT_FAILURE);
break;
default:
printf("Client initalized!\n");
break;
}; network_client_destroy(&network);switch (network_client_connect(&network)){
case CONNECTERROR:
printf("Can't connect to network!\n");
network_client_destroy(&network);
printf("Network destroyed!\n");
exit(EXIT_FAILURE);
break;
default:
printf("Connected to server!\n");
break;
}; char respond[BUFFER_SIZE + NAMESIZE] = {0};
network_client_read(&network, respond, BUFFER_SIZE + NAMESIZE) char send_request[NAMESIZE + BUFFER_SIZE] = "request";
network_client_send(network, send_request, NAMESIZE + BUFFER_SIZE); char respond[NAMESIZE + BUFFER_SIZE] = {0};
int val = network_client_read_from(network_provider, respond, NAMESIZE + BUFFER_SIZE);
for(unsigned int i = 0; i < 100; i++) {
if(val == NODATA) {
sleep(1/20);
val = network_client_read_from(network_provider, respond, NAMESIZE + BUFFER_SIZE);
};
};
printf("%s\n", respond); char request[NAMESIZE + BUFFER_SIZE] = "request";
network_client_send_to(network_provider, request, NAMESIZE + BUFFER_SIZE) char respond[BUFFER_SIZE] = {0};
network_client_send_request(&network, request, BUFFER_SIZE, respond, 20, 100);
printf("%s\n", respond); if(network_check_if_empty_message(request) == NODATA){
printf("Can't send empty message\n");
};- Non-blocking I/O: No need to wait for data to be read or sent; the program can continue executing while awaiting network events.
- Multiple Client Handling: The server can handle multiple client connections simultaneously.
- Protocols: TCP and UDP protocol
- Cross-Platform: Windows and Linux supported.
- Working Example: Ran server in cloud. So many users can connect and chat with each other.
GNU GENERAL PUBLIC LICENSE Version 2, June 1991
- CMake 3.15 or higher
- Git (for cloning with submodules)
