diff options
Diffstat (limited to 'example')
-rw-r--r-- | example/Makefile | 21 | ||||
-rw-r--r-- | example/client.c | 47 | ||||
-rw-r--r-- | example/server.c | 36 |
3 files changed, 104 insertions, 0 deletions
diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..6f0fff8 --- /dev/null +++ b/example/Makefile @@ -0,0 +1,21 @@ +SERVER_NAME=server +SERVER_OBJ=server.o ../src/server.o ../src/tsocket.o ../src/util.o ../src/evloop.o ../src/tpool.o ../src/connection.o ../src/acceptor.o + +CLIENT_NAME=client +CLIENT_OBJ=client.o ../src/util.o + +.PHONY: all +all: $(SERVER_NAME) $(CLIENT_NAME) + +%.o: %.c + $(CC) -c $(CFLAGS) $< -o $@ + +$(SERVER_NAME): $(SERVER_OBJ) + $(CC) -o $@ $^ + +$(CLIENT_NAME): $(CLIENT_OBJ) + $(CC) -o $@ $^ + +.PHONY: clean +clean: + rm -rf $(CLIENT_NAME) $(SERVER_NAME) *.o diff --git a/example/client.c b/example/client.c new file mode 100644 index 0000000..56ba072 --- /dev/null +++ b/example/client.c @@ -0,0 +1,47 @@ +#include <sys/socket.h> +#include <arpa/inet.h> +#include <strings.h> +#include <sys/types.h> +#include <unistd.h> +#include <stdbool.h> +#include <stdio.h> + +#include "../src/util.h" + +int main() +{ + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) + panic("socket creation error"); + + struct sockaddr_in serv_addr; + bzero(&serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + serv_addr.sin_port = htons(8888); + + if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) + panic("socket connect error"); + + for (;;) { + char buf[1024]; + bzero(&buf, sizeof(buf)); + scanf("%s", buf); + ssize_t n_write_bytes = write(sockfd, buf, sizeof(buf)); + if (n_write_bytes == -1) { + printf("socket already disconnected, cannot write any more!\n"); + break; + } + + ssize_t n_read_bytes = read(sockfd, buf, sizeof(buf)); + if (n_read_bytes > 0) { + printf("message from server: %s\n", buf); + } else if (n_read_bytes == 0) { + printf("server socket disconnected!\n"); + break; + } else if (n_read_bytes == -1) { + close(sockfd); + panic("socket read error"); + } + } +} diff --git a/example/server.c b/example/server.c new file mode 100644 index 0000000..fe8cba9 --- /dev/null +++ b/example/server.c @@ -0,0 +1,36 @@ +#include <errno.h> +#include <unistd.h> +#include <stdio.h> + +#include "../src/server.h" +#include "../src/connection.h" + +#define READ_BUFFER_SIZE 1024 + +static void echo(connection_t *conn) +{ + char buf[READ_BUFFER_SIZE]; + ssize_t n_read_bytes; + int fd = connection_fd(conn); + + for (;;) { + n_read_bytes = read(fd, buf, sizeof(buf)); + if (n_read_bytes > 0) { + printf("message from conn %d: %s\n", fd, buf); + write(fd, buf, sizeof(buf)); + } else if (n_read_bytes == 0) { + printf("conn %d disconnected\n", fd); + return; + } else if (n_read_bytes == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) + break; + } + } +} + +int main() +{ + server_t *serv = server_create(); + server_on_connect(serv, echo); + server_run(serv); +} |