hostname client, ip client
This commit is contained in:
BIN
client1/.cache/clangd/index/main.c.2CEDD008559E27BF.idx
Normal file
BIN
client1/.cache/clangd/index/main.c.2CEDD008559E27BF.idx
Normal file
Binary file not shown.
BIN
client1/.cache/clangd/index/sockets.h.75AABA95DBE9AAA6.idx
Normal file
BIN
client1/.cache/clangd/index/sockets.h.75AABA95DBE9AAA6.idx
Normal file
Binary file not shown.
4
client1/.clangd
Normal file
4
client1/.clangd
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
CompileFlags:
|
||||||
|
Add:
|
||||||
|
- -Isrc
|
||||||
|
- -std=c11
|
||||||
69
client1/Makefile
Normal file
69
client1/Makefile
Normal file
@@ -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
|
||||||
21
client1/compile_commands.json
Normal file
21
client1/compile_commands.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
49
client1/src/main.c
Normal file
49
client1/src/main.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include "sockets.h"
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
16
client1/src/sockets.h
Normal file
16
client1/src/sockets.h
Normal file
@@ -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
|
||||||
|
};
|
||||||
BIN
client2/.cache/clangd/index/main.c.2CEDD008559E27BF.idx
Normal file
BIN
client2/.cache/clangd/index/main.c.2CEDD008559E27BF.idx
Normal file
Binary file not shown.
BIN
client2/.cache/clangd/index/sockets.h.75AABA95DBE9AAA6.idx
Normal file
BIN
client2/.cache/clangd/index/sockets.h.75AABA95DBE9AAA6.idx
Normal file
Binary file not shown.
4
client2/.clangd
Normal file
4
client2/.clangd
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
CompileFlags:
|
||||||
|
Add:
|
||||||
|
- -Isrc
|
||||||
|
- -std=c11
|
||||||
69
client2/Makefile
Normal file
69
client2/Makefile
Normal file
@@ -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
|
||||||
21
client2/compile_commands.json
Normal file
21
client2/compile_commands.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
29
client2/src/main.c
Normal file
29
client2/src/main.c
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user