TCP/IP is a protocol stack that forms the foundation of the Internet and most computer networks. It combines the TCP protocol, responsible for reliable data delivery, and IP, which ensures packet routing between nodes. According to IETF RFC 1180 (2024), the TCP/IP stack handles over 80% of global network traffic, including mobile and web applications. The protocol stack defines how data is packaged, addressed, and transmitted between devices on a network.
Key Takeaways
TCP/IP (Transmission Control Protocol / Internet Protocol) is a protocol stack developed in the 1970s for ARPANET and has become the de facto standard for the global Internet. It defines how data is divided into packets, addressed, transmitted, and reassembled on the receiver’s side.
The TCP/IP architecture is built on a multi-layered principle, where each layer abstracts specific functions. The application layer works with application data, the transport layer ensures reliability, the network layer handles routing, and the link layer manages physical transmission. Modularity allows replacing protocols within a layer without affecting neighboring ones.
In mobile development, TCP/IP is used by every application that makes network requests. Libraries such as URLSession, OkHttp, AFNetworking, and Alamofire work on top of this stack, hiding the details of packet packaging and routing from the developer. However, understanding TCP/IP is necessary for performance optimization: configuring TCP parameters through URLSessionConfiguration allows managing timeouts, maintaining HTTP Persistent Connections, and setting up proxies for enterprise environments.
Understanding TCP/IP is essential for diagnosing network problems: if an application cannot connect to a server, the issue may be at any layer of the stack — from the physical channel to the application protocol. Tools like tcpdump, Wireshark, and Charles Proxy allow analyzing traffic at each layer and finding bottlenecks. For mobile developers, Xcode provides a Network Debug Dashboard, and Android Studio offers a Network Inspector for analyzing TCP connections in real time.
The TCP/IP model includes four layers, each performing strictly defined functions during data transmission. Understanding this hierarchy is necessary for diagnosing network problems and optimizing applications.
At the application layer, protocols that applications directly interact with operate: HTTP, HTTPS, FTP, SMTP, DNS, WebSocket. This layer formats data in a way the application understands and passes it to the transport layer. Mobile application developers work at this layer through networking libraries.
The transport layer ensures data transmission between applications on different devices. The main protocols are TCP (reliable transmission with acknowledgment) and UDP (fast transmission without guarantees). TCP adds sender and receiver ports to the data, manages packet ordering, and controls network congestion.
The network layer is implemented by the IP protocol, which defines addressing (IPv4, IPv6) and packet routing. Each packet contains the sender and receiver IP addresses, and routers along the path make decisions about the next node based on routing tables. IPv4 uses 32-bit addresses (about 4.3 billion), while IPv6 uses 128-bit addresses, solving the address exhaustion problem.
The link layer works with the physical transmission medium: Ethernet, Wi-Fi, Bluetooth. This layer converts packets into signals and back, manages access to the transmission medium via MAC addresses, and detects physical layer errors using frame check sequences (FCS).
At the link layer, protocols such as ARP (Address Resolution Protocol), which converts IP addresses to MAC addresses of devices on the local network, and PPP (Point-to-Point Protocol), used in VPN connections and mobile data networks, operate.
| Layer | Protocols | Function |
|---|---|---|
| Application | HTTP, HTTPS, DNS, FTP, WebSocket | Application data formatting |
| Transport | TCP, UDP | Transmission between applications, error control |
| Network | IP, ICMP, ARP | Packet routing and addressing |
| Link | Ethernet, Wi-Fi, PPP | Physical medium transmission |
TCP (Transmission Control Protocol) is one of the main transport layer protocols. It ensures reliable, ordered, and error-checked delivery of a byte stream between applications. Before transmitting data, TCP establishes a connection through a three-way handshake.
The three-way handshake begins with the client sending a SYN packet. The server responds with a SYN-ACK packet, confirming receipt. The client completes the handshake with an ACK packet, after which data transmission begins. This process guarantees that both parties are ready for exchange and synchronized on sequence numbers.
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('example.com', 80))
sock.send(b'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
response = sock.recv(4096)
print(response.decode())
sock.close()
In the example, a TCP socket is created with the SOCK_STREAM type, which automatically performs a three-way handshake when connect is called. The send method transmits an HTTP request, and recv receives the response. After completion, close sends a FIN packet to properly close the connection.
Let’s look at an example of a TCP server in Python that accepts multiple clients via threads. Each connection is handled in a separate thread, allowing multiple clients to be served simultaneously without blocking the main loop.
import socket
import threading
def handle_client(conn, addr):
print(f'Client connected: {addr}')
with conn:
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data.upper())
print(f'Client {addr} disconnected')
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('0.0.0.0', 9090))
server.listen(5)
print('TCP server started on port 9090')
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
The TCP server handles each connection in a separate thread, calling accept in an infinite loop. The handle_client function receives data, converts it to uppercase, and sends it back via sendall. When the recv loop ends (when the client closes the connection), the thread terminates, and the socket is automatically closed. In mobile applications, such low-level socket work is not used directly — mobile HTTP libraries manage connections through thread pools and automatically reuse TCP connections.
The choice between TCP and UDP is one of the key decisions when designing network communication. TCP guarantees delivery, order, and data integrity at the cost of additional latency. UDP sacrifices reliability for minimal latency and lower channel load.
| Characteristic | TCP | UDP |
|---|---|---|
| Connection | Establishes (handshake) | Does not establish |
| Reliability | Guarantees delivery | Does not guarantee |
| Packet order | Orders packets | Does not order |
| Speed | Lower | Higher |
| Usage | Web, email, files | Streaming, games, DNS |
For mobile applications, the protocol choice depends on the scenario: REST APIs and data loading require TCP, while video calls and online games benefit from UDP speed. Some applications combine both protocols — TCP for control and UDP for media streaming. The modern QUIC protocol, running over UDP and used in HTTP/3, combines the advantages of both approaches and is already supported on iOS and Android through URLSession automatically.
Let’s look at an example of a TCP client in Python with error handling and timeout. This approach is used in mobile applications for reliable server connection.
import socket
import sys
def tcp_client(host, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect((host, port))
print(f'Connected to {host}:{port}')
return sock
except socket.error as err:
print(f'Connection error: {err}')
sys.exit(1)
sock = tcp_client('api.example.com', 443)
sock.close()
The tcp_client function takes a host and port, creates a socket with a 10-second timeout, and attempts to connect. On connection error — for example, server unavailability or firewall blocking — an exception with a problem description is thrown. In mobile applications, connection timeout is critical: too short leads to false errors on slow networks, too long leads to interface freezing. The recommended value for mobile networks is 10–15 seconds for timeoutIntervalForRequest and 30–60 seconds for timeoutIntervalForResource in URLSession configuration.
Frequently Asked Questions
IP is responsible for routing — delivering a packet from sender to receiver through intermediate nodes. TCP ensures reliable data assembly: controls ordering, checks integrity, and requests retransmission of lost packets. Together they form the TCP/IP stack.
TCP/IP does not use a fixed port — the port is assigned by the application. Standard ports: 80 (HTTP), 443 (HTTPS), 22 (SSH), 25 (SMTP). Port 443 is the main one for secure web traffic in mobile applications.
A TCP connection is a logical channel between two applications, established through a three-way handshake (SYN, SYN-ACK, ACK). The connection is identified by the sender and receiver pair (IP address, port). The handshake guarantees that both parties are ready for data exchange.
Yes, TCP/IP works on local networks (LAN) without Internet access. Devices connect via Wi-Fi Direct, Bluetooth PAN, or Ethernet and exchange data using local IP addresses. Local TCP/IP is used in IoT devices and peer-to-peer applications.
TCP is slower due to additional mechanisms: a three-way handshake before transmission, acknowledgment of each packet, retransmission of lost segments, and congestion control. UDP sends datagrams without these checks, reducing latency at the cost of potential loss.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also