implement basic network server
This commit is contained in:
34
tests/tcptest.c
Normal file
34
tests/tcptest.c
Normal 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);
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user