https://github.com/potatomaster101/netutil
Some code for socket programming and IO
https://github.com/potatomaster101/netutil
c mit-license networking socket socket-programming
Last synced: 10 months ago
JSON representation
Some code for socket programming and IO
- Host: GitHub
- URL: https://github.com/potatomaster101/netutil
- Owner: PotatoMaster101
- License: mit
- Created: 2019-08-15T13:49:12.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2021-11-26T12:47:59.000Z (over 4 years ago)
- Last Synced: 2025-07-16T07:14:04.710Z (12 months ago)
- Topics: c, mit-license, networking, socket, socket-programming
- Language: C
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NetUtil
Util functions for socket programming in C. Originally used in uni assignments.
## Compile
Compile with `-std=gnu99` flag.
```
gcc *.c -std=gnu99
```
## Example
**Server**:
```c
#include "netutil.h"
// server config
const char port[] = "1234";
int main(void) {
struct addrinfo hints = tcphints(AF_UNSPEC, AI_PASSIVE), *res;
getaddrinfo(NULL, port, &hints, &res);
int sockfd = addrsock(res);
addrbind(sockfd, res);
addrreuse(sockfd);
freeaddrinfo(res);
struct sockaddr_storage remote;
socklen_t rsize = sizeof remote;
listen(sockfd, 10);
int accfd = accept(sockfd, TOSADDR(&remote), &rsize);
// do network stuff with accfd
return 0;
}
```
**Client**:
```c
#include "netutil.h"
// remote server config
const char addr[] = "192.168.1.1";
const char port[] = "1234";
int main(void) {
struct addrinfo hints = tcphints(AF_UNSPEC, AI_PASSIVE), *res;
getaddrinfo(addr, port, &hints, &res);
int sockfd = addrsock(res);
addrconn(sockfd, res);
freeaddrinfo(res);
// do network stuff with sockfd
return 0;
}
```