diff options
| author | Carlos Maiolino <[email protected]> | 2025-07-10 22:55:07 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-07-10 22:56:55 +0200 |
| commit | d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch) | |
| tree | 267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /C/netCode/client.c | |
| parent | 869e68986aa8f69af6e7842260a68d1e5c6f796f (diff) | |
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'C/netCode/client.c')
| -rw-r--r-- | C/netCode/client.c | 66 |
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; +} |
