0% found this document useful (0 votes)
30 views2 pages

Cnlab 3

Uploaded by

Anvita Manne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Cnlab 3

Uploaded by

Anvita Manne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Server

import socket

import threading

def from_client(c):

while True:

data = c.recv(1024)

if not data:

break

message = data.decode('utf-8')

print(f"Received message: {message}")

response = "Server received your message: " + message

c.sendall(response.encode('utf-8'))

c.close()

s = socket.socket()

port = 40075

s.bind(('127.0.0.1', port))

s.listen(5)

print(f"Server listening on 127.0.0.1:{port}")

while True:

c, addr = s.accept()

print(f"Accepted connection from {addr}")

c.settimeout(5)

c_handler = threading.Thread(target=from_client, args=(c,))

c_handler.start()

Client

import socket

import threading

import time

c= socket.socket()

port = 40075

c.connect(('127.0.0.1', port))
print("welcome to the chat bot")

time_check = time.time()

expiry = 5

while True:

if time.time() - time_check > expiry:

print("Chat expired due to inactivity.")

break

message = input("Enter your message: ")

if time.time() - time_check > expiry:

print("Chat expired due to inactivity.")

break

c.sendall(message.encode('utf-8'))

time_check = time.time()

data = c.recv(1024)

response = data.decode('utf-8')

print(f"Server response: {response}\n")

if response:

c.sendall('1'.encode('utf-8'))

else:

c.sendall('0'.encode('utf-8'))

time_check = time.time()

You might also like