From 9240342b366db9999f11659a1f1c396ba418ad78 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 7 Jan 2023 18:51:03 +0100 Subject: Adds --- Year_3/TSDWL/ex_20161004/socket.c | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Year_3/TSDWL/ex_20161004/socket.c (limited to 'Year_3/TSDWL/ex_20161004/socket.c') 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 +#include +#include +#include +#include +#include + +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; +} -- cgit v1.2.3-18-g5258