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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy