CN Lab
CN Lab
CN Lab
Task-2
while True:
if option == 4:
break
a = len(t)
N = len(g)
t += '0' * (N - 1)
# CRC calculation
cs = t
for _ in range(len(t)):
if cs[0] == '1':
cs = cs[1:] + '0'
print("\nGenerating polynomial:", g)
t = t[:a] + cs[a:]
if 0 < e <= a + N - 1:
print("\nErroneous data:", t)
# CRC calculation after error insertion
cs = t
for _ in range(len(t)):
if cs[0] == '1':
cs = cs[1:] + '0'
else:
print("Invalid option")
Task-3
pre_post = '01111110'
pre_post = '01111110'
decode_op = ''
i=8
decode_op += op[i]
if op[i] == '1':
i += 1
if op[i] == '0':
i += 1
i += 1
print("Destuffed Bit Sequence is:", decode_op)
Task-4
# Character stuffing
# Character destuffing
Task-5
while True:
for i in range(framesize):
sent += 1
if sent == framesize: break
if (sent := int(input("\nPlease enter the last acknowledgment received:\n"))) >= framesize: break
Task-6
S, E = input("Sorcce:"), input("destination:")
s, e = N.index(S), N.index(E)
S[s].update({'l':0,'v':1})
k=s
while k != e:
l = S[k]['l'] + D[k][i]
S[k]['v'] = 1
i, k, p = 0, e, [0] * M
Task-7
edge = [[int(input(f"\nIs there any edge from {i+1} to {j+1}? ")) for j in range(n)] for i in range(n)]
for i in range(n):
if edge[i][x-1] or edge[x-1][i]:
cost[i] = int(input(f"\nEnter the cost of node {x} to its neighbour {i+1}: ")) + int(input(f"Enter the
Routing table entry from {i+1} to {y}: "))
print(f"\nEstimated cost from node {x} to {y} is {delay} via the node {d}.")
Task-8
import time
import random
bucket_size = 512
time.sleep(2)
print("\n\t\tBucket overflow")
else:
time.sleep(2)
pkt_size -= op
time.sleep(2)
server
import socket
MAX = 80
PORT = 8080
sockfd.bind(('localhost', PORT))
sockfd.listen(5)
print("Server listening..")
while True:
buff = conn.recv(MAX).decode()
msg = input()
conn.send(msg.encode())
if msg.lower() == "exit":
print("Server Exit...")
break
conn.close()
client
import socket
MAX = 80
PORT = 8080
sockfd = socket.socket()
sockfd.connect(('127.0.0.1', PORT))
buff = sockfd.recv(MAX).decode()
if buff.strip().lower() == "exit":
print("Client Exit...")
break
sockfd.close()
Java
Client
import java.io.*;
import java.net.*;
try{
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
Server
import java.io.*;
import java.net.*;
public class MyServer {
try{
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e)
System.out.println(e);
Task-10
Server
import socket
PORT = 8080
MAXLINE = 1024
sockfd.bind(('127.0.0.1', PORT))
while True:
print("Client:", buffer.decode())
sockfd.sendto(hello.encode(), cliaddr)
Client
import socket
PORT = 8080
MAXLINE = 1024
buffer, _ = sockfd.recvfrom(MAXLINE)
print("Server:", buffer.decode())
sockfd.close()
Java
Server
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
while (true){
ds.receive(DpReceive);
System.out.println("Client:-" + data(receive));
if (data(receive).toString().equals("bye")){
break;
if (a == null)
return null;
int i = 0;
ret.append((char) a[i]);
i++;
return ret;
Client
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
InetAddress ip = InetAddress.getLocalHost();
byte buf[] = null;
while (true){
buf = inp.getBytes();
ds.send(DpSend);
if (inp.equals("bye"))
break;
EXP-