Lab2 PDC
Lab2 PDC
CMS: 388064
SECTION: BSCS11A
Objectives
Tools/Software Requirement
Description
By using simple network applications, we can send files or messages over the internet. A socket
is an object that symbolize a low-level connection point to the IP stack. This socket can be
opened or closed or one of a set amount of intermediate states. A socket can deliver and receive
data down this connection. Files is generally sent in blocks of a few kilobytes at a time for
effectiveness; each of these blocks is named as a packet. Here are the list of well-known port
numbers which usually used.
In this lab, we will explore the client server programming model in C#, however you are
free to choose any language for the lab task.
Lab Tasks
1. Your task is to use the above mentioned program and convert it to group chat using event
publish/subscribe architecture as discussed in class. Write a middleware that act as a chat
group. Users can join and leave that group. Middleware broadcast the received messages
to all the connected users.
Code:
Client:
import socket
import threading
class ChatClient:
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.username = None
receive_thread = threading.Thread(target=self.receive_messages)
receive_thread.start()
def receive_messages(self):
while True:
data, addr = self.sock.recvfrom(1024)
print(data.decode())
if __name__ == "__main__":
Middleware
import socket
import threading
class ChatGroupMiddleware:
def __init__(self, host, port):
self.host = host
self.port = port
self.users = []
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((host, port))
self.lock = threading.Lock()
if __name__ == "__main__":
middleware = ChatGroupMiddleware("127.0.0.1", 2055)
middleware.start()
Output:
User1
CS332: Distributed Computing Page 6
User 2
User 3
Middleware
2. Add another feature in this middleware which is based on unicast i.e. message
send to particular user will not be received by all other users in the group. For this
you can assume the user ID of all users is well-known or middleware can provide
you that. Moreover, all the communication must be go through the middleware.
Code:
Middleware.py
import socket
import threading
def start(self):
print("Middleware started, servicing on port", self.port)
while True:
data, addr = self.sock.recvfrom(1024)
message = data.decode()
if message.startswith("JOIN"):
self.handle_join(message, addr)
elif message.startswith("LEAVE"):
self.handle_leave(addr)
else:
self.route_message(data)
if __name__ == "__main__":
middleware = ChatGroupMiddleware("127.0.0.1", 2055)
middleware.start()
Client.py:
import socket
import threading
class ChatClient:
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.username = None
def start(self):
self.username = input("Enter Your Name: ")
join_message = "JOIN " + self.username + " " + "id"
self.sock.sendto(join_message.encode(), (self.host, self.port))
receive_thread = threading.Thread(target=self.receive_messages)
receive_thread.start()
def receive_messages(self):
while True:
data, addr = self.sock.recvfrom(1024)
print(data.decode())
if __name__ == "__main__":
client = ChatClient("127.0.0.1", 2055)
client.start()
Output :
User 1
User 2
User 3
Middleware