aboutsummaryrefslogtreecommitdiff
path: root/client.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-20 01:55:59 -0400
committersotech117 <michael_foiani@brown.edu>2023-09-20 01:55:59 -0400
commit4311db5d742f03080dfa58aeba45dadc175c625c (patch)
tree48d0c9d27c0013ac0a223466201361ed9bf4da9d /client.c
parentaf44220248c7da4c2f66f5bd7ff018acfe623b03 (diff)
more small things to fit assignment spec
Diffstat (limited to 'client.c')
-rw-r--r--client.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/client.c b/client.c
index df5ea9b..735330c 100644
--- a/client.c
+++ b/client.c
@@ -117,7 +117,7 @@ int main(int argc, char *argv[])
} else {
// convert input to an int
int inputInt = atoi(input);
- printf("Changing to station %d.\n", inputInt);
+ // printf("Changing to station %d.\n", inputInt);
// send the command to change the station
struct SetStation setStation;
@@ -139,6 +139,7 @@ void *reply_thread_routine(void* args) {
char buf[MAX_READ_SIZE];
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) {
perror("recv");
exit(1);
@@ -148,9 +149,9 @@ void *reply_thread_routine(void* args) {
// 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]);
- }
+ // for (int i = 0; i < recvbytes; i++) {
+ // printf("%c ", buf[i]);
+ // }
struct Reply reply;
memcpy(&reply, buf, 2);
@@ -163,6 +164,7 @@ void *reply_thread_routine(void* args) {
memcpy(&msg, buf, sizeof(struct Welcome));
msg.numStations = ntohs(msg.numStations);
printf("Welcome to Snowcast! The server has %d stations.\n", msg.numStations);
+ continue;
}
// print the size of reply
@@ -172,19 +174,21 @@ void *reply_thread_routine(void* args) {
char *song_name = malloc(reply.stringSize);
// printf(sizeof(struct Reply));
memcpy(song_name, buf + 2, reply.stringSize);
-
printf("New song announced: %s\n", song_name);
-
free(song_name);
-
continue;
} else if (reply.replyType == 4) {
- printf("client: received an invalid command message\n");
-
- continue;
+ // print sockfd
+ char *message = malloc(reply.stringSize);
+ // printf(sizeof(struct Reply));
+ memcpy(message, buf + 2, reply.stringSize);
+ printf("Exiting. %s\n", message);
+ close(sockfd);
+ exit(1);
}
- printf("client: received an unknown message\n");
- memset(buf, 0, MAX_READ_SIZE);
+ printf("Exiting. Lost connection to server.");
+ close(sockfd);
+ exit(1);
}
} \ No newline at end of file