Skip to content

Instantly share code, notes, and snippets.

@lettergram
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/49740e2b23ca194abce4 to your computer and use it in GitHub Desktop.
Save lettergram/49740e2b23ca194abce4 to your computer and use it in GitHub Desktop.
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
struct addrinfo directives, *result;
memset(&directives, 0, sizeof(struct addrinfo));
directives.ai_family = AF_INET;
directives.ai_socktype = SOCK_STREAM;
directives.ai_flags = AI_PASSIVE;
/* Translates IP, port, protocal into struct */
if(0 != getaddrinfo(NULL, "8080", &directives, &result))
exit(1);
/* Binds socket to port, so we know where new connections form */
if(bind(sock_fd, result->ai_addr, result->ai_addrlen) != 0)
exit(1);
/* Places socket to "listen" or "wait for stuff" state */
if(listen(sock_fd, 10) != 0)
exit(1);
printf("Waiting for connection on http://localhost:8080 ...\n");
while(1){
/* Accepts Connection */
int client_fd = accept(sock_fd, NULL, NULL);
char buffer[1000];
int len = read(client_fd, buffer, 999);
buffer[len] = '\0';
char * header = "<b>You Connected to the Server!</b></br></br>";
char * body = "Brought to you by: <a href=\"austingwalters.com\">Austin Walters</a>!";
write(client_fd, header, strlen(header));
write(client_fd, body, strlen(body));
printf("\nRead %d chars\n", len);
printf("=== Client Sent ===\n");
printf("%s\n", buffer);
close(client_fd);
sleep(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment