Junior
What is TCP?
sobes.tech AI
Answer from AI
TCP (Transmission Control Protocol) is a connection-oriented transport layer protocol that provides reliable, ordered, and error-checked delivery of a stream of bytes between applications.
Main features:
- Connection-oriented: A logical connection is established between the sender and receiver before data transfer.
- Reliability: Ensures delivery of all data using acknowledgments (ACKs) and retransmission in case of lost packets.
- Ordering: Ensures data is delivered in the same order it was sent. Segments are numbered, and the receiver reassembles them in the correct sequence.
- Flow control: Prevents buffer overflow at the receiver by regulating the rate of data transmission.
- Congestion control: Adjusts the transmission rate to avoid network congestion.
- Stream protocol: Treats data as a continuous stream of bytes rather than discrete messages.
Used in protocols such as HTTP, HTTPS, FTP, SMTP, SSH.
# Example of a socket in Python using TCP
import socket
# Create a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to an address and port
server_address = ('localhost', 10000)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
# Accept a connection
connection, client_address = sock.accept()
# Receive data
data = connection.recv(1024)
# Send a response
connection.sendall(b'Hello, client!')
# Close the connection
connection.close()
sock.close()