aboutsummaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-20 23:49:07 -0400
committersotech117 <michael_foiani@brown.edu>2023-09-20 23:49:07 -0400
commitdc5d15064b90f9c25b1b47503e4074dba063db70 (patch)
tree62155eec59046a6658069a1b573780814eb6dcb6 /server.c
parent3d9c7d4c5ae135ace068ba50f6b3ae971d8e276b (diff)
fix the set station byte reading
Diffstat (limited to 'server.c')
-rw-r--r--server.c50
1 files changed, 35 insertions, 15 deletions
diff --git a/server.c b/server.c
index 837432a..59cd4e7 100644
--- a/server.c
+++ b/server.c
@@ -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