summaryrefslogtreecommitdiff
path: root/Year_3
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-11-22 20:54:21 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-11-22 20:54:21 +0100
commit9120732acc43cf3e2a42152331fca16a84226635 (patch)
tree2fb56fd32446491f8c9ded7d7695668084122a06 /Year_3
parent6faea485bf85368311123cbbc8ccae56e0457c4f (diff)
tdwl: add ex
Diffstat (limited to 'Year_3')
-rw-r--r--Year_3/TSDWL/ex_20191218/serverA.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/Year_3/TSDWL/ex_20191218/serverA.c b/Year_3/TSDWL/ex_20191218/serverA.c
new file mode 100644
index 0000000..f13835a
--- /dev/null
+++ b/Year_3/TSDWL/ex_20191218/serverA.c
@@ -0,0 +1,74 @@
+#include <arpa/inet.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+void
+print_buffer_digits(char* buffer, int n)
+{
+ char* ch = buffer;
+ int i;
+
+ for (i = 0; i < n && isdigit(*ch); ++i, ch++) {
+ printf("%c", *ch);
+ }
+ printf("\n");
+}
+
+int
+main(int argc, char* argv[])
+{
+ int sockfd, clientfd;
+ struct sockaddr_in addr;
+ socklen_t addrlen;
+ char buffer[1024];
+ int nbytes, ntotbytes;
+
+ if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
+ perror("socket");
+ exit(1);
+ }
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(7777);
+ addr.sin_addr.s_addr = INADDR_ANY;
+ addrlen = sizeof(addr);
+
+ if (bind(sockfd, (struct sockaddr*)&addr, addrlen) == -1) {
+ perror("bind");
+ exit(1);
+ }
+
+ if (listen(sockfd, 1) == -1) {
+ perror("listen");
+ exit(1);
+ }
+
+ while (1) {
+ if ((clientfd = accept(sockfd, (struct sockaddr*)&addr, &addrlen)) == -1) {
+ perror("accept");
+ continue;
+ }
+
+ ntotbytes = 0;
+
+ while ((nbytes = read(clientfd, buffer, sizeof(buffer) - ntotbytes)) != 0) {
+ if (nbytes == -1) {
+ perror("error reading");
+ goto closeclient;
+ }
+ ntotbytes += nbytes;
+ }
+
+ print_buffer_digits(buffer, ntotbytes);
+
+ closeclient:
+ close(clientfd);
+ }
+
+ close(sockfd);
+ return 0;
+}