A TCP socket is a type of network socket that allows for a reliable, stream-oriented communication between two endpoints over a network. It provides a connection-oriented, reliable, and ordered delivery of data. When two applications want to communicate with each other over a network, they can create a TCP socket and use it to exchange a data.
This tutorial explores how to create a simple TCP socket using the Rust programming language. Building a TCP socket in Rust teaches you several essential concepts such as networking and handling TCP connections.
Requirements:
Before proceeding, we assume that you are familiar with the fundamentals of the Rust programming language. We also assume that you have a Rust development environment configured on your machine including the Rust compiler and cargo.
Setting Up the Project
With Rust installed, we can create a new Rust project using the cargo command. Navigate to the directory where you want to create your project and run the following command:
The provided command creates a new Rust project called “tcp_socket” with a binary target. The –bin flag tells cargo to build a binary target rather than a library.
Setting Up the Project Dependencies
To better understand how to work with sockets in Rust, we will avoid using an external crate. Instead, we use Rust’s networking module.
We can do this by importing the “std::net” module into our project. This is a built-in module; we do not need to use cargo to install it.
Create a New TCP Socket
In the src folder of your project directory, create a new file called “server.rs” and add the following source code to create a new TCP socket in Rust:
use std::net::{TcpListener, TcpStream};
fn start_server() {
let listener = TcpListener::bind("127.0.0.1:9001").unwrap();
for stream in listener.incoming() {
let mut tcp_stream = stream.unwrap();
let mut buffer = [0; 1024];
let _size = tcp_stream.read(&mut buffer).unwrap();
tcp_stream.write_all(b"Hello, client").unwrap();
tcp_stream.flush().unwrap();
}
}
The previous code defines a function called “start_server” that creates a TCP server which listens for incoming connections on the IP address 127.0.0.1 and port number 9001.
Create a TCP Client
To communicate with the server, we can create a client that connects to the server address. Create a new “client.rs” file in the src directory and add the following code:
let mut tcp_stream = TcpStream::connect("127.0.0.1:9001").unwrap();
tcp_stream.write_all(b"Hello server").unwrap();
tcp_stream.flush().unwrap();
let mut buffer = [0; 1024];
let size = tcp_stream.read(&mut buffer).unwrap();
let message = String::from_utf8_lossy(&buffer[..size]);
println!("Server says: {}", message);
}
In the previous code, we first create a TcpStream connection to the server by calling the TcpStream::connect(“127.0.0.1:9001”).
This connects to the server at the IP address of 127.0.0.1 and port number 9001. We then write the “Hello server” message to the server using the “write_all” method and flush the stream using flush.
Start the server and client in the main function.
fn main() {
start_server();
start_client();
}
The server should respond when the client connects with “Hello, client.”
Conclusion
This article covers the basics of creating a basic TCP socket and server using the Rust std::net module.