#include #include #include #include #include #include #include #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 \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; }