Fork me on GitHub

Project Notes

#359 Chat CLI

Build and update the CLI chat example from Rust lang: The complete beginner’s guide, learning about making client-server network programs with Rust.

Notes

This project is adapted from an example given in the Rust lang: The complete beginner’s guide course. Dependencies and code have been updated to work cleanly in 2025.

Server

See ./chat_server/src/main.rs

The server listens to messages on an IP and port (http://127.0.0.1:6000 by default).

When a message is received, it is logged to the console and broadcast to all clients (including the sending client)

Key implementation notes:

Client

See ./chat_client/src/main.rs

Any number of clients may be started. When started, it connects to the server (http://127.0.0.1:6000 by default).

  • Any message entered in the client is sent to the server.
  • All messages received are logged to the screen
  • The client may be ended by typing “:quit” or ctrl-C

Key implementation notes:

Example Usage

Run the server:

$ cd chat_server
$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/chat_server`
Client 127.0.0.1:49686 connected
127.0.0.1:49686: "hello from 1"
Client 127.0.0.1:49687 connected
127.0.0.1:49687: "hello from 2"
127.0.0.1:49686: "saying bye from 1"
Closing connection with 127.0.0.1:49686
127.0.0.1:49687: "saying bye from 2"
Closing connection with 127.0.0.1:49687

Start a client session:

$ cd chat_client/
$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/chat_client`
Write a message:
hello from 1
Message sent "hello from 1"
message received [104, 101, 108, 108, 111, 32, 102, 114, 111, 109, 32, 49]
 -> "hello from 1"
message received [104, 101, 108, 108, 111, 32, 102, 114, 111, 109, 32, 50]
 -> "hello from 2"
saying bye from 1
Message sent "saying bye from 1"
message received [115, 97, 121, 105, 110, 103, 32, 98, 121, 101, 32, 102, 114, 111, 109, 32, 49]
 -> "saying bye from 1"
:quit
bye
$

Start a second client in parallel

$ cd chat_client/
$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/chat_client`
Write a message:
hello from 2
Message sent "hello from 2"
message received [104, 101, 108, 108, 111, 32, 102, 114, 111, 109, 32, 50]
 -> "hello from 2"
message received [115, 97, 121, 105, 110, 103, 32, 98, 121, 101, 32, 102, 114, 111, 109, 32, 49, 27, 91, 68, 27, 91, 68, 27, 91, 68, 27, 91, 68, 27, 91, 68]
 -> "saying bye from 1\u{1b}[D\u{1b}[D\u{1b}[D\u{1b}[D\u{1b}[D"
message received [115, 97, 121, 105, 110, 103, 32, 98, 121, 101, 32, 102, 114, 111, 109, 32, 49]
 -> "saying bye from 1"
saying bye from 2
Message sent "saying bye from 2"
message received [115, 97, 121, 105, 110, 103, 32, 98, 121, 101, 32, 102, 114, 111, 109, 32, 50]
 -> "saying bye from 2"
^C
$

Credits and References

About LCK#359 rust

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader