aboutsummaryrefslogtreecommitdiff
path: root/protocol.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-20 23:24:05 -0400
committersotech117 <michael_foiani@brown.edu>2023-09-20 23:24:05 -0400
commit3d9c7d4c5ae135ace068ba50f6b3ae971d8e276b (patch)
tree5e391492380af5fb351d543d8fd6b17ad5871afe /protocol.c
parent362250d1cb923d9ced8ea51508ace50b76050b06 (diff)
implement collecting all bits, if they don't come in one message
Diffstat (limited to 'protocol.c')
-rw-r--r--protocol.c40
1 files changed, 40 insertions, 0 deletions
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 <sys/types.h>
+#include <sys/socket.h>
+
+#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
+}