From a7ff320857f6007ab2aca8729cd55d205fc9d891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hatvani=20Tam=C3=A1s?= Date: Mon, 9 Feb 2026 08:30:34 +0100 Subject: [PATCH] hostname client, ip client --- .../clangd/index/main.c.2CEDD008559E27BF.idx | Bin 0 -> 628 bytes .../index/sockets.h.75AABA95DBE9AAA6.idx | Bin 0 -> 768 bytes client1/.clangd | 4 + client1/Makefile | 69 ++++++++++++++++++ client1/compile_commands.json | 21 ++++++ client1/src/main.c | 49 +++++++++++++ client1/src/sockets.h | 16 ++++ .../clangd/index/main.c.2CEDD008559E27BF.idx | Bin 0 -> 628 bytes .../index/sockets.h.75AABA95DBE9AAA6.idx | Bin 0 -> 768 bytes client2/.clangd | 4 + client2/Makefile | 69 ++++++++++++++++++ client2/compile_commands.json | 21 ++++++ client2/src/main.c | 29 ++++++++ 13 files changed, 282 insertions(+) create mode 100644 client1/.cache/clangd/index/main.c.2CEDD008559E27BF.idx create mode 100644 client1/.cache/clangd/index/sockets.h.75AABA95DBE9AAA6.idx create mode 100644 client1/.clangd create mode 100644 client1/Makefile create mode 100644 client1/compile_commands.json create mode 100644 client1/src/main.c create mode 100644 client1/src/sockets.h create mode 100644 client2/.cache/clangd/index/main.c.2CEDD008559E27BF.idx create mode 100644 client2/.cache/clangd/index/sockets.h.75AABA95DBE9AAA6.idx create mode 100644 client2/.clangd create mode 100644 client2/Makefile create mode 100644 client2/compile_commands.json create mode 100644 client2/src/main.c diff --git a/client1/.cache/clangd/index/main.c.2CEDD008559E27BF.idx b/client1/.cache/clangd/index/main.c.2CEDD008559E27BF.idx new file mode 100644 index 0000000000000000000000000000000000000000..c00747310dd1316e647b1d2eb475df70b6a6e9c5 GIT binary patch literal 628 zcmWIYbaTsLVqkDi@vO*AElFfyU|r#l@5(QX&xlN0d(F50mUotiQ26>IPmjF4ufSuN$1>gS1NYmtU5O0E zmAOgsK>z&>e^vc(DSsOegD3-o02>P%2bciLKtNGyTJbX=^V{`<9AzgbvNFmDa0qco zz_cy;@t9NGor{x^M?ipEKo}-`;VEzY;aL+w!u;I)Vld$=d)fUT+g}m`3Uf$uC_t@X z2yC9lBPEsQ1`<}{(1i)FJj=H-C1z0tR2XImjAoQVajC4yw{s@TR<8m&P(W5d4W?>u z^VhApuJ6wR4G|C#kb((Y2^^Zcb!PcLplWUoZa$cBQEE;i$ok@<MOIQ1nqD&9~Rp{iHr9yu16-B4hsNojdR3{@$@y>S3OWD0A(M6AF(G zOboDmG&RjDggKMm{{3ag}?-ZBm)CbGsAL?JK?Jq1}pP1Z~}F+aj}Vj37GCv ztRlasZ(85M#=s2J&A`sU2PPOe!DchKCv7%Ozw~4g9|Jo-0~4<>uMC)g=?=+Ckm;Y6 zxR{TD4JggW$tM6NVA2Mz@|{JmOE>W`Z~^UR7iO0R6EJBlm6PD zf(e-P-sZ1cb6wv9rCC`Rm>7i^CBOtsdVhHThfjqI-?1_90bS0+&LadSU@k98O)LHa z^aL#Q7`ecq1`9114HO0i8N|1U&;kmBf(oVz7D`N9kkElD=V9XD<`9C}01GRiUQj^6 z+y@IIpfD(aV8XDl0Sbcx24)j1On}0m0D%d^!U8A^3J92cV1@yOK>+~sMNw)_B8XjF Wlw8aWq?wqiuO`*ou!y8EFaQ8wYt@(l literal 0 HcmV?d00001 diff --git a/client1/.clangd b/client1/.clangd new file mode 100644 index 0000000..a634458 --- /dev/null +++ b/client1/.clangd @@ -0,0 +1,4 @@ +CompileFlags: + Add: + - -Isrc + - -std=c11 diff --git a/client1/Makefile b/client1/Makefile new file mode 100644 index 0000000..83eec9f --- /dev/null +++ b/client1/Makefile @@ -0,0 +1,69 @@ +APPNAME = networkc +SRCDIR = src +BUILDDIR = build +VENDORDIR = vendor + +target ?= linux +profile ?= debug +valgrind ?= false + +ifeq ($(target), windows) + CC = x86_64-w64-mingw32-gcc + EXT = .exe + + OPT_FLAGS = -O2 -DNDEBUG + + INCLUDES = -I$(SRCDIR) + LIBS = + + # Command to copy DLL after build + POST_BUILD_CMD = #@cp .. $(BUILDDIR); @echo "Build complete." + +else + CC = gcc + EXT = + + INCLUDES = -I$(SRCDIR) + LIBS = + POST_BUILD_CMD = @echo "Build complete." + + # Profile Logic + ifeq ($(profile), release) + OPT_FLAGS = -O3 -DNDEBUG -march=native + else + OPT_FLAGS = -O0 -g -DDEBUG -Wall -Wextra + endif +endif + +# Combine Flags +CFLAGS = -Wall $(OPT_FLAGS) $(INCLUDES) + +SRC = $(shell find $(SRCDIR) -name "*.c") +OBJ = $(SRC:$(SRCDIR)/%.c=$(BUILDDIR)/%.o) + +all: $(BUILDDIR)/$(APPNAME)$(EXT) + +$(BUILDDIR)/$(APPNAME)$(EXT): $(OBJ) + @echo "Linking $@ (Target: $(target), Profile: $(profile))" + @mkdir -p $(dir $@) + $(CC) $(OBJ) -o $@ $(LIBS) + $(POST_BUILD_CMD) + +$(BUILDDIR)/%.o: $(SRCDIR)/%.c + @echo "Compiling $<" + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -rf $(BUILDDIR) + +ifeq ($(valgrind), true) + RUN_CMD = valgrind --leak-check=full ./$(BUILDDIR)/$(APPNAME)$(EXT) +else + RUN_CMD = ./$(BUILDDIR)/$(APPNAME)$(EXT) +endif + +run: all + $(RUN_CMD) + +.PHONY: all clean run diff --git a/client1/compile_commands.json b/client1/compile_commands.json new file mode 100644 index 0000000..34c83bd --- /dev/null +++ b/client1/compile_commands.json @@ -0,0 +1,21 @@ +[ + { + "file": "src/main.c", + "arguments": [ + "gcc", + "-Wall", + "-O0", + "-g", + "-DDEBUG", + "-Wall", + "-Wextra", + "-Isrc", + "-c", + "src/main.c", + "-o", + "build/main.o" + ], + "directory": "/home/tom/Dev/networkc", + "output": "build/main.o" + } +] \ No newline at end of file diff --git a/client1/src/main.c b/client1/src/main.c new file mode 100644 index 0000000..214be95 --- /dev/null +++ b/client1/src/main.c @@ -0,0 +1,49 @@ +#include "sockets.h" + +#include +#include +#include +#include +#include + +int main() { + int socket_desc; + socket_desc = socket(AF_INET, SOCK_STREAM, 0); + + if (socket_desc == -1) { + printf("Could not create a socket!"); + return 1; + } + + struct sockaddr_in server; + server.sin_addr.s_addr = inet_addr("142.250.180.206"); + server.sin_family = AF_INET; + server.sin_port = htons(80); + + // connect to remote server + if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) { + puts("connection error\n"); + return 1; + } + + puts("connected\n"); + + const char *message = "GET / HTTP/1.1\r\n\r\n"; + if (send(socket_desc, message, strlen(message), 0) < 0) { + puts("send failed\n"); + return 1; + } + + puts("Data sent\n"); + + char server_reply[2000]; + if (recv(socket_desc, server_reply, 2000, 0) < 0) { + puts("recv failed\n"); + } + puts("Reply:\n"); + puts(server_reply); + + close(socket_desc); + + return 0; +} diff --git a/client1/src/sockets.h b/client1/src/sockets.h new file mode 100644 index 0000000..e5b6649 --- /dev/null +++ b/client1/src/sockets.h @@ -0,0 +1,16 @@ +struct in_address { + unsigned long s_addr; // load with inet_pton() +}; + +// IPv4 AF_INET sockets: +struct sockaddress_in { + short sin_family; // ipv4 or v6 + unsigned short sin_port; // htons(3490) + struct in_address sin_addr; + char sin_zero[8]; +}; + +struct sockaddress { + unsigned short sa_family; // address family AF_XXX + char sa_data[14]; // 14 bytes of protocal address +}; diff --git a/client2/.cache/clangd/index/main.c.2CEDD008559E27BF.idx b/client2/.cache/clangd/index/main.c.2CEDD008559E27BF.idx new file mode 100644 index 0000000000000000000000000000000000000000..c00747310dd1316e647b1d2eb475df70b6a6e9c5 GIT binary patch literal 628 zcmWIYbaTsLVqkDi@vO*AElFfyU|r#l@5(QX&xlN0d(F50mUotiQ26>IPmjF4ufSuN$1>gS1NYmtU5O0E zmAOgsK>z&>e^vc(DSsOegD3-o02>P%2bciLKtNGyTJbX=^V{`<9AzgbvNFmDa0qco zz_cy;@t9NGor{x^M?ipEKo}-`;VEzY;aL+w!u;I)Vld$=d)fUT+g}m`3Uf$uC_t@X z2yC9lBPEsQ1`<}{(1i)FJj=H-C1z0tR2XImjAoQVajC4yw{s@TR<8m&P(W5d4W?>u z^VhApuJ6wR4G|C#kb((Y2^^Zcb!PcLplWUoZa$cBQEE;i$ok@<MOIQ1nqD&9~Rp{iHr9yu16-B4hsNojdR3{@$@y>S3OWD0A(M6AF(G zOboDmG&RjDggKMm{{3ag}?-ZBm)CbGsAL?JK?Jq1}pP1Z~}F+aj}Vj37GCv ztRlasZ(85M#=s2J&A`sU2PPOe!DchKCv7%Ozw~4g9|Jo-0~4<>uMC)g=?=+Ckm;Y6 zxR{TD4JggW$tM6NVA2Mz@|{JmOE>W`Z~^UR7iO0R6EJBlm6PD zf(e-P-sZ1cb6wv9rCC`Rm>7i^CBOtsdVhHThfjqI-?1_90bS0+&LadSU@k98O)LHa z^aL#Q7`ecq1`9114HO0i8N|1U&;kmBf(oVz7D`N9kkElD=V9XD<`9C}01GRiUQj^6 z+y@IIpfD(aV8XDl0Sbcx24)j1On}0m0D%d^!U8A^3J92cV1@yOK>+~sMNw)_B8XjF Wlw8aWq?wqiuO`*ou!y8EFaQ8wYt@(l literal 0 HcmV?d00001 diff --git a/client2/.clangd b/client2/.clangd new file mode 100644 index 0000000..a634458 --- /dev/null +++ b/client2/.clangd @@ -0,0 +1,4 @@ +CompileFlags: + Add: + - -Isrc + - -std=c11 diff --git a/client2/Makefile b/client2/Makefile new file mode 100644 index 0000000..83eec9f --- /dev/null +++ b/client2/Makefile @@ -0,0 +1,69 @@ +APPNAME = networkc +SRCDIR = src +BUILDDIR = build +VENDORDIR = vendor + +target ?= linux +profile ?= debug +valgrind ?= false + +ifeq ($(target), windows) + CC = x86_64-w64-mingw32-gcc + EXT = .exe + + OPT_FLAGS = -O2 -DNDEBUG + + INCLUDES = -I$(SRCDIR) + LIBS = + + # Command to copy DLL after build + POST_BUILD_CMD = #@cp .. $(BUILDDIR); @echo "Build complete." + +else + CC = gcc + EXT = + + INCLUDES = -I$(SRCDIR) + LIBS = + POST_BUILD_CMD = @echo "Build complete." + + # Profile Logic + ifeq ($(profile), release) + OPT_FLAGS = -O3 -DNDEBUG -march=native + else + OPT_FLAGS = -O0 -g -DDEBUG -Wall -Wextra + endif +endif + +# Combine Flags +CFLAGS = -Wall $(OPT_FLAGS) $(INCLUDES) + +SRC = $(shell find $(SRCDIR) -name "*.c") +OBJ = $(SRC:$(SRCDIR)/%.c=$(BUILDDIR)/%.o) + +all: $(BUILDDIR)/$(APPNAME)$(EXT) + +$(BUILDDIR)/$(APPNAME)$(EXT): $(OBJ) + @echo "Linking $@ (Target: $(target), Profile: $(profile))" + @mkdir -p $(dir $@) + $(CC) $(OBJ) -o $@ $(LIBS) + $(POST_BUILD_CMD) + +$(BUILDDIR)/%.o: $(SRCDIR)/%.c + @echo "Compiling $<" + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -rf $(BUILDDIR) + +ifeq ($(valgrind), true) + RUN_CMD = valgrind --leak-check=full ./$(BUILDDIR)/$(APPNAME)$(EXT) +else + RUN_CMD = ./$(BUILDDIR)/$(APPNAME)$(EXT) +endif + +run: all + $(RUN_CMD) + +.PHONY: all clean run diff --git a/client2/compile_commands.json b/client2/compile_commands.json new file mode 100644 index 0000000..34c83bd --- /dev/null +++ b/client2/compile_commands.json @@ -0,0 +1,21 @@ +[ + { + "file": "src/main.c", + "arguments": [ + "gcc", + "-Wall", + "-O0", + "-g", + "-DDEBUG", + "-Wall", + "-Wextra", + "-Isrc", + "-c", + "src/main.c", + "-o", + "build/main.o" + ], + "directory": "/home/tom/Dev/networkc", + "output": "build/main.o" + } +] \ No newline at end of file diff --git a/client2/src/main.c b/client2/src/main.c new file mode 100644 index 0000000..84e8d91 --- /dev/null +++ b/client2/src/main.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +int main() { + char *hostname = "www.motionweb.hu"; + char ip[100]; + struct hostent *he; + struct in_addr **addr_list; + int i; + + if ((he = gethostbyname(hostname)) == NULL) { + puts("gethostbyname error\n"); + return 1; + } + + // Cast the h_addr_list to in_addr , since h_addr_list also has the ip address in long format only + addr_list = (struct in_addr **)he->h_addr_list; + + for (i = 0; addr_list[i] != NULL; i++) { + // return the first one + strcpy(ip, inet_ntoa(*addr_list[i])); + } + printf("%s resolved to : %s", hostname, ip); + + return 0; +}