Requirements:
- A client mode.
- A server mode.
- The server should wait for and listen to incoming connections.
- The client should take the server's hostname to connect to.
- After the connection is established, both parties should be able to send messages.
- The server should continue listening for connections after one is closed.
- The acknowledgment message should be
Message received
. - The receiving side should always return the acknowledgment message.
- The sending side should always calculate the round-trip time for the acknowledgment message.
- The wire protocol should not make any assumptions about the message contents (e.g., allowed byte values, character encoding, etc.).
Developed with:
- Opam 2.3.0
- Dune 3.17.2
- OCaml 5.3.0
$ opam install --deps-only .
# In some cases, you'll need to run:
$ opam update && opam upgrade
# Build
$ dune build
# Run through dune
$ dune exec chat
# Or with the binary
$ ./_build/default/src/chat.exe
$ dune runtest
# or
$ dune exec tests_suite
$ dune exec chat -- server
# or
$ dune exec chat -- server --port <port_number>
Expected output:
Starting server...
Listening on 127.0.0.1:8090.
$ dune exec chat -- client --hostname <hostname> --port <port_number>
Expected output (if there is a server running and you passed the correct hostname + port number):
$ dune exec chat -- client --hostname localhost --port 8090
Connected to 127.0.0.1:8090
- The
hostname
is expected to be an IP value such as127.0.0.1
or a host likelocalhost
. - Any
host
added to/etc/hosts
should work fine. - In case the host isn't on
/etc/hosts
or it is an invalid IP, this should be the expected behaviour:
chat: option '--hostname': Invalid hostname.
Usage: chat client [--hostname=<HOSTNAME>] [--port=<PORT_NUMBER>] [OPTION]…
Try 'chat client --help' or 'chat --help' for more information.
- Something similar goes to the port number:
$ dune exec chat -- client --hostname localhost --port notanumber
chat: option '--port': Unable to parse the port number.
Usage: chat client [--hostname=<HOSTNAME>] [--port=<PORT_NUMBER>] [OPTION]…
Try 'chat client --help' or 'chat --help' for more information.
- If the port number isn't assigned at server mode, the default port will be 8090.
- To close a connection on the client side, you can input the char
]
to end it.
Client side:
$ dune exec chat -- client --hostname localhost --port 8090
Connected to 127.0.0.1:8090
]
Closing connection...
Server side:
dune exec chat -- server
Starting server...
Listening on 127.0.0.1:8090.
New connection with 127.0.0.1:50210.
Connection closed with 127.0.0.1:50210
- If a connection exists between the server and the client and you cancel the server, the client will be shut down with the abruptly closed connection.
Server side:
$ dune exec chat -- server
Starting server...
Listening on 127.0.0.1:8090.
New connection with 127.0.0.1:50169.
^C
Closing connection and shutting down the server...
Client side:
$ dune exec chat -- client --hostname localhost --port 8090
Connected to 127.0.0.1:8090
Connection closed with 127.0.0.1:8090