0% found this document useful (0 votes)
31 views8 pages

CN Ex 7-12

Java Code for Computer Networks for sending messages via IP , RSA algorithm and so on.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views8 pages

CN Ex 7-12

Java Code for Computer Networks for sending messages via IP , RSA algorithm and so on.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program - 7:

//[Link]
import [Link].*;
import [Link].*;

public class Sender7


{
public static void main(String a[])throws Exception
{
ServerSocket ser=new ServerSocket(10);
Socket s=[Link]();
DataInputStream in=new DataInputStream([Link]);
DataInputStream in1=new DataInputStream([Link]());
String sbuff[]=new String[8];
PrintStream p;
int sptr=0,sws=8,nf,ano,i;
String ch;
do
{
p=new PrintStream([Link]());
[Link]("Enter the no. of frames : ");
nf=[Link]([Link]());
[Link](nf);
if(nf<=sws-1)
{

[Link]("Enter "+nf+" Messages to be send\n");


for(i=1;i<=nf;i++)
{
sbuff[sptr]=[Link]();
[Link](sbuff[sptr]);
sptr=++sptr%8;
}
sws-=nf;
[Link]("Acknowledgment received");
ano=[Link]([Link]());
[Link](" for "+ano+" frames");
sws+=nf;
}
else
{
[Link]("The no. of frames exceeds window size");
break;
}
[Link]("\nDo you wants to send some more frames : ");
ch=[Link](); [Link](ch);
}
while([Link]("yes"));
[Link]();
}
}
//[Link]
import [Link].*;
import [Link].*;
class Receiver7
{
public static void main(String a[])throws Exception
{
Socket s=new Socket([Link](),10);
DataInputStream in=new DataInputStream([Link]());
PrintStream p=new PrintStream([Link]());
int i=0,rptr=-1,nf,rws=8;
String rbuf[]=new String[8];
String ch; [Link]();
do
{
nf=[Link]([Link]());
if(nf<=rws-1)
{
for(i=1;i<=nf;i++)
{
rptr=++rptr%8;
rbuf[rptr]=[Link]();
[Link]("The received Frame " +rptr+" is : "+rbuf[rptr]);
}
rws-=nf;
[Link]("\nAcknowledgment sent\n");
[Link](rptr+1); rws+=nf; }
else
break;
ch=[Link]();
}
while([Link]("yes"));
}
}

OUTPUT:
//Sender
Enter the no. of frames : 3
Enter 4 Messages to be send
a
b
c
Acknowledgment received for 4 frames
Do you wants to send some more frames : no

//Receiver
The received Frame 0 is : a
The received Frame 1 is : b
The received Frame 2 is : c

Acknowledgment sent
Program - 8:
//[Link]
import [Link].*;
import [Link].*;
import [Link];

class stopwaitsender {
public static void main(String args[]) throws Exception {
stopwaitsender sws = new stopwaitsender();
[Link]();
}
public void run() throws Exception {
Scanner sc=new Scanner([Link]);
[Link]("Enter no of frames to be sent:");

int n=[Link]();
Socket myskt=new Socket("localhost",9999);
PrintStream myps=new PrintStream([Link]());

for(int i=0;i<=n;) {
if(i==n){+s
[Link]("exit");
break;
}
[Link]("Frame no "+i+" is sent");

[Link](i);
BufferedReader bf=new BufferedReader(new
InputStreamReader([Link]()));
String ack=[Link]();

if(ack!=null) {
[Link]("Acknowledgement was Received from receiver");
i++;
[Link](4000);
}
else
[Link](i);
}
}
}

//[Link]
import [Link].*;
import [Link].*;

class stopwaitreceiver {
public static void main(String args[])throws Exception {
stopwaitreceiver swr = new stopwaitreceiver();
[Link]();
}
public void run() throws Exception {
String temp="any message",str="exit";

ServerSocket myss=new ServerSocket(9999);


Socket ss_accept=[Link]();
BufferedReader ss_bf=new BufferedReader(new
InputStreamReader(ss_accept.getInputStream()));
PrintStream myps=new PrintStream(ss_accept.getOutputStream());

while([Link](str)!=0) {
[Link](1000);
temp=ss_bf.readLine();

if([Link](str)==0)
break;
[Link]("Frame "+temp+" was received");
[Link](500);
[Link]("Received");
}
[Link]("ALL FRAMES WERE RECEIVED SUCCESSFULLY");
}
}

OUTPUT:

//sender
Enter no of frames to be sent:
4
Frame no 0 is sent
Acknowledgement was Received from receiver
Frame no 1 is sent
Acknowledgement was Received from receiver
Frame no 2 is sent
Acknowledgement was Received from receiver
Frame no 3 is sent
Acknowledgement was Received from receiver

//receiver
Frame 0 was received
Frame 1 was received
Frame 2 was received
Frame 3 was received
ALL FRAMES WERE RECEIVED SUCCESSFULLY
Program - 9:
import ipaddress

ip = input("Enter ipv6 address :")


