server1
This commit is contained in:
BIN
server1/.cache/clangd/index/main.c.2CEDD008559E27BF.idx
Normal file
BIN
server1/.cache/clangd/index/main.c.2CEDD008559E27BF.idx
Normal file
Binary file not shown.
BIN
server1/.cache/clangd/index/sockets.h.75AABA95DBE9AAA6.idx
Normal file
BIN
server1/.cache/clangd/index/sockets.h.75AABA95DBE9AAA6.idx
Normal file
Binary file not shown.
4
server1/.clangd
Normal file
4
server1/.clangd
Normal file
@@ -0,0 +1,4 @@
|
||||
CompileFlags:
|
||||
Add:
|
||||
- -Isrc
|
||||
- -std=c11
|
||||
69
server1/Makefile
Normal file
69
server1/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
server1/compile_commands.json
Normal file
21
server1/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"
|
||||
}
|
||||
]
|
||||
56
server1/src/main.c
Normal file
56
server1/src/main.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main() {
|
||||
int 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 = INADDR_ANY;
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(8888);
|
||||
|
||||
// connect to remote server
|
||||
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
||||
puts("bind error\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
puts("bind done\n");
|
||||
|
||||
// listen for connections
|
||||
listen(socket_desc, 3);
|
||||
|
||||
// accept incoming connections
|
||||
puts("waiting for connections...");
|
||||
int c = sizeof(struct sockaddr_in);
|
||||
struct sockaddr_in client;
|
||||
int new_socket;
|
||||
while (new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c)) {
|
||||
puts("connection accepted\n");
|
||||
|
||||
char *client_ip = inet_ntoa(client.sin_addr);
|
||||
int client_port = ntohs(client.sin_port);
|
||||
printf("client address: %s:%d\n", client_ip, client_port);
|
||||
|
||||
char *message = "Hello Client\n";
|
||||
write(new_socket, message, strlen(message));
|
||||
}
|
||||
|
||||
if (new_socket < 0) {
|
||||
perror("accept failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
close(socket_desc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user