Skip to content

Instantly share code, notes, and snippets.

@nfarring
Created August 24, 2011 22:27
Show Gist options
  • Save nfarring/1169451 to your computer and use it in GitHub Desktop.
Save nfarring/1169451 to your computer and use it in GitHub Desktop.
Templates for C sockets
#include <stdio.h> // PERROR(3)
#include <stdlib.h> // EXIT(3)
#include <sys/socket.h> // BIND(2)
int retval = bind(
/* int sockfd */
/* const struct sockaddr *addr */
/* socklen_t addrlen */
);
if (retval == -1) {
perror("bind");
exit(-1);
}
#include <stdio.h> // PERROR(3)
#include <stdlib.h> // EXIT(3)
#include <sys/socket.h> // SOCKET(2)
// Raw Ethernet IPv6
int sockfd = socket(
/* int domain */ AF_PACKET,
/* int type */ SOCK_RAW,
/* int protocol */ 0x0800
);
if (sockfd == -1) {
perror("socket");
exit(-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment