diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-11-01 22:40:58 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-11-01 22:40:58 +0100 |
commit | 5496448b6a2a06e774ad93918b18415972e33fdc (patch) | |
tree | 770ce3d413bb3000c79e6b14360a49af380bea25 /Year_3 | |
parent | a1ebb23463f0859013fd03bcf3e0c2234ebb3322 (diff) |
tsdwl: add socket tcp client/server
Diffstat (limited to 'Year_3')
-rw-r--r-- | Year_3/TSDWL/socket_tcp/client.c | 59 | ||||
-rw-r--r-- | Year_3/TSDWL/socket_tcp/server.c | 77 |
2 files changed, 136 insertions, 0 deletions
diff --git a/Year_3/TSDWL/socket_tcp/client.c b/Year_3/TSDWL/socket_tcp/client.c new file mode 100644 index 0000000..a4056db --- /dev/null +++ b/Year_3/TSDWL/socket_tcp/client.c @@ -0,0 +1,59 @@ +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +int +main(int argc, char* argv[]) +{ + int sockfd; + struct sockaddr_in addr; + socklen_t addrlen; + char buffer[1024]; + int nbytes; + + if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { + perror("Error with socket creation"); + exit(1); + } + + addr.sin_port = htons(atoi(argv[1])); + addr.sin_family = AF_INET; + if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) == -1) { + perror("Can't convert IP in bytes"); + exit(1); + } + + addrlen = sizeof(addr); + + if ((connect(sockfd, (struct sockaddr*)&addr, addrlen)) == -1) { + perror("Error with the connection"); + exit(1); + } + + nbytes = 0; + do { + if (strncmp(buffer, "quit", 4) == 0) + break; + + if (nbytes == -1) { + perror("Error sending data"); + break; + } + + fgets(buffer, 1024, stdin); + + } while ((nbytes = send(sockfd, buffer, strlen(buffer), 0)) > 0); + + if (recv(sockfd, buffer, 1024, 0) > 0) { + printf("%s", buffer); + } + + close(sockfd); + + return 0; +} diff --git a/Year_3/TSDWL/socket_tcp/server.c b/Year_3/TSDWL/socket_tcp/server.c new file mode 100644 index 0000000..7e9da88 --- /dev/null +++ b/Year_3/TSDWL/socket_tcp/server.c @@ -0,0 +1,77 @@ +#include <netdb.h> +#include <netinet/in.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +#define PORT 3030 +#define CONNECTIONS_NUM 1 + +int +main(int argc, char* argv[]) +{ + int sockfd, clientfd; + struct sockaddr_in address; + socklen_t addrlen; + int nbytes, ntotbytes; + char buffer[1024]; + + if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { + perror("Error creating a new socket"); + exit(1); + } + + address.sin_family = AF_INET; + address.sin_port = htons(atoi(argv[1])); + address.sin_addr.s_addr = INADDR_ANY; + + addrlen = sizeof(address); + + if (bind(sockfd, (struct sockaddr*)&address, addrlen) == -1) { + perror("Error binding the port"); + exit(1); + } + + if (listen(sockfd, CONNECTIONS_NUM) == -1) { + perror("Can't listen that port"); + exit(1); + } + + while (1) { + if ((clientfd = accept(sockfd, (struct sockaddr*)&address, &addrlen)) == -1) { + perror("Can't accept request from client"); + break; + } + + ntotbytes = 0; + while ((nbytes = recv(clientfd, buffer, 1024, 0)) != 0) { + if (nbytes == -1) { + perror("Error reading the client"); + break; + } + + buffer[nbytes] = '\0'; + if (strncmp(buffer, "quit", 4) == 0) { + break; + } + + printf("%s", buffer); + ntotbytes += nbytes; + } + + sprintf(buffer, "Total bytes read: %d\n", ntotbytes); + + if ((nbytes = send(clientfd, buffer, strlen(buffer), 0)) != strlen(buffer)) { + perror("Error sending back a message"); + } + + close(clientfd); + } + + close(sockfd); + + return 0; +} |