diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-09-20 23:24:05 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-09-20 23:24:05 -0400 |
commit | 3d9c7d4c5ae135ace068ba50f6b3ae971d8e276b (patch) | |
tree | 5e391492380af5fb351d543d8fd6b17ad5871afe /client.c | |
parent | 362250d1cb923d9ced8ea51508ace50b76050b06 (diff) |
implement collecting all bits, if they don't come in one message
Diffstat (limited to 'client.c')
-rw-r--r-- | client.c | 90 |
1 files changed, 50 insertions, 40 deletions
@@ -16,7 +16,7 @@ #include <arpa/inet.h> -#include "protocol.h" +#include "protocol.c" #define MAXDATASIZE 100 // max number of bytes we can get at once @@ -103,6 +103,9 @@ int main(int argc, char *argv[]) exit(1); } + // CONSIDER: could recieve the welcome message here + + char input[LINE_MAX]; printf("Enter a number to change to it's station. Click q to end stream.\n"); while (1) { @@ -123,8 +126,9 @@ int main(int argc, char *argv[]) struct SetStation setStation; setStation.commandType = 1; setStation.stationNumber = htons(inputInt); - if ((numbytessent = send(sockfd, &setStation, sizeof(struct SetStation), 0)) == -1) { - perror("send"); + int bytes_to_send = sizeof(struct SetStation); + if (send_all(sockfd, &setStation, &bytes_to_send) == -1) { + perror("send_all"); exit(1); } } @@ -135,59 +139,65 @@ int main(int argc, char *argv[]) void *reply_thread_routine(void* args) { int sockfd = (int)args; - int recvbytes; - char buf[MAX_READ_SIZE]; + // int recvbytes; while (1) { - // recv the message, check for errors too - memset(buf, 0, MAX_READ_SIZE); - if ((recvbytes = recv(sockfd, &buf, MAX_READ_SIZE, 0)) == -1) { + // recv the first byte of the message to get it's type + uint8_t reply_type = -1; + // print size of utin8 + if (recv(sockfd, &reply_type, 1, 0) == -1) { perror("recv"); exit(1); } - buf[recvbytes] = '\0'; - // printf("client: received %d bytes on a reply call \n", recvbytes); - // print the two first field of the call - // printf("client: replyType: %d, stringSize: %d\n", buf[0], buf[1]); - // print the while buffer by char - // for (int i = 0; i < recvbytes; i++) { - // printf("%c ", buf[i]); - // } - struct Reply reply; - memcpy(&reply, buf, 2); - - // print out the fields of reply on one line - // printf("\nclient: replyType: %d, stringSize: %d\n", reply.replyType, reply.stringSize); - if (reply.replyType == 2) { - struct Welcome msg; + if (reply_type == 2) { // we have a welcome message // recv the message, check for errors too - memcpy(&msg, buf, sizeof(struct Welcome)); - msg.numStations = ntohs(msg.numStations); - printf("Welcome to Snowcast! The server has %d stations.\n", msg.numStations); + int16_t num_stations = -1; + int bytes_to_read = sizeof(uint16_t); + if (recv_all(sockfd, &num_stations, &bytes_to_read) == -1) { + perror("recv_all"); + exit(1); + } + num_stations = ntohs(num_stations); + printf("Welcome to Snowcast! The server has %d stations.\n", num_stations); continue; } - // print the size of reply - if (reply.replyType == 3) { - // printf("client: received an announce message\n"); - - char *song_name = malloc(reply.stringSize); - // printf(sizeof(struct Reply)); - memcpy(song_name, buf + 2, reply.stringSize); + if (reply_type == 3) { // we have an announce message + // get the string size + u_int8_t string_size = -1; + if (recv(sockfd, &string_size, 1, 0) == -1) { + perror("recv"); + exit(1); + } + char *song_name = malloc(string_size); + int bytes_to_read = string_size; + if (recv_all(sockfd, song_name, &bytes_to_read) == -1) { + perror("recv_all"); + exit(1); + } printf("New song announced: %s\n", song_name); free(song_name); continue; - } else if (reply.replyType == 4) { - // print sockfd - char *message = malloc(reply.stringSize); - // printf(sizeof(struct Reply)); - memcpy(message, buf + 2, reply.stringSize); - printf("Exiting. %s\n", message); + } else if (reply_type == 4) { // we have an invalid command message + // get the string size + u_int8_t string_size = -1; + if (recv(sockfd, &string_size, 1, 0) == -1) { + perror("recv"); + exit(1); + } + char *message = malloc(string_size); + int bytes_to_read = string_size; + if (recv_all(sockfd, message, &bytes_to_read) == -1) { + perror("recv_all"); + exit(1); + } + printf("Invalid protocol: %s. Exiting.\n", message); + free(message); close(sockfd); exit(1); } - printf("Exiting. Lost connection to server."); + printf("Lost connection to server. Exiting."); close(sockfd); exit(1); } |