Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

TCP/IP Tutorial

Introduction to TCP/IP

The Transmission Control Protocol/Internet Protocol (TCP/IP) is a set of rules that govern how data is transmitted over the internet and networks. It is the foundational protocol suite for the internet, allowing different devices to communicate with each other. TCP/IP was developed in the 1970s and has since evolved to support a wide range of applications.

Understanding the TCP/IP Model

The TCP/IP model consists of four layers:

  • Application Layer: The top layer where applications and protocols reside, such as HTTP, FTP, and SMTP.
  • Transport Layer: Responsible for providing communication services directly to the application processes. The two main protocols here are TCP (reliable) and UDP (unreliable).
  • Internet Layer: Handles the routing of data packets across the network. The main protocol is IP (Internet Protocol).
  • Link Layer: The lowest layer, responsible for the physical connection between devices and the protocols used to access the physical medium.

TCP vs. UDP

TCP and UDP are two protocols that operate at the transport layer of the TCP/IP model:

  • TCP (Transmission Control Protocol): A connection-oriented protocol that ensures reliable data transmission. It establishes a connection before transmitting data and guarantees the order and integrity of packets.
  • UDP (User Datagram Protocol): A connectionless protocol that does not guarantee reliability or order. It is faster than TCP and is used for applications like video streaming or online gaming where speed is more critical than reliability.

IP Addressing

IP addresses are unique identifiers assigned to devices on a network. They come in two versions:

  • IPv4: The most widely used format, consisting of four octets (e.g., 192.168.1.1).
  • IPv6: A newer format designed to replace IPv4, allowing for a vastly larger number of unique addresses (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334).

Example: Creating a Simple TCP Client and Server in Rust

Below is a simple example of a TCP client and server implemented in Rust.

TCP Server

Save the following code in server.rs:

use std::net::{TcpListener, TcpStream};
use std::io::{Read, Write};

fn handle_client(mut stream: TcpStream) {
    let mut buffer = [0; 1024];
    stream.read(&mut buffer).unwrap();
    stream.write(&buffer).unwrap();
}

fn main() {
    let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                handle_client(stream);
            }
            Err(e) => {
                println!("Error: {}", e);
            }
        }
    }
}
                

TCP Client

Save the following code in client.rs:

use std::net::TcpStream;
use std::io::{Write, Read};

fn main() {
    let mut stream = TcpStream::connect("127.0.0.1:7878").unwrap();
    stream.write(b"Hello, server!").unwrap();

    let mut buffer = [0; 1024];
    let size = stream.read(&mut buffer).unwrap();
    println!("Received: {}", String::from_utf8_lossy(&buffer[0..size]));
}
                

Conclusion

TCP/IP is an essential part of networking that enables communication between devices. Understanding its layers, the differences between TCP and UDP, and how to implement simple applications using Rust provides a solid foundation for exploring more complex networking topics.