summaryrefslogtreecommitdiff
path: root/C/netCode/client.c
diff options
context:
space:
mode:
Diffstat (limited to 'C/netCode/client.c')
-rw-r--r--C/netCode/client.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/C/netCode/client.c b/C/netCode/client.c
new file mode 100644
index 0000000..dafd1e8
--- /dev/null
+++ b/C/netCode/client.c
@@ -0,0 +1,66 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include "types.h"
+
+void handle_client(int fd)
+{
+ char buf[4096] = {0};
+ int *data;
+ proto_hdr_t *hdr = (proto_hdr_t*)buf;
+
+
+ read(fd, buf, sizeof(proto_hdr_t) + sizeof(int));
+
+ hdr->type = ntohl(hdr->type);
+ hdr->len = ntohs(hdr->len);
+
+ data = (int *)&hdr[1];
+ *data = ntohl(*data);
+
+ if (hdr->type != PROTO_HELLO)
+ printf("Protocol mismatch\n");
+
+ if (*data != 1)
+ printf("Protocol version mismatch\n");
+
+ printf("Client connected - proto: %d ver: %d payload: %d\n",
+ hdr->type, hdr->len, *data);
+}
+
+int main(int argc, char *argv[])
+{
+ int err;
+ struct sockaddr_in serverInfo = {0};
+ int fd = socket(AF_INET, SOCK_STREAM, 0);
+
+ if (argc != 2) {
+ printf("Usage: ./client <ip>\n");
+ exit(1);
+ }
+
+ serverInfo.sin_family = AF_INET;
+ serverInfo.sin_addr.s_addr = inet_addr(argv[1]);;
+ serverInfo.sin_port = htons(SERVER_PORT);
+
+ if (fd < 0) {
+ perror("socket");
+ exit(1);
+ }
+
+ err = connect(fd, (struct sockaddr*)&serverInfo, sizeof(serverInfo));
+ if (err < 0) {
+ perror("connect");
+ close(fd);
+ exit(1);
+ }
+
+ handle_client(fd);
+ close(fd);
+ return 0;
+}