Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Building a Simple TCP Server | Working with Sockets
Python Networking Basics

bookBuilding a Simple TCP Server

Note
Definition

A TCP server is a program that listens for incoming network connections, receives data from clients, and can send responses back.

You should use a TCP server when you need a reliable service, for example a chat server, file server, or web server. TCP guarantees that data arrives in the correct order and without loss, which makes it suitable for applications where consistency and accuracy matter. TCP servers are often used when you want full control over how clients connect and communicate with your system.

1234567891011121314151617181920212223
import socket # Create a TCP/IP socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to localhost on port 65432 server_socket.bind(("127.0.0.1", 65432)) # Listen for incoming connections (allow 1 connection in the queue) server_socket.listen(1) print("Server is listening on 127.0.0.1:65432") # Wait for a connection connection, client_address = server_socket.accept() print("Connection from", client_address) # Receive data in small chunks data = connection.recv(1024) print("Received:", data.decode()) # Clean up the connection connection.close() server_socket.close()
copy

This server code begins by importing the socket module, which is Python’s standard way to work with network sockets. The server creates a new socket using socket.socket(socket.AF_INET, socket.SOCK_STREAM). Here, AF_INET tells Python to use IPv4 addresses, and SOCK_STREAM sets up a TCP connection. The server then binds to the address 127.0.0.1 (localhost) and port 65432, making it available for clients that connect to this port on the same machine.

Next, the server starts listening for incoming connections by calling listen(1). The number 1 means it will allow only one connection to wait in the queue at a time. When a client connects, accept() returns a new connection object and the address of the client. The server prints out the client’s address to confirm the connection. It then waits to receive up to 1024 bytes of data from the client using recv(1024). The received data is decoded from bytes to a string for easier reading and printed to the console.

Once the data is received, the server closes both the connection to the client and the main server socket. This simple server handles a single connection and then shuts down, which is useful for basic testing and understanding how TCP servers work in Python.

question mark

What is the purpose of using socket.AF_INET when creating a TCP server socket in Python?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookBuilding a Simple TCP Server

Scorri per mostrare il menu

Note
Definition

A TCP server is a program that listens for incoming network connections, receives data from clients, and can send responses back.

You should use a TCP server when you need a reliable service, for example a chat server, file server, or web server. TCP guarantees that data arrives in the correct order and without loss, which makes it suitable for applications where consistency and accuracy matter. TCP servers are often used when you want full control over how clients connect and communicate with your system.

1234567891011121314151617181920212223
import socket # Create a TCP/IP socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to localhost on port 65432 server_socket.bind(("127.0.0.1", 65432)) # Listen for incoming connections (allow 1 connection in the queue) server_socket.listen(1) print("Server is listening on 127.0.0.1:65432") # Wait for a connection connection, client_address = server_socket.accept() print("Connection from", client_address) # Receive data in small chunks data = connection.recv(1024) print("Received:", data.decode()) # Clean up the connection connection.close() server_socket.close()
copy

This server code begins by importing the socket module, which is Python’s standard way to work with network sockets. The server creates a new socket using socket.socket(socket.AF_INET, socket.SOCK_STREAM). Here, AF_INET tells Python to use IPv4 addresses, and SOCK_STREAM sets up a TCP connection. The server then binds to the address 127.0.0.1 (localhost) and port 65432, making it available for clients that connect to this port on the same machine.

Next, the server starts listening for incoming connections by calling listen(1). The number 1 means it will allow only one connection to wait in the queue at a time. When a client connects, accept() returns a new connection object and the address of the client. The server prints out the client’s address to confirm the connection. It then waits to receive up to 1024 bytes of data from the client using recv(1024). The received data is decoded from bytes to a string for easier reading and printed to the console.

Once the data is received, the server closes both the connection to the client and the main server socket. This simple server handles a single connection and then shuts down, which is useful for basic testing and understanding how TCP servers work in Python.

question mark

What is the purpose of using socket.AF_INET when creating a TCP server socket in Python?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt