First commit
This commit is contained in:
29
ch1/TCPServerUtility.c
Normal file
29
ch1/TCPServerUtility.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "Practical.h"
|
||||
|
||||
void HandleTCPClient(const int client_sock) {
|
||||
char buffer[BUFSIZE];
|
||||
|
||||
ssize_t num_bytes_rcvd = recv(client_sock, buffer, BUFSIZE, 0);
|
||||
if (num_bytes_rcvd < 0) {
|
||||
DieWithSystemMessage("recv() failed");
|
||||
}
|
||||
|
||||
while (num_bytes_rcvd > 0) {
|
||||
ssize_t num_bytes_sent = send(client_sock, buffer, num_bytes_rcvd, 0);
|
||||
if (num_bytes_sent < 0) {
|
||||
DieWithSystemMessage("send() failed");
|
||||
} else if (num_bytes_sent != num_bytes_rcvd) {
|
||||
DieWithUserMessage("send()", "sent unexpected number of bytes");
|
||||
}
|
||||
|
||||
num_bytes_rcvd = recv(client_sock, buffer, BUFSIZE, 0);
|
||||
if (num_bytes_rcvd < 0) {
|
||||
DieWithSystemMessage("recv() failed");
|
||||
}
|
||||
}
|
||||
|
||||
close(client_sock);
|
||||
}
|
||||
Reference in New Issue
Block a user