From 3d9c7d4c5ae135ace068ba50f6b3ae971d8e276b Mon Sep 17 00:00:00 2001 From: sotech117 Date: Wed, 20 Sep 2023 23:24:05 -0400 Subject: implement collecting all bits, if they don't come in one message --- protocol.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 protocol.c (limited to 'protocol.c') diff --git a/protocol.c b/protocol.c new file mode 100644 index 0000000..864afd8 --- /dev/null +++ b/protocol.c @@ -0,0 +1,40 @@ +#include +#include + +#include "protocol.h" + +int send_all(int sock, char *buf, int *len) +{ + int total = 0; // how many bytes we've sent + int bytesleft = *len; // how many we have left to send + int n; + + while(total < *len) { + n = send(sock, buf+total, bytesleft, 0); + if (n == -1) { break; } + total += n; + bytesleft -= n; + } + + *len = total; // return number actually sent here + + return n==-1?-1:0; // return -1 on failure, 0 on success +} + +int recv_all(int sock, char *buf, int *len) +{ + int total = 0; // how many bytes we've sent + int bytesleft = *len; // how many we have left to send + int n; + + while(total < *len) { + n = recv(sock, buf+total, bytesleft, 0); + if (n == -1) { break; } + total += n; + bytesleft -= n; + } + + *len = total; // return number actually sent here + + return n==-1?-1:0; // return -1 on failure, 0 on success +} -- cgit v1.2.3-70-g09d2