Building a TCP Client
A TCP client is a program that initiates and manages a connection to a server using the Transmission Control Protocol (TCP).
In a typical client-server model, the client connects to a server, sends data, and then waits for a response or closes the connection. TCP clients are the starting point for many networked applications such as chat programs, file transfers, or web browsers. When you build a TCP client, you control how to connect, what data to send, and when to disconnect, which is fundamental to writing networked software in Python.
import socket
# Create a socket object using IPv4 and TCP
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define the server address and port
server_address = ('localhost', 65432)
try:
# Connect to the server
client_socket.connect(server_address)
print("Connected to server at", server_address)
# Send a message to the server
message = "Hello, Server!"
client_socket.sendall(message.encode('utf-8'))
print("Message sent:", message)
finally:
# Close the connection
client_socket.close()
print("Connection closed")
This client code begins by creating a socket object, specifying IPv4 (AF_INET) and TCP (SOCK_STREAM) as the connection type. The server_address variable holds the address and port of the server you want to connect to. Using the connect() method, the client attempts to establish a connection with the server. Once connected, the client prepares a message, encodes it as bytes using UTF-8, and sends it with sendall(). After sending the message, the client closes the socket with close() to end the connection. This approach ensures that your client communicates reliably with a TCP server, handles the connection lifecycle, and cleans up resources when finished.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 9.09
Building a TCP Client
Svep för att visa menyn
A TCP client is a program that initiates and manages a connection to a server using the Transmission Control Protocol (TCP).
In a typical client-server model, the client connects to a server, sends data, and then waits for a response or closes the connection. TCP clients are the starting point for many networked applications such as chat programs, file transfers, or web browsers. When you build a TCP client, you control how to connect, what data to send, and when to disconnect, which is fundamental to writing networked software in Python.
import socket
# Create a socket object using IPv4 and TCP
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define the server address and port
server_address = ('localhost', 65432)
try:
# Connect to the server
client_socket.connect(server_address)
print("Connected to server at", server_address)
# Send a message to the server
message = "Hello, Server!"
client_socket.sendall(message.encode('utf-8'))
print("Message sent:", message)
finally:
# Close the connection
client_socket.close()
print("Connection closed")
This client code begins by creating a socket object, specifying IPv4 (AF_INET) and TCP (SOCK_STREAM) as the connection type. The server_address variable holds the address and port of the server you want to connect to. Using the connect() method, the client attempts to establish a connection with the server. Once connected, the client prepares a message, encodes it as bytes using UTF-8, and sends it with sendall(). After sending the message, the client closes the socket with close() to end the connection. This approach ensures that your client communicates reliably with a TCP server, handles the connection lifecycle, and cleans up resources when finished.
Tack för dina kommentarer!