Implement Server #1

Merged
eburk merged 8 commits from tcpsrv into master 2024-07-09 21:03:23 +00:00
5 changed files with 43 additions and 14 deletions
Showing only changes of commit c52712471f - Show all commits

View File

@ -14,15 +14,11 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y libbsd-dev cmake build-essential
sudo apt-get install -y libbsd-dev build-essential
- name: Build and test
run: |
mkdir build
cd build
cmake ..
make
./tests
make tests
docs:
runs-on: ubuntu-latest

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ test
tcptest
testrunner
.idea
netmanual

View File

@ -1,4 +1,4 @@
.PHONY : clean tests all
.PHONY : clean tests all manual
CFLAGS = -std=c99 -Iinclude -pedantic
WARNINGS= -Wall -Wextra
@ -30,6 +30,7 @@ clean:
rm -f $(TARGET)
rm -f testrunner
rm -f tcptest
rm -f netmanual
rm -f compile_commands.json
tests:
@ -37,3 +38,5 @@ tests:
cc $(CFLAGS) -o tcptest tests/tcptest.c src/*.c
./run_tests.sh
manual:
cc $(CFLAGS) -o netmanual tests/netmanual.c src/*.c

View File

@ -1,4 +1,5 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
@ -117,9 +118,22 @@ void handler_tcp_echo(Server *s) {
// Start child process
if (!fork()) {
close(s->fd); // Child doesn't need the initial socket
if (send(new_fd, "Hello, world!", 13, 0) == -1) {
fprintf(stderr, "Failed to send hello world\n");
char recv_buf[256];
int r = recv(new_fd, recv_buf, 256, 0);
if (r == -1) {
fprintf(stderr, "Failed to recv. Errno: %d\n", errno);
goto CHILD_END;
} else if (r == 0) {
fprintf(stderr, "Client closed connection\n");
goto CHILD_END;
}
if (send(new_fd, recv_buf, strlen(recv_buf), 0) == -1) {
fprintf(stderr, "Failed to send echo\n");
}
CHILD_END:
close(new_fd);
_exit(0);
}

15
tests/netmanual.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include "lfnetwork.h"
int main(int argc, char **argv) {
Server *server = new_server(SERVERTYPE_TCP, "18632", handler_tcp_echo);
serve(server, DEFAULT_BACKLOG);
delete_server(server);
}