From bbfbcc6c936b16568d96619f02f8bb081c190b2a Mon Sep 17 00:00:00 2001 From: sotech117 Date: Thu, 14 Sep 2023 19:02:36 -0400 Subject: finish warmup, hopefully --- client | Bin 34200 -> 34344 bytes client.c | 65 +++++++++++++++++++++------- client.dSYM/Contents/Resources/DWARF/client | Bin 11222 -> 12030 bytes protocol.h | 16 +++++++ server | Bin 34656 -> 34616 bytes server.c | 37 ++++++++++------ server.dSYM/Contents/Resources/DWARF/server | Bin 12990 -> 13478 bytes snowcast-server.c | 0 8 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 protocol.h create mode 100644 snowcast-server.c diff --git a/client b/client index 2277b67..be376f0 100755 Binary files a/client and b/client differ diff --git a/client.c b/client.c index 591d030..11c462f 100644 --- a/client.c +++ b/client.c @@ -14,7 +14,7 @@ #include -#define PORT "3490" // the port client will be connecting to +#include "protocol.h" #define MAXDATASIZE 100 // max number of bytes we can get at once @@ -30,14 +30,14 @@ void *get_in_addr(struct sockaddr *sa) int main(int argc, char *argv[]) { - int sockfd, numbytesrecv, numbytessent; + int sockfd, numbytesrecv, numbytessent, recvbytes; char buf[MAXDATASIZE]; struct addrinfo hints, *servinfo, *p; int rv; char s[INET6_ADDRSTRLEN]; - if (argc != 3) { - fprintf(stderr,"usage: client hostname\n"); + if (argc != 4) { + fprintf(stderr," \n"); exit(1); } @@ -45,7 +45,10 @@ int main(int argc, char *argv[]) hints.ai_family = AF_INET; // only IPv4 hints.ai_socktype = SOCK_STREAM; - if ((rv = getaddrinfo(argv[2], PORT, &hints, &servinfo)) != 0) { + char* tcpPort = argv[2]; // port we use to connect to server's tcp stream + char* udpPort = argv[3]; // port we use to connect to server's udp info and command + + if ((rv = getaddrinfo(argv[1], tcpPort, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } @@ -74,26 +77,58 @@ int main(int argc, char *argv[]) inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); - printf("client: connecting to %s\n", s); + // printf("client: connecting to %s\n", s); freeaddrinfo(servinfo); // all done with this structure - if ((numbytesrecv = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { + struct Welcome msg; + // recv the message, check for errors too + if ((recvbytes = recv(sockfd, (char*)&msg, sizeof(struct snowcast_message), 0)) == -1) { perror("recv"); exit(1); } - - buf[numbytesrecv] = '\0'; - - printf("client: received '%s'\n",buf); - - if ((numbytessent = send(sockfd, "Welcome", 7, 0)) == -1) { + // print the num bytes received + // printf("client: received '%s'\n", msg); + // printf("size of '%d'\n", recvbytes); + printf("Welcome to Snowcast! The server has %d stations.\n", msg.numStations); + // printf("type %d", msg.replyType); + // close(sockfd); + + // if ((numbytesrecv = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { + // perror("recv"); + // exit(1); + // } + // buf[numbytesrecv] = '\0'; + + // printf("client: received '%s'\n",buf); + + // make a struct for the message, number is thennubmer of stations + struct Hello hello; + hello.commandType = 0; + // convert updPort to an int + int udpPortInt = atoi(udpPort); + hello.udpPort = htonl(udpPortInt); + + if ((numbytessent = send(sockfd, &hello, sizeof(struct Hello), 0)) == -1) { perror("send"); exit(1); } - buf[numbytessent] = '\0'; - + // make new struct for the incoming message + // buf[numbytesrecv] = '\0'; + // char bufff[MAXDATASIZE]; + // if ((recvbytes = recv(sockfd, bufff, MAXDATASIZE, 0)) == -1) + // { + // perror("recv"); + // exit(1); + // } + // bufff[recvbytes] = '\0'; + + // // print the num bytes received + // printf("client: received '%d'\n", recvbytes); + + // //printf("Welcome to Snowcast! The server has %d stations.\n", ntohl(msg.number)); + // printf("%s", bufff); close(sockfd); return 0; diff --git a/client.dSYM/Contents/Resources/DWARF/client b/client.dSYM/Contents/Resources/DWARF/client index 4d642ff..ef0cdd0 100644 Binary files a/client.dSYM/Contents/Resources/DWARF/client and b/client.dSYM/Contents/Resources/DWARF/client differ diff --git a/protocol.h b/protocol.h new file mode 100644 index 0000000..0cdc998 --- /dev/null +++ b/protocol.h @@ -0,0 +1,16 @@ +#include // Provides uint8_t, int8_t, etc. + +struct snowcast_message { + uint8_t type; + uint16_t number; +} __attribute__((packed)); + +struct Welcome { + uint8_t replyType; + uint16_t numStations; +} __attribute__((packed)); + +struct Hello { + uint8_t commandType; + uint16_t udpPort; +} __attribute__((packed)); diff --git a/server b/server index 4f78e6c..1ef6cd4 100755 Binary files a/server and b/server differ diff --git a/server.c b/server.c index ea7d62c..c17e039 100644 --- a/server.c +++ b/server.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -15,7 +16,7 @@ #include #include -#define PORT "3490" // the port users will be connecting to +#include "protocol.h" #define BACKLOG 10 // how many pending connections queue will hold @@ -43,9 +44,9 @@ void *get_in_addr(struct sockaddr *sa) return &(((struct sockaddr_in6*)sa)->sin6_addr); } -int main(void) +int main(int argc, char *argv[]) { - int sockfd, new_fd, numbytes; // listen on sock_fd, new connection on new_fd + int sockfd, new_fd, numbytes, b; // listen on sock_fd, new connection on new_fd char buf[MAXDATASIZE]; struct addrinfo hints, *servinfo, *p; struct sockaddr_storage their_addr; // connector's address information @@ -55,12 +56,19 @@ int main(void) char s[INET6_ADDRSTRLEN]; int rv; + if (argc < 3) { + fprintf(stderr,"usage: [file 1] [file 2] ... \n"); + exit(1); + } + memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; // only IPv4 - hints.ai_socktype = SOCK_STREAM; + hints.ai_socktype = SOCK_STREAM; // TCP connection hints.ai_flags = AI_PASSIVE; // use my IP - if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) { + const char* port = argv[1]; // the port users will be connecting to + + if ((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } @@ -126,18 +134,19 @@ int main(void) if (!fork()) { // this is the child process close(sockfd); // child doesn't need the listener - if (send(new_fd, "Hello, world!", 13, 0) == -1) + + // make a struct for the message, number is thennubmer of stations + struct Welcome welcome; + welcome.replyType = 2; + welcome.numStations = argc - 2; + if ((b = send(new_fd, &welcome, sizeof(struct Welcome), 0)) == -1) perror("send"); - // close(new_fd); - if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) { - perror("recv"); - exit(1); - } - buf[numbytes] = '\0'; - printf("server: received '%s'\n",buf); + close(new_fd); + // print the num bytes sent + // printf("server: sent '%d'\n",b); exit(0); } - close(new_fd); // parent doesn't need this + // close(new_fd); // parent doesn't need this } return 0; diff --git a/server.dSYM/Contents/Resources/DWARF/server b/server.dSYM/Contents/Resources/DWARF/server index 22ad6d7..40af941 100644 Binary files a/server.dSYM/Contents/Resources/DWARF/server and b/server.dSYM/Contents/Resources/DWARF/server differ diff --git a/snowcast-server.c b/snowcast-server.c new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3-70-g09d2