summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2020-05-28 13:59:46 +0200
committerNiklas Halle <niklas@niklashalle.net>2020-05-28 13:59:46 +0200
commit459123928ab5eb8fa3994a7d812ee9ec5838c0f2 (patch)
tree71454aba03f880b9ae7f0c37c2d1fdfdad266865
parentbc48f521390b360e1e1b131fc670cc3d02dcaf89 (diff)
downloadbetriebssysteme-459123928ab5eb8fa3994a7d812ee9ec5838c0f2.tar.gz
betriebssysteme-459123928ab5eb8fa3994a7d812ee9ec5838c0f2.zip
erstes gedöns
-rw-r--r--03_exercise/Makefile1
-rw-r--r--03_exercise/cli/client.c67
-rw-r--r--03_exercise/srv/server.c75
3 files changed, 128 insertions, 15 deletions
diff --git a/03_exercise/Makefile b/03_exercise/Makefile
index 8c02dc9..dbf4b28 100644
--- a/03_exercise/Makefile
+++ b/03_exercise/Makefile
@@ -18,6 +18,7 @@ all: $(TAR)
run: all
srv/server& echo $$! > .srv_pid
+ sleep 1
echo "Client Ready:"
cli/client
kill `cat .srv_pid`
diff --git a/03_exercise/cli/client.c b/03_exercise/cli/client.c
index 48bf724..3cd6f1b 100644
--- a/03_exercise/cli/client.c
+++ b/03_exercise/cli/client.c
@@ -1,13 +1,68 @@
#include <stdio.h>
+#include <string.h>
#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
#define PORT 9000
#define HOST "127.0.0.1"
-int main()
-{
- getc(stdin);
- printf("Client Exit\n");
-
- return 0;
+static inline void die(const char *msg) {
+ perror(msg);
+ exit(-1);
+}
+
+int main() {
+ struct sockaddr_in addr = {
+ .sin_family = AF_INET,
+ .sin_port = htons(PORT),
+ .sin_addr.s_addr = inet_addr(HOST)
+ };
+ char buf[256];
+ int cfd;
+
+ if ((cfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ die("Could not open socket");
+
+ if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
+ die("Could not connect to socket");
+
+ char *line = NULL;
+ size_t cap = 0;
+ __ssize_t length;
+
+ while (1) {
+ if ((length = getline(&line, &cap, stdin)) < 0) {
+ fprintf(stderr, "Failed to read from STDIN");
+ fflush(stderr);
+ exit(-1);
+ }
+
+ if (strspn(line, " \n\t") == strlen(line)) {
+ // skip empty lines - empty being just spaces or tabs
+ continue;
+ }
+
+ line[length - 1] = '\0'; // cut the line feed
+
+ if (strcmp(line, "exit") == 0) {
+ break;
+ }
+
+ strncpy(buf, line, strlen(line));
+
+ if (write(cfd, buf, sizeof(buf)) < 0)
+ die("Could not send message");
+
+ if (read(cfd, buf, sizeof(buf)) < 0)
+ die("Could not receive message");
+
+ printf("$> %s\n", buf);
+ }
+
+ close(cfd);
+ return 0;
}
diff --git a/03_exercise/srv/server.c b/03_exercise/srv/server.c
index 972b11f..bb2b3ab 100644
--- a/03_exercise/srv/server.c
+++ b/03_exercise/srv/server.c
@@ -1,16 +1,73 @@
#include <stdio.h>
+#include <string.h>
#include <stdlib.h>
+
#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
#define PORT 9000
-int main()
-{
- while (1)
- {
- printf("[srv]: idle\n");
- sleep(2);
- }
-
- return 0;
+static inline void die(const char *msg) {
+ perror(msg);
+ exit(-1);
+}
+
+int main() {
+ struct sockaddr_in srv_addr, cli_addr;
+ int sockopt = 1;
+ socklen_t sad_sz = sizeof(struct sockaddr_in);
+ int sfd, cfd;
+ ssize_t bytes;
+ char in_buf[256];
+ char out_buf[256];
+
+ srv_addr.sin_family = AF_INET;
+ srv_addr.sin_port = htons(PORT);
+ srv_addr.sin_addr.s_addr = INADDR_ANY;
+
+ if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ die("Could not open socket");
+
+ setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char *) &sockopt, sizeof(sockopt));
+
+ if (bind(sfd, (struct sockaddr *) &srv_addr, sad_sz) < 0)
+ die("Could not bind socket");
+
+ if (listen(sfd, 1) < 0)
+ die("Could not listen on socket");
+
+ cfd = accept(sfd, (struct sockaddr *) &cli_addr, &sad_sz);
+ if (cfd < 0)
+ die("Could not accept incoming connection");
+
+ printf("srv: connected: %s\n", inet_ntoa(cli_addr.sin_addr));
+
+ while ((bytes = read(cfd, in_buf, sizeof(in_buf))) != 0) {
+ if (bytes < 0)
+ die("Couldn't receive message");
+
+ printf("srv: %s\n", in_buf);
+
+ if (strcmp(in_buf, "get") == 0) {
+ // TODO: implement get
+ } else if (strcmp(in_buf, "put") == 0) {
+ // TODO: implement put
+ } else if (strcmp(in_buf, "ping") == 0) {
+ strncpy(out_buf, "pong!", sizeof(out_buf));
+ } else {
+ // TODO: connect our shell implementation
+ strncpy(out_buf, "got: ", sizeof(out_buf));
+ strncpy(out_buf + sizeof("got: "), in_buf, sizeof(out_buf) - sizeof("got: "));
+ }
+
+ if (write(cfd, out_buf, sizeof(out_buf)) < 0)
+ die("Couldn't send message");
+ }
+
+ printf("srv: closing down\n");
+
+ close(cfd);
+ close(sfd);
}