Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Created March 20, 2017 05:08
Show Gist options
  • Save StevenJL/7a36f821b8f7eb3e90a79b6b088391ca to your computer and use it in GitHub Desktop.
Save StevenJL/7a36f821b8f7eb3e90a79b6b088391ca to your computer and use it in GitHub Desktop.
sock_programming/bind
int tcp_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// AF_INET to create a IPv4 socket
// SOCK_STREAM to use a stream-based protocol
// IPPROTO_TCP to use the TCP protocol
// we create a struct to store ip address data;
struct sockaddr_in address_info;
// zero out the data in the struct
memset(&address_info, 0, sizeof(address_info));
// set the address family to be an internet address
address_info.sin_family = AF_INET;
// we let the system pick out the internet address by specifying the
// wildcard `INADDR_ANY`
address_info.sin_addr.s_addr = htonl(INADDR_ANY);
// we pick the port we expect incoming connections
in_port_t local_port = 42;
address_info.sin_port = htons(local_port);
// use `bind` to connect the socket with to the address specified in the
// address_info struct. The most common reason for `bind` failing is that
// another socket is already connected to this port.
if (bind(tcp_sock, (struct sockaddr*) &address_info, sizeof(address_info)) < 0) {
fputs("bind() failed", stderr);
exit(1);
}
int max_connections = 10;
// `listen()` lets the system know to allow incoming connections
if (listen(tcp_sock, max_connections) < 0) {
fputs("listen() failed", stderr);
exit(1);
}
struct sockaddr_in incoming_addr;
socklen_t incoming_addr_len = sizeof(incoming_addr);
// infinite loop
for (;;) {
// `accept()` is a blocked until an incoming connection is made. At that point, accept returns
// a descriptor for a new socket, which is connected to a remote socket. Note upon success, this also fils the incoming_addr struct with the ip address and port of the remote connection.
int remote_sock = accept(tcp_sock, (struct sockaddr *) &incoming_addr, &incoming_addr_len);
if (remote_sock < 0){
fputs("accept() failed", stderr);
exit(1);
}
char remote_address_str[INET_ADDRSTRLEN];
// `inet_ntop` takes an address struct and writes the ip address from binary form into dotted-quad string format.
// `ntohs` stands for network short to host, which converts a converts a port number from network byte-order
// to host byte-order (more on this later).
if (inet_ntop(AF_INET, &incoming_addr.sin_addr.s_addr, remote_address_str, sizeof(remote_address_str)) != NULL)
printf("Handling remote connection. remote ip and port: %s/%d\n", remote_address_str, ntohs(incoming_addr.sin_port));
else
printf("Unable to get client address");
int const BUFSIZE = 256;
char buffer[BUFSIZE];
// `recv()` is blocking until something is received or the connection is closed.
ssize_t num_bytes_received = recv(remote_sock, buffer, BUFSIZE, 0);
if (num_bytes_received < 0) {
fputs("recv() failed", stderr);
exit(1);
} else {
// handle the data in buffer
}
close(remote_sock);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment