You are on page 1of 9

2CS502 - Computer Networks

19BCE045 – Badrish Davra

Practical – 3 : Data Link Layer (frame generation): write a program to read a stream of data
file to create frames by implementing bit stuffing and byte stuffing.

First see the code on server side and then of the client side….
Server code
import socket
import pickle

class dataFrame: # we use this class to stuf the frames and send to client
def __init__(self,snumber,frame):
self.sequenceNumber=snumber
self.frame=frame

def stuf(self,a,e):
temp1=self.frame.replace(e,e*2)
temp2=temp1.replace(a,a+e)
self.frame=a+temp2+a

s = socket.socket()
print('socket creation successful...')
s.bind((bytes('localhost', 'utf-8'), 9999))
#localhost means my own device and we have the port address on the server and clien
t side as the same

s.listen(5)
print('connecting...')
print('')

while True:
c, address = s.accept()
print('connection established - ', address)
textFile=open(r'F:\sem v\cn\textForPractical3.txt','rb')
frame=textFile.read(1024)
frameNumber=1
while frame:
fobj=dataFrame(frameNumber,frame.decode('utf-8'))
fobj.stuf('a','e')
toSend=pickle.dumps(fobj)
c.send(toSend)
print('frame number sending from server....',fobj.sequenceNumber)
print('the frame sent : ')
print(fobj.frame)
print('successfully sent the frame!!!')
print('')
frame=textFile.read(1024)
frameNumber=frameNumber+1
c.close()

Client code
import socket
import pickle

class dataFrame: #we make this class to receive the frame object and unstuf
the data
def __init__(self):
self.sequenceNumber=None
self.frame=None

def unstuf(self,a,e):
temp1=str(self.frame).replace(e*2,e)
temp2=str(temp1).replace(a+e,a)
self.frame=temp2[1:-1]

c = socket.socket()
print('socket creation successful...')
c.connect((bytes('localhost', 'utf-8'), 9999))
print('')
#localhost means my own device and we have the port address on the server and clien
t side as the same

while True:
try:
dataAtClient=c.recv(4096)
toReceive=pickle.loads(dataAtClient)

if toReceive.frame:
print('frame number receiving to client....',toReceive.sequenceNumber)
toReceive.unstuf('a','e')
print('the frame received : ')
print(toReceive.frame)
print('successfully received the frame!!!')
print('')

except EOFError:
break

Below shown are the snapshots of code….


Server code below
Client code below
Below is the snap shot of the text file containing the text which I framed it and sent from server to client by
using byte stuffing. It is just one window but it is a long text.

Now we need to run both the code simultaneously and I did this practical in spyder. So, I used two consoles
and ran both the code separately in each console.
So, first we will run the server code in first console and it will search for connections.
When we run it, output we get when just server code is ran is shown below…
So, it means that the server has still not got connection with client.
So consecutively, on the other console we run the client code.
Now, we see the output of server console and we can see that, wherever there is an ‘a’ or ‘e’, the text is
stuffed.
So, 4 frames are generated each of 1024 bytes.

And now, we will see the client output on the other console, we will see that ‘a’ and ‘e’ are unstuffed from the code
and we get the original text with same number of frames and the exact text.

It will be on console 2.
So, the text is transferred from the server side to client side.

You might also like