cs381/as7
John Montagu, the 4th Earl of Sandvich da549ff655
Add READMEs
2026-04-05 23:16:09 -07:00
..
asio@bd500f0a01 Add AS7 2026-03-22 22:58:50 -07:00
generated Add AS7 2026-03-22 22:58:50 -07:00
CMakeLists.txt Fix linking wrong asio library 2026-04-04 22:28:36 -07:00
CMakeLists.txt.backup Add AS7 2026-03-22 22:58:50 -07:00
README.md Add READMEs 2026-04-05 23:16:09 -07:00
as7.cpp Finish AS7 functionality 2026-03-29 19:45:45 -07:00
client.hpp Fix linking wrong asio library 2026-04-04 22:28:36 -07:00
server.hpp Fix linking wrong asio library 2026-04-04 22:28:36 -07:00
skybox.cpp Add AS7 2026-03-22 22:58:50 -07:00
skybox.hpp Add AS7 2026-03-22 22:58:50 -07:00

README.md

Building and Running

Clone the repository, navigate to the root of the project, and initialize the submodules:

git clone https://github.com/humanoidsandvichdispenser/cs381.git
cd cs381
git submodule update --init --recursive

Navigate to the as7 directory, create a build directory, and run CMake to generate the build files:

cd as7
mkdir -p build
cd build
cmake ..

Compile the code using make:

make

This should create an executable named as7 in the build directory. You can run the executable with the following command and options:

./as7 server|client

Instructions on how to use the program

To start the server, run:

./as7 server

To start a client, run:

./as7 client

The server will listen on port 9000 and bind to all addresses, and the client will connect to localhost on port 9000.

Commands

You can use the /nick <name> command to change your nickname, and the /color red|green command to change the color of the chat.

You can also use /ping to view the latency between all connected clients and the server. The latency is calculated as the round-trip time for a message to go from the client to the server and back.

Readme Question

Commands Question

/nick <name> is the server-side command to change the nickname of a connected client. /color red|green is the client-side command to change the color of the chat messages displayed in the client's terminal.

To make the /nick command work, the server needs to maintain an additional nickname property for the ClientConnection struct. When handling the /nick command, the server would receive the client's message starting with /nick and the new nickname. It would not broadcast this as a message to other clients, but updates the nickname property and broadcasts that to all clients to inform them of the nickname change.

To make the /color command work, the client needs to maintain a color variable that determines how to display incoming messages. When the client enters the command /color red or /color green, it updates the color variable accordingly but does not send this command to the server. Any future messages received from the server would be displayed in the chosen color.

Commenting

// include iostream for input/output operations
#include <iostream>

// include the client code which contains the client entry point and related functions
#include "client.hpp"
// include the server code which contains the server entry point and related functions
#include "server.hpp"

int main(int argc, char** argv) {
    // ./as7 client
    // ./as7 server

    // check if number of cli args is 2.
    if(argc != 2) {
        // if not display usage
        std::cerr << "Usage: " << argv[0] << " [client/server]" << std::endl;

        // then exit with code -1 which is 255 (although to be pedantic,
        // returning 2 is more appropriate and a bash convention for incorrect
        // usage https://tldp.org/LDP/abs/html/exitcodes.html)
        return -1;
    }

    // command (which is either "client" or "server") is the second cli argument
    // use string_view for string methods, since argv is a const char*
    std::string_view command = argv[1];

    // if the command is "server", start the server
    if(command == "server") {
        // run the server entry point
        server_main();
    // if the command is "server", start the server
    } else if(command == "client") {
        // start client, connecting to localhost
        client_main("127.0.0.1");
    } else {
        // display usage if neither "client" nor "server" was entered
        std::cerr << "Usage: " << argv[0] << " [client/server]" << std::endl;

        // exit with code -1 which is 255
        return -1;
    }
}

Extra Credit

The /ping command was implemented to allow clients to check their and other clients' latency to the server.