Last active
October 17, 2017 00:35
-
-
Save rozanecm/7ff5079e0d81f31125bcf0e6835f0c56 to your computer and use it in GitHub Desktop.
Basic server socket in C, wo error check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BUF_SIZE | |
#define PORT | |
#define MAX_CONNECTION_QUEUE | |
int main(int argc, char* argv[]){ | |
structo addrinfo hints; | |
memset(&hints, 0, sizeof(struct addrinfo)); | |
hints.ai_family = AF_INET; //case for IPv4. Use AF_INET6 for IPv6 or AF_UNSPEC for either | |
hints.ai_socktype = SOCK_STREAM; | |
struct addrinfo *result; | |
getaddrinfo(IP, PORT, &hints, &result); | |
int serverSocketFileDescriptor = socket(&result->family, &result->socktype, &result->protocol); | |
bind(serverSocketFileDescriptor, result->ai_addr, result->ai_addrlen); | |
freeaddrinfo(&result); | |
listen(serverSocketFileDescriptor, MAX_CONNECTION_QUEUE); | |
socket_t *clientSocket; | |
socklen_t cliLength = (socklen_t) sizeof(struct sockadrr_in); | |
int connected_socket = accept(serverSocketFileDescriptor, (struct sockaddr *)&clientSocket, &cliLength); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment