libflint/tests/tcptest.c

35 lines
1008 B
C

#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);
}