30 lines
826 B
C
30 lines
826 B
C
#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);
|
|
}
|