Linux Networking & TCP/IP for Developers · レッスン

TCPとUDPの基礎

TCP(信頼性のあるコネクション指向)とUDP(信頼性のないコネクションレス)の違いを理解し、それぞれのアプリケーションでの用途を学びます。

レッスン 3/412 ステップ

「TCPとUDPの基礎」はCoddyKit上の無料Linux Networking & TCP/IP for Developersレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはLinux Networking & TCP/IP for Developers学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Linux Networking & TCP/IP for Developersコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Meet TCP and UDP

When data travels across a network, it uses different rules or 'protocols'. At the Transport layer of the TCP/IP model, two main protocols handle how applications send and receive data: TCP and UDP.

They both move data, but they do it in fundamentally different ways, each suited for specific tasks.

TCP: The Reliable Partner

TCP stands for Transmission Control Protocol. Think of TCP as a very careful post office that guarantees your letter will arrive, in order, and without errors.

  • It's connection-oriented: A connection must be established before data is sent.
  • It's reliable: It guarantees delivery of data.
  • It ensures ordered data transfer and error checking.

TCP's Connection Handshake

Before TCP sends any application data, it performs a 'three-way handshake' to establish a connection:

  1. SYN (Synchronize): Client asks to connect.
  2. SYN-ACK (Synchronize-Acknowledge): Server acknowledges and agrees.
  3. ACK (Acknowledge): Client acknowledges, and the connection is ready.

This handshake ensures both sides are ready to communicate reliably.

TCP: Guarantees Delivery

TCP uses several mechanisms to ensure reliability:

  • Acknowledgements (ACKs): The receiver sends ACKs for data received. If no ACK, the sender retransmits.
  • Sequence Numbers: Data packets are numbered to ensure they arrive in the correct order and to detect missing packets.
  • Flow Control: Prevents a fast sender from overwhelming a slow receiver.
  • Congestion Control: Manages network traffic to avoid overloading the network.

TCP Client: A Brief Look

This Python code snippet shows how a TCP client socket is created and attempts to connect. Notice the SOCK_STREAM type, which signifies TCP.

Run it to see the connection attempt!

import socket

# Create a TCP (Stream) socket
# AF_INET for IPv4, SOCK_STREAM for TCP
tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Attempt to connect to a server
# This establishes the 3-way handshake
server_address = ('localhost', 8080)
print(f"Attempting TCP connection to {server_address[0]}:{server_address[1]}...")

try:
    tcp_client_socket.connect(server_address)
    print("TCP connection established.")
    message = b"Hello TCP!"
    tcp_client_socket.sendall(message)
    print(f"Sent: '{message.decode()}'")
    # In a real app, you'd also receive a response here
except ConnectionRefusedError:
    print("Connection refused. No TCP server running at that address.")
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    tcp_client_socket.close()
    print("TCP socket closed.")

UDP: The Fast Messenger

UDP stands for User Datagram Protocol. Unlike TCP, UDP is like sending a postcard: you send it, and you hope it arrives, but there's no guarantee or tracking.

  • It's connectionless: No connection is established before sending.
  • It's unreliable: No guarantees of delivery, order, or error-free transmission.
  • It prioritizes speed and low overhead over reliability.

UDP: No Handshake, No ACKs

The simplicity of UDP comes from its lack of features:

  • There's no three-way handshake to set up a connection.
  • There are no acknowledgements (ACKs) for received data.
  • There's no retransmission of lost packets.
  • There's no built-in flow or congestion control.

This means less overhead, making it much faster for certain applications.

UDP Client: A Brief Look

This Python code demonstrates a UDP client sending data. Notice the SOCK_DGRAM type, which indicates UDP. Because it's connectionless, it can send data directly.

Run it to see a UDP datagram being sent!

import socket

# Create a UDP (Datagram) socket
# AF_INET for IPv4, SOCK_DGRAM for UDP
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# UDP is connectionless.
# You can send data directly without establishing a connection.
target_address = ('localhost', 9090)
message = b"Hello UDP!"
print(f"UDP socket created.")
print(f"Sending '{message.decode()}' to {target_address[0]}:{target_address[1]}...")

# Send the data
udp_socket.sendto(message, target_address)

print("UDP datagram sent.")
udp_socket.close()
print("UDP socket closed.")

TCP vs. UDP: The Core Differences

Here's a quick comparison of the two:

  • Connection: TCP is connection-oriented; UDP is connectionless.
  • Reliability: TCP guarantees delivery; UDP offers best-effort delivery.
  • Order: TCP ensures ordered data; UDP does not.
  • Speed: UDP is generally faster due to less overhead; TCP is slower due to reliability mechanisms.
  • Overhead: TCP has higher overhead; UDP has lower overhead.

When to Use TCP or UDP?

Choosing between TCP and UDP depends on your application's needs:

  • Use TCP for: Web browsing (HTTP/HTTPS), Email (SMTP, IMAP, POP3), File Transfer (FTP), Secure Shell (SSH). When data integrity and order are critical.
  • Use UDP for: Online gaming, Video/Audio streaming, Voice over IP (VoIP), Domain Name System (DNS). When speed and low latency are more important than guaranteed delivery.

Quick Check: Protocol Choices

Based on what you've learned, which of the following statements correctly describe UDP?

Recap: TCP & UDP Essentials

You've explored the fundamental differences between TCP and UDP!

  • TCP is reliable, connection-oriented, and ensures data integrity and order, but with higher overhead.
  • UDP is fast, connectionless, and has low overhead, but offers no delivery guarantees.

Understanding these distinctions is crucial for designing and troubleshooting network applications. Next, you'll apply these concepts to real-world scenarios!

無料で開始

AI チューターと学ぶ Linux Networking & TCP/IP for Developers — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「TCPとUDPの基礎」レッスンは無料ですか?

はい。「TCPとUDPの基礎」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Linux Networking & TCP/IP for Developersコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Linux Networking & TCP/IP for Developersコースには全4レッスンが含まれています。

「TCPとUDPの基礎」で何を学びますか?

TCP(信頼性のあるコネクション指向)とUDP(信頼性のないコネクションレス)の違いを理解し、それぞれのアプリケーションでの用途を学びます。 ブラウザで直接実行するハンズオンコードでLinux Networking & TCP/IP for Developersを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Linux Networking & TCP/IP for Developersを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのLinux Networking & TCP/IP for Developersは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「TCPとUDPの基礎」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このLinux Networking & TCP/IP for Developersレッスンでコードを書いて実行できますか?

はい。すべてのLinux Networking & TCP/IP for Developersレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. TCP/IPモデルを理解する
  2. IPアドレスとサブネット化
  3. TCPとUDPの基礎
  4. ICMPとPing・Tracerouteの役割
← Linux Networking & TCP/IP for Developersに戻る