Skip to content

Onofre-Portfolio/simple-chat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple one-on-one chat with sockets

About

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

Installing and Building

Install the dependencies:

$ opam install --deps-only .

# In some cases, you'll need to run:
$ opam update && opam upgrade

Build and Exec

# Build
$ dune build

# Run through dune
$ dune exec chat

# Or with the binary
$ ./_build/default/src/chat.exe

Tests

$ dune runtest
# or
$ dune exec tests_suite

Usage

Execution Modes

Server side

$ dune exec chat -- server
# or
$ dune exec chat -- server --port <port_number>

Expected output:

Starting server...
Listening on 127.0.0.1:8090.

Client side

$ 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

Guide

  • The hostname is expected to be an IP value such as 127.0.0.1 or a host like localhost.
  • 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

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published