summaryrefslogtreecommitdiff
path: root/client.c
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-06-01 21:24:56 +0200
committerOskar <[email protected]>2024-06-01 21:24:56 +0200
commit54b7f4b1d69673182c1358d484b35e86111b66be (patch)
tree5a4ece1af78965237a94e3aa9ec805cfcf5ba3b2 /client.c
parentb0d494f15524fb8dc037c1fce2cb72c0556bb36e (diff)
client thing, it only sends a message and exits but i learned how the basics like that work now
Diffstat (limited to 'client.c')
-rw-r--r--client.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/client.c b/client.c
new file mode 100644
index 0000000..3ace3ac
--- /dev/null
+++ b/client.c
@@ -0,0 +1,60 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <stdbool.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+// int listen(int sockfd, int backlog);
+#define __FAIL EXIT_FAILURE
+#define BACKLOG 2
+#define __PORT "62000"
+
+int main () {
+
+ int sockfd = -1;
+ int gai_result;
+ int connect_result = -1;
+ struct addrinfo hints;
+ struct addrinfo *res;
+ struct addrinfo *p;
+ memset(&hints, 0, sizeof(hints));
+ //hints.ai_flags = ;
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+
+ gai_result = getaddrinfo("192.168.1.18", __PORT, &hints, &res);
+ if (gai_result != 0) {
+ fprintf(stderr, "getaddrinfo: %d", gai_result);
+ exit(__FAIL);
+ }
+
+ for (p = res ; p != NULL ; p = p->ai_next) {
+
+ sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
+ if (sockfd == -1) {
+ fprintf(stderr, "socket fail\n");
+ continue;
+ }
+
+ connect_result = connect(sockfd, p->ai_addr, p->ai_addrlen);
+ if (connect_result == -1) {
+ fprintf(stderr, "connect fail\n");
+ continue;
+ }
+
+ }
+ if (connect_result == -1 || sockfd == -1) { exit(__FAIL); }
+
+ char *msg = "Client says hello!\n";
+ size_t len = strlen(msg);
+ send(sockfd, msg, len, 0);
+ freeaddrinfo(res);
+ return 0;
+}