781 Assignment5
781 Assignment5
Question Statements:
1. Write a TCP socket program (in C/C++/Java/Python) to establish connection between client
and server. The client program will send an input string to the server and the server program
will check whether the string is a palindrome or not and send the response to the client
accordingly. Client will display the value send by server. The communication between client
and server will continue until
client send ‘Quit’ message to the server.
2. Write a TCP socket program (in C/C++/Java/Python) to establish connection between client
and server. The client program will send a string to the server and server program will generate
the reverse of that string and send it back to the client. Client will display the value send by
server. The communication between client and server will continue until client send ‘Quit’
message to the server.
3. Write a TCP socket program (in C/C++/Java/Python) to establish connection between client
and server. The client program will send send a URL to the server and a depth up to which the
web-crawler visits all the pages from the initial page. Server will use the URL, run a web
crawler function up to the given depth to check all the URLs available through the input URL
and send the list of those
URLs to Client. Client will display the list send by server. The communication between client
and server will continue until client send ‘Quit’ message to the server.
TASK-1:
CLIENT PRORAM:
import socket
mssg=''
while mssg.lower()!='quit':
mssg=input("Enter the message: ")
# Send data to the server
client_socket.send(mssg.encode())
SERVER PRORAM:
import socket
while True:
data = client_socket.recv(1024)
data=data.decode()
print(f"Received message: {data}")
if data.lower() == 'quit':
response="Successfully quit."
client_socket.send(response.encode())
print(f"Response We get by the server: {response}")
break
else:
if data[::-1]==data:
response = "Pallindrome"
else:
response="Not a pallindrome"
client_socket.send(response.encode())
OUTPUT:
TASK-2:
CLIENT-SIDE PROGRAM:
import socket
mssg=''
while mssg.lower()!='quit':
mssg=input("Enter the message: ")
# Send data to the server
client_socket.send(mssg.encode())
while True:
data = client_socket.recv(1024)
data=data.decode()
print(f"Received message: {data}")
if data.lower() == 'quit':
response="Successfully quit."
client_socket.send(response.encode())
print(f"Response We get by the server: {response}")
break
else:
response = data[::-1]
client_socket.send(response.encode())
print(f"Response We get by the server: {response}")
Client-side output
Se
rver-side output
TASK 3:
CLIENT SIDE PROGRAM:
import socket
def communicate_with_server():
HOST = 'localhost'
PORT = 8888
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
try:
while True:
url = input("Enter a URL (or 'Quit' to exit): ")
client_socket.send(url.encode())
if url.lower() == 'quit':
break
response = client_socket.recv(4096).decode()
print(f"Server response:\n{response}")
except KeyboardInterrupt:
print("\nClosing connection...")
finally:
client_socket.close()
if __name__ == "__main__":
communicate_with_server()
try:
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
result_urls.append(url)
except requests.RequestException as e:
print(f"Error while processing {url}: {e}")
return result_urls
def handle_client(client_socket):
while True:
url = client_socket.recv(1024).decode('utf-8').strip()
if not url:
break
if url.lower() == 'quit':
break
depth = int(client_socket.recv(1024).decode('utf-8'))
result_urls = web_crawler(url, 1, depth)
client_socket.close()
def start_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 8888))
server_socket.listen(5)
print("Server listening on port 8888...")
while True:
client_socket, client_address = server_socket.accept()
print(f"Accepted connection from {client_address}")
handle_client(client_socket)
start_server()
OUTPUT: