The document discusses code examples for client-server communication using sockets in Python. It includes examples of sending and receiving data between a TCP and UDP client and server, as well as handling file transfers and calculating elapsed time for a TCP connection. The examples demonstrate basic socket programming techniques in Python.
The document discusses code examples for client-server communication using sockets in Python. It includes examples of sending and receiving data between a TCP and UDP client and server, as well as handling file transfers and calculating elapsed time for a TCP connection. The examples demonstrate basic socket programming techniques in Python.
The document discusses code examples for client-server communication using sockets in Python. It includes examples of sending and receiving data between a TCP and UDP client and server, as well as handling file transfers and calculating elapsed time for a TCP connection. The examples demonstrate basic socket programming techniques in Python.
The document discusses code examples for client-server communication using sockets in Python. It includes examples of sending and receiving data between a TCP and UDP client and server, as well as handling file transfers and calculating elapsed time for a TCP connection. The examples demonstrate basic socket programming techniques in Python.
while True: data, client_address = server_socket.recvfrom(1024) print("Received packet from client:", data.decode()) number = int(data.decode()) if number % 2 == 0: result = "even" else: result = "odd" server_socket.sendto(result.encode(), client_address)
# Accept incoming connections and echo back any messages received
while True: client_socket, client_address = server_socket.accept() print("Accepted connection from client:", client_address) while True: data = client_socket.recv(1024) if not data: break client_socket.sendall(data) client_socket.close()
9) import socket
# Define the server address and port
SERVER_ADDRESS = 'localhost' SERVER_PORT = 12345
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the server address and port server_socket.bind((SERVER_ADDRESS, SERVER_PORT))
# Listen for incoming connections
server_socket.listen()
# Accept incoming connections and receive the file
while True: client_socket, client_address = server_socket.accept() print("Accepted connection from client:", client_address) with open('received_file.txt', 'wb') as f: while True: data = client_socket.recv(1024) if not data: break f.write(data) client_socket.close() print("File received and saved successfully")