Devember: Half-closing the socket

This post is part of the Devember collection, useful for the study of the Socket API.

Devember: Half-closing the socket

In the wake of the previous post, another way to improve the robustness of the client code is by shutting down the write half of the connection once all reading operations from standard input end. This is performed by calling the shutdown function:

char stdineof = 0;
// ...
if (FD_ISSET(fileno(fp), &readset)) {
	if (fgets(sendline, MAXLINE, fp) == NULL) {
		stdineof = 1;
		shutdown(sockfd, SHUT_WR);
		FD_CLR(fileno(fp), &readset);
		continue;
	}
	if (exso_writen(sockfd, sendline, strlen(sendline)) < 0) {
		perror("exso_writen");
		return -1;
	}
}

The complete code can be found on this repository.

10 December 2015