diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-09-20 23:49:07 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-09-20 23:49:07 -0400 |
commit | dc5d15064b90f9c25b1b47503e4074dba063db70 (patch) | |
tree | 62155eec59046a6658069a1b573780814eb6dcb6 | |
parent | 3d9c7d4c5ae135ace068ba50f6b3ae971d8e276b (diff) |
fix the set station byte reading
-rw-r--r-- | server.c | 50 | ||||
-rwxr-xr-x | snowcast_control | bin | 35565 -> 35565 bytes | |||
-rwxr-xr-x | snowcast_listener | bin | 34654 -> 34654 bytes | |||
-rwxr-xr-x | snowcast_server | bin | 56124 -> 56124 bytes | |||
-rw-r--r-- | snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server | bin | 27185 -> 27319 bytes |
5 files changed, 35 insertions, 15 deletions
@@ -560,8 +560,9 @@ void *select_thread(void *arg) { } } else { // handle data from a client - struct Command command; - if ((nbytes = recv(i, (char*)&command, sizeof(struct Command), 0)) <= 0) { + uint8_t command_type = -1; + if ((nbytes = recv(i, &command_type, 1, 0)) <= 0) + { // got error or connection closed by client if (nbytes == 0) { // connection closed @@ -573,13 +574,24 @@ void *select_thread(void *arg) { FD_CLR(i, &master); // remove from master set // remove user from data structures destroy_user(i); - } else { + } + else + { // we got some data from a client - if (command.commandType == 0) { - // hello message with udpPort - // printf("udpPort (from Hello) for new connection is %d.\n", ntohs(command.number)); + if (command_type == 0) { // we got a hello message + + // get the udp port + uint16_t udp_port = -1; + int bytes_to_read = sizeof(uint16_t); + if (recv_all(i, &udp_port, &bytes_to_read) == -1) { + perror("recv_all"); + exit(1); + } + udp_port = ntohs(udp_port); + + printf("udpPort (from Hello) for new connection is %d.\n", udp_port); // update udp port of user - update_user_udpPort(i, ntohs(command.number)); + update_user_udpPort(i, udp_port); // send the welcome message to client struct Welcome welcome; @@ -589,11 +601,11 @@ void *select_thread(void *arg) { if (send_all(i, &welcome, &bytes_to_send) == -1) perror("send_all"); } - else if (command.commandType == 1) { + else if (command_type == 1) { // we got a setStation command // check if user has a udpPort if (user_data[sockfd_to_user[i]].udpPort == -1) { // send back in invalid command - char * message = "Must send Hello message first."; + char * message = "must send Hello message first"; send_invalid_command_reply(i, strlen(message), message); // drop connection upon invalid command close(i); @@ -602,11 +614,19 @@ void *select_thread(void *arg) { continue; } - int station_num = ntohs(command.number); + // get the station number + uint16_t station_number = -1; + int bytes_to_read = sizeof(uint16_t); + if (recv_all(i, &station_number, &bytes_to_read) == -1) { + perror("recv_all"); + exit(1); + } + station_number = ntohs(station_number); + // printf("station_num: %d\n", station_num); - if (station_num >= num_stations || station_num < 0) { + if (station_number >= num_stations || station_number < 0) { // send back in invalid command - char * message = "Station number out of range."; + char * message = "station number out of range"; send_invalid_command_reply(i, strlen(message), message); // drop connection upon invalid command close(i); @@ -617,12 +637,12 @@ void *select_thread(void *arg) { // printf("setting station to %d\n", ntohs(command.number)); // update station of user - update_user_station(i, ntohs(command.number)); - send_announce_reply(i, station_num); + update_user_station(i, station_number); + send_announce_reply(i, station_number); } else { // send back in invalid command - char * message = "Invalid command"; + char * message = "invalid command"; send_invalid_command_reply(i, strlen(message), message); // drop connection upon invalid command diff --git a/snowcast_control b/snowcast_control Binary files differindex df78cdb..ebf8c93 100755 --- a/snowcast_control +++ b/snowcast_control diff --git a/snowcast_listener b/snowcast_listener Binary files differindex 7f0e6a4..b8357dd 100755 --- a/snowcast_listener +++ b/snowcast_listener diff --git a/snowcast_server b/snowcast_server Binary files differindex 918f71e..6c3f2dc 100755 --- a/snowcast_server +++ b/snowcast_server diff --git a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server Binary files differindex 3f0a8c4..6f668a7 100644 --- a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server +++ b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server |