diff --git a/test/Makefile b/test/Makefile index 8d698b00..280ee50e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -4,7 +4,7 @@ PROJECT_DIR := $(realpath $(CUR_DIR)/../) # Dependencies: need to be compiled but not to run by any Makefile target TEST_DEPS := dev_null # Tests: need to be compiled and run by test-% target -TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate readdir mkdir link tls pthread +TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate readdir mkdir link tls pthread clone server # Benchmarks: need to be compiled and run by bench-% target BENCHES := spawn_and_exit_latency pipe_throughput diff --git a/test/client/Makefile b/test/client/Makefile new file mode 100644 index 00000000..9e1b6dec --- /dev/null +++ b/test/client/Makefile @@ -0,0 +1,5 @@ +include ../test_common.mk + +EXTRA_C_FLAGS := +EXTRA_LINK_FLAGS := +BIN_ARGS := diff --git a/test/client/main.c b/test/client/main.c new file mode 100644 index 00000000..844a79df --- /dev/null +++ b/test/client/main.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, const char *argv[]) { + const int BUF_SIZE = 0x1000; + const char* message = "Hello world!"; + int ret; + + if (argc != 2) { + printf("usage: ./client \n"); + return -1; + } + + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + printf("create socket error: %s(errno: %d)\n", strerror(errno), errno); + return -1; + } + + struct sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(6666); + + ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr); + if (ret <= 0) { + printf("inet_pton error for %s\n", argv[1]); + return -1; + } + + ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); + if (ret < 0) { + printf("connect error: %s(errno: %d)\n", strerror(errno), errno); + return -1; + } + + printf("send msg to server: %s\n", message); + ret = send(sockfd, message, strlen(message), 0); + if (ret < 0) { + printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); + return -1; + } + + close(sockfd); + return 0; +} \ No newline at end of file diff --git a/test/server/Makefile b/test/server/Makefile new file mode 100644 index 00000000..9e1b6dec --- /dev/null +++ b/test/server/Makefile @@ -0,0 +1,5 @@ +include ../test_common.mk + +EXTRA_C_FLAGS := +EXTRA_LINK_FLAGS := +BIN_ARGS := diff --git a/test/server/main.c b/test/server/main.c new file mode 100644 index 00000000..27094499 --- /dev/null +++ b/test/server/main.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, const char *argv[]) { + const int BUF_SIZE = 0x1000; + int ret; + + int listenfd = socket(AF_INET, SOCK_STREAM, 0); + if (listenfd < 0) { + printf("create socket error: %s(errno: %d)\n", strerror(errno), errno); + return -1; + } + + struct sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(6666); + + ret = bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); + if (ret < 0) { + printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno); + return -1; + } + + ret = listen(listenfd, 10); + if (ret < 0) { + printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno); + return -1; + } + + int client_pid; + char* client_argv[] = {"client", "127.0.0.1"}; + ret = posix_spawn(&client_pid, "client", NULL, NULL, client_argv, NULL); + if (ret < 0) { + printf("spawn client process error: %s(errno: %d)\n", strerror(errno), errno); + return -1; + } + + printf("====== waiting for client's request ======\n"); + int connect_fd = accept(listenfd, (struct sockaddr *) NULL, NULL); + if (connect_fd < 0) { + printf("accept socket error: %s(errno: %d)", strerror(errno), errno); + return -1; + } + char buff[BUF_SIZE]; + int n = recv(connect_fd, buff, BUF_SIZE, 0); + buff[n] = '\0'; + printf("recv msg from client: %s\n", buff); + close(connect_fd); + + close(listenfd); +} \ No newline at end of file