From 6bb9ef9b208689accdd438759bd29125c627ed08 Mon Sep 17 00:00:00 2001 From: Sergiusz Warga Date: Sat, 15 May 2021 20:56:56 +0200 Subject: [PATCH] Introduced Topic-12 --- Topic-12/client.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++ Topic-12/server.c | 16 +++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 Topic-12/client.c create mode 100644 Topic-12/server.c diff --git a/Topic-12/client.c b/Topic-12/client.c new file mode 100644 index 0000000..68b4d1c --- /dev/null +++ b/Topic-12/client.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include + +void run_client(int port, char *msg) { + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + fprintf(stderr, "socket: "); + exit(EXIT_FAILURE); + } + + struct sockaddr_in server_addr; + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(port); + if (connect(sockfd, (const struct sockaddr *)&server_addr, sizeof(server_addr))) { + fprintf(stderr, "connect: "); + exit(EXIT_FAILURE); + } + + int rv = sendto(sockfd, msg, strlen(msg), 0, (const struct sockaddr *)&server_addr, sizeof(server_addr)); + if (rv < 0) { + fprintf(stderr, "sendto: "); + exit(EXIT_FAILURE); + } + + char buffer[100]; + memset(&buffer, 0, sizeof(buffer)); + socklen_t server_addr_len = sizeof(server_addr); + rv = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&server_addr, &server_addr_len); + if (rv < 0) { + fprintf(stderr, "recvfrom: "); + exit(EXIT_FAILURE); + } + + printf("Server reply: %s\n", buffer); + + close(sockfd); +} + +int main(int arc, char *argv[]) { + + run_client(2137, argv[1]); + + return 0; +} \ No newline at end of file diff --git a/Topic-12/server.c b/Topic-12/server.c new file mode 100644 index 0000000..82b79ee --- /dev/null +++ b/Topic-12/server.c @@ -0,0 +1,16 @@ +void run_server(int port) { + +} + +int main(int argc, char *argv[]) { + // Socket + // setsockopt + // Bind + // listen() + // accept + // send/recv + + run_server(2137); + + return 0; +} \ No newline at end of file