summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/ex_20161004
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2023-01-07 18:51:03 +0100
committerSanto Cariotti <santo@dcariotti.me>2023-01-07 18:51:03 +0100
commit9240342b366db9999f11659a1f1c396ba418ad78 (patch)
treef34e9259cbb6f8d4bcdea234e4b2c927950411cc /Year_3/TSDWL/ex_20161004
parentba36beaec6d37d26b075d96e58aad73151d6d39e (diff)
Adds
Diffstat (limited to 'Year_3/TSDWL/ex_20161004')
-rw-r--r--Year_3/TSDWL/ex_20161004/socket.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/Year_3/TSDWL/ex_20161004/socket.c b/Year_3/TSDWL/ex_20161004/socket.c
new file mode 100644
index 0000000..7d018c8
--- /dev/null
+++ b/Year_3/TSDWL/ex_20161004/socket.c
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <string.h>
+#include <arpa/inet.h>
+
+char*
+check_string(char buffer[1024], int n)
+{
+ char* string = "yes";
+ int i;
+
+ for(i = 0; i < n; ++i) {
+ if (buffer[i] != 'V' && buffer[i] != 'F') {
+ strcpy(string, "no");
+ break;
+ }
+ }
+
+ return string;
+}
+
+int
+main(int argc, char* argv[])
+{
+ int sockfd, clientfd;
+ struct sockaddr_in addr;
+ socklen_t addrlen;
+ char buffer[1024];
+ char* result;
+ int n;
+
+ if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
+ perror("socket");
+ exit(1);
+ }
+
+ addr.sin_addr.s_addr = INADDR_ANY;
+ addr.sin_port = htons(3333);
+ addr.sin_family = AF_INET;
+
+ 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");
+ exit(1);
+ }
+
+ if ((n = read(clientfd, buffer, sizeof(buffer))) == -1) {
+ perror("read");
+ goto clientclose;
+ }
+
+ result = check_string(buffer, n);
+ printf("%s\n", result);
+
+clientclose:
+ close(clientfd);
+ }
+
+ close(sockfd);
+ return 0;
+}