implement basic network server
Some checks failed
Test and Deploy / test (push) Failing after 13s
Test and Deploy / docs (push) Has been skipped

This commit is contained in:
2024-07-07 19:50:28 -07:00
parent 074798ed62
commit 1424925cbf
10 changed files with 238 additions and 58 deletions

34
tests/tcptest.c Normal file
View File

@ -0,0 +1,34 @@
#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"
void test_handler(Server *s) {
struct sockaddr_storage client_addr;
socklen_t client_addr_sz = sizeof(client_addr);
int new_fd = accept(s->fd, (struct sockaddr *)&client_addr, &client_addr_sz);
if (new_fd == -1) {
printf("failed to accept. Errno: %d\n", errno);
return;
}
char buf[33];
inet_ntop(client_addr.ss_family, get_in_addr((struct sockaddr *)&client_addr), buf, 32);
printf("Received connection from %s\n", buf);
if (send(new_fd, "TEST SEND", 10, 0) == -1) {
printf("Failed to send hello world. Errno: %d\n", errno);
}
close(new_fd);
}
int main(int argc, char **argv) {
Server *server = new_server(SERVERTYPE_TCP, "18632", test_handler);
serve(server, DEFAULT_BACKLOG);
delete_server(server);
}

View File

@ -4,6 +4,7 @@
#include <unistd.h>
#include "lflinkedlist.h"
#include "lfnetwork.h"
#include "lfset.h"
#include "lfstack.h"
#include "lfbinarytree.h"
@ -426,4 +427,4 @@ int main() {
#endif
return 0;
}
}