try:
ip = ipaddress.ip_address(ip)
if [Link] == 4:
print("You should enter an ipv6 address")
exit()
else:
net_mask = ipaddress.ip_address("[Link]")
host_mask = ipaddress.ip_address("[Link]")
print("Net id ==> ", ipaddress.ip_address(int(ip) & int(net_mask)))
print("Host id ==> ", ipaddress.ip_address(int(ip) & int(host_mask)))
except ValueError:
print("Invalid ip address")

OUTPUT:

Enter ipv6 address: [Link]


Net id ==> [Link]
Host id ==> [Link]

Enter ipv6 address: ff::ff


Net id ==> ff::
Host id ==> [Link]

Enter ipv6 address :192.168


Invalid ip address

Program - 10:
//[Link]
import socket

host_name = [Link]()
UDP_IP = [Link](host_name)
UDP_PORT = 8000

sock = [Link](socket.AF_INET,socket.SOCK_DGRAM)
[Link]((UDP_IP, UDP_PORT))
while True:
data,addr = [Link](1024)
print("received message: %s" % [Link]())

//[Link]
import socket

host_name = [Link]()
UDP_IP = [Link](host_name)
UDP_PORT = 8000
while True:
msg = input("Enter a message: ")
if msg == "$exit":
print("Program exited!")
exit()

MESSAGE = bytes(msg, 'utf-8')


sock = [Link](socket.AF_INET,socket.SOCK_DGRAM)
[Link](MESSAGE, (UDP_IP, UDP_PORT))
print("Sent a message to", "{}:{}".format(UDP_IP, UDP_PORT))

OUTPUT:

//client
Enter a message: Hello
Sent a message to [Link]:8000
Enter a message: Bye
Sent a message to [Link]:8000
Enter a message: $exit
Program exited!

//server
received message: Hello
received message: Bye

Program - 11:
import heapq

def dijkstra(graph, start):


distances = {vertex: float('infinity') for vertex in graph}
predecessors = {vertex: None for vertex in graph}
distances[start] = 0

priority_queue = [(0, start)]

while priority_queue:
current_distance, current_vertex = [Link](priority_queue)

if current_distance > distances[current_vertex]:


continue

for neighbor, weight in graph[current_vertex].items():


distance = current_distance + weight

if distance < distances[neighbor]:


distances[neighbor] = distance
predecessors[neighbor] = current_vertex
[Link](priority_queue, (distance, neighbor))

return distances, predecessors


graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 5},
'D': {'B': 5, 'C': 5}
}

start_vertex = 'A'
distances, predecessors = dijkstra(graph, start_vertex)

for vertex, distance in [Link]():


path = []
current = vertex
while current is not None:
[Link](0, current)
current = predecessors[current]
print('Shortest distance from {} to {}: {}'.format(start_vertex, vertex, distance))
print('Shortest path: {}\n'.format(" -> ".join(path)))

OUTPUT:

Shortest distance from A to A: 0


Shortest path: A

Shortest distance from A to B: 1


Shortest path: A -> B

Shortest distance from A to C: 3


Shortest path: A -> B -> C

Shortest distance from A to D: 6


Shortest path: A -> B -> D

Program - 12:

import rsa

message = input("Enter a message to encrypt: ")


(pub_key, priv_key) = [Link](2048)
message = [Link]('utf-8')
encrypted_message = [Link](message, pub_key)

with open("[Link]", "wb") as file:


[Link](encrypted_message)

decrypted_message = [Link](encrypted_message, priv_key).decode('utf-8')


with open("[Link]", "w") as file:
[Link](decrypted_message)

print("Encrypted message:", encrypted_message)


print("Decrypted message:", decrypted_message)
OUTPUT:

Enter a message to encrypt: Hello there


Encrypted message:
b'\x1a\xfa\xe1\xb6%\x8c\x98v1\x7f\xfbUY\xe6\xe3MV\x1fA\x88,h!j\\n~\x92B0$!y\xe2N[Ck?\xa4x\
xe2=\xa7\x04JG\xee\xa6\x19\xc5\xa4\x8d\x7f\x8b0\x9dq\xa4\xd2$7\xc4\x7f\xb8\x90\xc5\xc3\xc
1\r*0\x06F\'\xbcuh\xdf\x90\xc8\xcc\x8c&?\xdf2R]\x7f3\xc4\xfb\xf6\x8b\x91\xf9\x05&0\xc0\x1f\xe2
(E\xd2\x8b\xd2\xac\x02\xc8u\x0e\xbe*r\x11\xad\xae)<U;\xc9cP\x00Q\x0e\xe0w_\xb8E\x10\x1f:!*
\xb1c^\xc9\xc9<\x06\x97\x19u\x12V\x1e\xca\x17\xb8\xe9(t\xc1b\x8e\x81\xf3\x95\x01fV\xc7\xd6\
xac\xed\xb50\x1a\xb4\xa0o\x92\xa3\xa2\xc6m\x19JZW"\x1b>Lr\x06$\xe6\x0b\xac7\xd4p\xe3\xd
0G\x84\x8f\xf9\xcb\xc6i\xceF\x94?k\xae\x87u+)\x97R\xf69\x93pe\xb8\x7f\xbb\xb7d\xaa\xa1\xa2
K\x8c\x07\xc3\xdb;H\xa2\x97\xf54*\x05&:*\x17`\x86}vi\x93'
Decrypted message: Hello there

You might also like