aboutsummaryrefslogtreecommitdiff
path: root/reference.c
diff options
context:
space:
mode:
Diffstat (limited to 'reference.c')
-rw-r--r--reference.c39
1 files changed, 38 insertions, 1 deletions
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 <sys/types.h>
+#include <sys/socket.h>
+
+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