From 053ef4fa8eca86fc3b8a5b81bdd0abe599413c65 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Thu, 14 Sep 2023 16:47:16 -0400 Subject: add notes --- reference.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'reference.c') diff --git a/reference.c b/reference.c index 1f5b74b..8ec6271 100644 --- a/reference.c +++ b/reference.c @@ -69,4 +69,41 @@ getaddrinfo("www.example.com", "http", &hints, &res); // assuming the first one is good (like many of these examples do). // See the section on client/server for real examples. -s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); \ No newline at end of file +s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + +// order is +getaddrinfo(); +socket(); +bind(); +listen(); +/* accept() goes here */ + +#include +#include + +int sendall(int s, 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(s, 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 +} + +char buf[10] = "Beej!"; +int len; + +len = strlen(buf); +if (sendall(s, buf, &len) == -1) { + perror("sendall"); + printf("We only sent %d bytes because of the error!\n", len); +} \ No newline at end of file -- cgit v1.2.3-70-g09d2