Networking in Rust
In this lesson, you will learn how networking works in Rust and how Rust programs communicate over a network using standard protocols.
Rust provides powerful networking support through its standard library, allowing you to build fast, safe, and reliable networked applications.
What Is Networking?
Networking allows programs to communicate with each other over a network, such as the internet or a local network.
Common networking tasks include:
- Sending and receiving data
- Connecting to servers
- Building client-server applications
- Handling multiple connections
Networking Support in Rust
Rust’s standard library provides networking functionality through the
std::net module.
It supports common protocols such as:
- TCP (Transmission Control Protocol)
- UDP (User Datagram Protocol)
In this lesson, we will focus on TCP networking.
TCP Client and Server Model
In TCP networking:
- A server listens for incoming connections
- A client connects to the server
- Data is exchanged between them
Rust makes this model safe and efficient.
Creating a TCP Server
Below is a simple TCP server that listens for incoming connections.
use std::net::TcpListener;
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080")
.expect("Failed to bind");
println!("Server listening on port 8080");
for stream in listener.incoming() {
match stream {
Ok(_) => println!("New client connected"),
Err(e) => println!("Connection failed: {}", e),
}
}
}
This server waits for clients to connect on port 8080.
Creating a TCP Client
A TCP client connects to a server and sends data.
use std::net::TcpStream;
use std::io::Write;
fn main() {
let mut stream = TcpStream::connect("127.0.0.1:8080")
.expect("Failed to connect");
stream.write_all(b"Hello from client")
.expect("Failed to send data");
}
The client connects to the server and sends a message.
Reading Data from a Stream
To read incoming data, use the Read trait.
use std::io::Read;
use std::net::TcpStream;
fn handle_client(mut stream: TcpStream) {
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();
println!("Received: {}", String::from_utf8_lossy(&buffer));
}
This reads data sent by the client.
Handling Multiple Connections
Rust allows you to handle multiple clients using threads.
use std::net::TcpListener;
use std::thread;
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
thread::spawn(|| {
println!("Client connected");
});
}
}
Each client connection runs in its own thread.
Why Rust Is Good for Networking
Rust is well-suited for networking because:
- Memory safety without garbage collection
- High performance
- Strong concurrency support
- Predictable behavior under load
📝 Practice Exercises
Exercise 1
Modify the server to print client messages.
Exercise 2
Change the client to send multiple messages.
Exercise 3
Experiment with different ports.
✅ Practice Answers
Answer 1
Use Read to capture incoming data and print it.
Answer 2
Call write_all multiple times.
Answer 3
Update the port number in both client and server.
What’s Next?
In the next lesson, you will learn about Serialization in Rust and how data is converted for transmission and storage.