while (nbytes < (ssize_t) PACKET_SIZE) {
n = recv(sockfd, rcv_buf, PACKET_SIZE - nbytes, 0);
- if (n <= 0)
+ DIE(n < 0, "recv");
+
+ if (n == 0)
break;
nbytes += n;
}
parse_args(argc, argv);
+ init();
+
listenfd = tcp_listen_connections(cmd_args.listen_port,
DEFAULT_SERVER_BACKLOG);
DIE(listenfd < 0, "tcp_listen_connections");
+new_connection:
sockfd = accept(listenfd, (SSA *) &addr, &addrlen);
DIE(sockfd < 0, "accept");
nbytes = receive_buffer(sockfd);
DIE(nbytes < 0, "receive_buffer");
- if (nbytes == 0)
- break;
+ if (nbytes == 0) {
+ printf("Connection closed.\n");
+ goto new_connection;
+ }
print_buffer_meta(rcv_buf, PACKET_SIZE);
}
close(sockfd);
close(listenfd);
+ cleanup();
+
return 0;
}