PYTHON EXP 7 (Pujan)
PYTHON EXP 7 (Pujan)
Experiment No.07
A.1 Aim:
To demonstrate of simple socket for basic information exchange between server and client.
Create a TCP/IP server program to send messages to client and client program to receive messages
from the server.
A.2 Prerequisite:
1. C, JAVA Language
A.3 Outcome:
Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate
within a process, between processes on the same machine, or between processes on different
continents.
Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP,
UDP, and so on. The socket library provides specific classes for handling the common transports as
well as a generic interface for handling the rest
PROGRAM:
SERVER CODE:
import socket
server_socket.bind((HOST, PORT))
server_socket.listen(1)
print(f"Connected by {addr}")
messages = ["Hello, Client!", "How are you?", "This is a basic TCP/IP socket example.", "Goodbye!"]
conn.sendall(message.encode())
print(f"Sent: {message}")
conn.close()
server_socket.close()
CLIENT CODE:
import socket
HOST = '127.0.0.1'
PORT = 12345
client_socket.connect((HOST, PORT))
data = client_socket.recv(1024).decode()
if not data:
break
print(f"Received: {data}")
client_socket.close()
OUTPUT:
SERVER TERMINAL OUTPUT:
Sent: Goodbye!
Received: Goodbye!