aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuangxiong Lin <[email protected]>2022-12-01 15:45:18 +0800
committerGuangxiong Lin <[email protected]>2022-12-01 15:45:18 +0800
commit5626ba1525179d634676a347b5cd869cc3266090 (patch)
treeb6eb244db5070e7b140bafa36241d2bbbae01845
downloadtinyserver-5626ba1525179d634676a347b5cd869cc3266090.tar.gz
tinyserver-5626ba1525179d634676a347b5cd869cc3266090.tar.bz2
tinyserver-5626ba1525179d634676a347b5cd869cc3266090.zip
First commit
-rw-r--r--.gitignore1
-rw-r--r--Makefile17
-rw-r--r--README.md3
-rw-r--r--client.c17
-rw-r--r--server.c26
5 files changed, 64 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5761abc
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.o
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..7c005c9
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+CC ?= gcc
+
+.PHONY: all
+all: client server
+
+.PHONY: clean
+clean:
+ rm -rf client server
+
+%.o: %.c
+ $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
+
+client: client.o
+ $(CC) -o $@ $<
+
+server: server.o
+ $(CC) -o $@ $<
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8212a8c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# tinyserver
+
+A minimal implementation of web server. The project was created as an exercise in implementing and learning web servers and c/c++.
diff --git a/client.c b/client.c
new file mode 100644
index 0000000..82c35a6
--- /dev/null
+++ b/client.c
@@ -0,0 +1,17 @@
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <strings.h>
+
+int
+main()
+{
+ int sockfd = socket(AF_INET, SOCK_STREAM, 0);
+
+ 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);
+
+ connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
+}
diff --git a/server.c b/server.c
new file mode 100644
index 0000000..9e2fd5c
--- /dev/null
+++ b/server.c
@@ -0,0 +1,26 @@
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <string.h>
+
+int main()
+{
+ int sockfd = socket(AF_INET, SOCK_STREAM, 0);
+
+ 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);
+
+ bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
+
+ listen(sockfd, SOMAXCONN);
+
+ struct sockaddr_in clnt_addr;
+ socklen_t clnt_addr_len = sizeof(clnt_addr);
+ bzero(&clnt_addr, sizeof(clnt_addr));
+ int clnt_sockfd = accept(sockfd, (struct sockaddr *)&clnt_addr, &clnt_addr_len);
+
+ printf("New client fd %d, ip: %s, port: %d\n", clnt_sockfd, inet_ntoa(clnt_addr.sin_addr), ntohs(clnt_addr.sin_port));
+}