Added clarifying comments

This commit is contained in:
Christopher Ramey 2015-02-04 00:30:24 +00:00 committed by cdramey
parent a2c745fdce
commit 36b884e0ba

View File

@ -82,6 +82,7 @@ int main(int argc, char *argv[])
} }
} }
// Establish the socket
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1){ if(sockfd == -1){
fprintf(stderr, "%s: cannot open socket - %s\n", fprintf(stderr, "%s: cannot open socket - %s\n",
@ -90,6 +91,8 @@ int main(int argc, char *argv[])
goto shutdown_error; goto shutdown_error;
} }
// This sets REUSE on the socket so it's easily reallocated if
// this program dies
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
memset(&sock, 0, sizeof(sock)); memset(&sock, 0, sizeof(sock));
@ -97,13 +100,15 @@ int main(int argc, char *argv[])
sock.sin_addr.s_addr = addr; sock.sin_addr.s_addr = addr;
sock.sin_port = htons(port); sock.sin_port = htons(port);
// Bind our socket to the specified address and port
if(bind(sockfd, (struct sockaddr*)&sock, sizeof(sock)) < 0){ if(bind(sockfd, (struct sockaddr*)&sock, sizeof(sock)) < 0){
fprintf(stderr, "%s: cannot bind to port - %s\n", fprintf(stderr, "%s: cannot bind - %s\n",
exec_name, strerror(errno) exec_name, strerror(errno)
); );
goto shutdown_error; goto shutdown_error;
} }
// Listen on port
if(listen(sockfd, 5) != 0){ if(listen(sockfd, 5) != 0){
fprintf(stderr, "%s: cannot listen - %s\n", fprintf(stderr, "%s: cannot listen - %s\n",
exec_name, strerror(errno) exec_name, strerror(errno)