aboutsummaryrefslogtreecommitdiff
path: root/client.c
diff options
context:
space:
mode:
Diffstat (limited to 'client.c')
-rw-r--r--client.c29
1 files changed, 14 insertions, 15 deletions
diff --git a/client.c b/client.c
index 19fd9d8..6ade043 100644
--- a/client.c
+++ b/client.c
@@ -108,9 +108,6 @@ int main(int argc, char *argv[])
char input[LINE_MAX];
- fflush(stdout);
- printf("Enter a number to change to it's station. Click q to end stream.\n");
- fflush(stdout);
while (1) {
char *line = fgets(input, LINE_MAX, stdin);
@@ -145,7 +142,7 @@ void *reply_thread_routine(void* args) {
// int recvbytes;
while (1) {
// recv the first byte of the message to get it's type
- uint8_t reply_type = -1;
+ int reply_type;
// print size of utin8
if (recv(sockfd, &reply_type, 1, 0) == -1) {
perror("recv");
@@ -154,31 +151,33 @@ void *reply_thread_routine(void* args) {
if (reply_type == 2) { // we have a welcome message
// recv the message, check for errors too
- char* num_stations = -1;
+ uint16_t buf_num_stations;
int bytes_to_read = sizeof(uint16_t);
- if (recv_all(sockfd, &num_stations, &bytes_to_read) == -1) {
+ if (recv_all(sockfd, &buf_num_stations, &bytes_to_read) == -1) {
perror("recv_all");
exit(1);
}
- num_stations = ntohs((uint16_t) num_stations);
+ uint16_t num_stations = ntohs(buf_num_stations);
fflush(stdout);
- printf("Welcome to Snowcast! The server has %d stations.\n", num_stations);
+ printf("Click q to end stream.\n");
+ printf("Welcome to Snowcast! The server has %u stations.\n", num_stations);
fflush(stdout);
continue;
}
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) {
+ int string_size;
+ if (recv(sockfd, &string_size, 1, 0) == -1)
+ {
perror("recv");
exit(1);
}
+
char *song_name = malloc(string_size);
if(song_name == NULL) { perror("malloc in song name"); }
- int bytes_to_read = string_size;
- if (recv_all(sockfd, song_name, &bytes_to_read) == -1) {
+ if (recv_all(sockfd, song_name, &string_size) == -1) {
perror("recv_all");
exit(1);
}
@@ -187,15 +186,15 @@ void *reply_thread_routine(void* args) {
continue;
} else if (reply_type == 4) { // we have an invalid command message
// get the string size
- u_int8_t string_size = -1;
+ int string_size;
if (recv(sockfd, &string_size, 1, 0) == -1) {
perror("recv");
exit(1);
}
+
char *message = malloc(string_size);
if(message == NULL) { perror("malloc in message"); }
- int bytes_to_read = string_size;
- if (recv_all(sockfd, message, &bytes_to_read) == -1) {
+ if (recv_all(sockfd, message, &string_size) == -1) {
perror("recv_all");
exit(1);
}