finish network
This commit is contained in:
@ -1,11 +1,3 @@
|
||||
#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) {
|
||||
|
@ -1,34 +0,0 @@
|
||||
#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);
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "lflinkedlist.h"
|
||||
#include "lfnetwork.h"
|
||||
@ -16,6 +18,7 @@
|
||||
|
||||
#if defined(__APPLE__) || defined(__MACH__)
|
||||
#include "lfmacos.h"
|
||||
#include "lfinput.h"
|
||||
#endif /* defined(__APPLE__) || defined(__MACH__) */
|
||||
|
||||
void print_ll(List *list) {
|
||||
@ -396,6 +399,61 @@ void test_parsing() {
|
||||
printf("Passes all parsing tests\n");
|
||||
}
|
||||
|
||||
#define NET_MSG "TEST SEND"
|
||||
|
||||
void tcp_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);
|
||||
assert(new_fd != -1);
|
||||
assert(send(new_fd, NET_MSG, 10, 0) != -1);
|
||||
close(new_fd);
|
||||
}
|
||||
|
||||
void *tcp_server_thread(void *vargp) {
|
||||
Server *server = new_server(SERVERTYPE_TCP, "18632", tcp_test_handler);
|
||||
serve(server, DEFAULT_BACKLOG);
|
||||
delete_server(server);
|
||||
}
|
||||
|
||||
void udp_test_handler(Server *s) {
|
||||
struct sockaddr_storage client_addr;
|
||||
socklen_t client_addr_sz = sizeof(client_addr);
|
||||
char recv_buf[128];
|
||||
|
||||
int r = (int)recvfrom(s->fd, recv_buf, 128, 0, (struct sockaddr*)&client_addr, &client_addr_sz);
|
||||
assert(r > 0);
|
||||
assert(strcmp(recv_buf, NET_MSG) == 0);
|
||||
}
|
||||
|
||||
void *udp_server_thread(void *vargp) {
|
||||
Server *server = new_server(SERVERTYPE_UDP, "18633", udp_test_handler);
|
||||
serve(server, DEFAULT_BACKLOG);
|
||||
delete_server(server);
|
||||
}
|
||||
|
||||
void test_network() {
|
||||
printf("\n--- NETWORK TEST ---\n");
|
||||
pthread_t srv_tid;
|
||||
pthread_create(&srv_tid, NULL, tcp_server_thread, NULL);
|
||||
|
||||
sleep(1);
|
||||
const char *s = capture_system("echo hello | nc localhost 18632", 0);
|
||||
assert(strcmp(s, NET_MSG) == 0);
|
||||
free((char *)s);
|
||||
|
||||
pthread_join(srv_tid, NULL);
|
||||
printf("Passed TCP test\n");
|
||||
|
||||
pthread_create(&srv_tid, NULL, udp_server_thread, NULL);
|
||||
sleep(1);
|
||||
system("echo hello | nc localhost 18633");
|
||||
assert(strcmp(s, NET_MSG) == 0);
|
||||
|
||||
pthread_join(srv_tid, NULL);
|
||||
printf("Passed UDP test\n");
|
||||
}
|
||||
|
||||
#if defined(__APPLE__) || defined(__MACH__)
|
||||
void test_macos() {
|
||||
printf("\n--- macOS TEST ---\n");
|
||||
@ -421,6 +479,7 @@ int main() {
|
||||
test_string();
|
||||
test_crypto();
|
||||
test_parsing();
|
||||
test_network();
|
||||
|
||||
#if defined(__APPLE__) || defined(__MACH__)
|
||||
test_macos();
|
||||
|
Reference in New Issue
Block a user