Skip to content

PotatoMaster101/netutil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

#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:

#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;
}

About

Some code for socket programming and IO

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages