Ex No 3
Ex No 3
PROCEDURE:
PROGRAM:
Echo Server:
import socket
try:
# Keep receiving and echoing messages from the client
while True:
data = connection.recv(1024) # Buffer size
if not data:
break # No more data received, exit the loop
print(f"Received: {data.decode()}")
finally:
# Clean up the connection
connection.close()
server_socket.close()
Echo Client:
import socket
Server Output:
Client Output:
Criteria Marks
Preparation /20
Observation /25
Viva /10
Total /75
RESULT:
3)ii) Chat
AIM:
PROCEDURE
while True:
try:
message = client_socket.recv(1024)
if message:
print(f"Received: {message.decode('utf-8')}")
broadcast(message, client_socket)
else:
break
except:
break
while True:
client_socket, client_address = server_socket.accept()
print(f"New connection from {client_address}")
clients.append(client_socket)
if __name__ == "__main__":
chat_server("127.0.0.1", 65432) # Server running on localhost:65432
Chat Client Code:
import socket
import threading
client_socket.close()
if __name__ == "__main__":
chat_client("127.0.0.1", 65432) # Connect to the server running on localhost:65432
Server Output:
Server started on 127.0.0.1:65432
New connection from ('127.0.0.1', 12345)
New connection from ('127.0.0.1', 12346)
Client 1 Output (After Connecting to Server):
Welcome to the chat! Type 'exit' to leave.
Hello everyone!
Client 2 Output (After Connecting to Server):
Welcome to the chat! Type 'exit' to leave.
Hi, how are you?
Evaluation by faculty
Criteria Marks
Preparation /20
Observation /25
Viva /10
Total /75
RESULT:
PROCEDURE:
Steps for File Transfer:
1. Server Side:
o The server listens on a specific port for incoming connections.
o It accepts the connection, receives the file data in chunks, and writes the data
to a file.
2. Client Side:
o The client connects to the server.
o It reads the file in binary mode and sends the data to the server in chunks
while True:
# Accept a client connection
client_socket, client_address = server_socket.accept()
print(f"Connection from {client_address} established.")
if __name__ == "__main__":
file_server("127.0.0.1", 65432) # You can use a different port
if __name__ == "__main__":
file_client("127.0.0.1", 65432, "sample_file.txt") # Path to the file you want to send
Server Output:
Server listening on 127.0.0.1:65432
Connection from ('127.0.0.1', 12345) established.
Receiving file: sample_file.txt
File 'sample_file.txt' received successfully.
Client Output:
File 'sample_file.txt' sent successfully.
Evaluation by faculty
Criteria Marks
Preparation /20
Observation /25
Viva /10
Total /75
RESULT: