Sie sind auf Seite 1von 6

Experiment No: 4

UDP Multicast and Broadcast


Date: 30.08.2019

Aim: To establish a connection between client and server using Transmission Control Protocol (TCP) and
User Datagram Protocol(UDP).

Algorithm:
Multicast:
1. When a client tries to join a group, distance vector routing tables are changed so that there is
spanning tree constructed for each group.
2. When Server tries to send a message to that particular group, this spanning is used to send the
message to all the clients subscribed to the group.

Broadcast:
1. When the server tries to broad cast a message to the clients in the network, the message is sent
using the broadcast address of the network.

Server:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPMulticastServer {

public static void sendUDPMessage(String message,String ipAddress, int port) throws IOException {
DatagramSocket socket = new DatagramSocket();
InetAddress group = InetAddress.getByName(ipAddress);
byte[] msg = message.getBytes();
DatagramPacket packet = new DatagramPacket(msg, msg.length,
group, port);
socket.send(packet);
socket.close();
}

public static void main(String[] args) throws IOException {


sendUDPMessage("This is a broadcast message", "10.1.21.255",4321);
sendUDPMessage("This is the first multicast messge","230.0.0.0", 4321);
sendUDPMessage("This is the second multicast messge","230.0.0.0", 4321);
sendUDPMessage("OK", "230.0.0.0", 4321);
}
}

Client:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class UDPMulticastClient implements Runnable {

public static void main(String[] args) {


Thread t=new Thread(new UDPMulticastClient());
t.start();
}

public void receiveUDPMessage(String ip, int port) throws IOException {


byte[] buffer=new byte[1024];
MulticastSocket socket=new MulticastSocket(4321);
InetAddress group=InetAddress.getByName("230.0.0.0");
socket.joinGroup(group);
while(true){
System.out.println("Waiting for multicast message...");
DatagramPacket packet=new DatagramPacket(buffer,
buffer.length);
socket.receive(packet);
String msg=new String(packet.getData(),
packet.getOffset(),packet.getLength());
System.out.println("[Multicast UDP message received]>> "+msg);
if("OK".equals(msg)) {
System.out.println("No more message. Exiting : "+msg);
break;
}
}
socket.leaveGroup(group);
socket.close();
}

@Override
public void run(){
try {
receiveUDPMessage("230.0.0.0", 4321);
}catch(IOException ex){
ex.printStackTrace();
}
}
}
Output:
Server:

Client:

Result:
Hence the JAVA program for multicasting and broadcasting using UDP has been implemented and the
output has been verified.
Experiment No: 5
Distance Vector Routing
Date: 13.09.2019

Aim: To implement distance vector routing for n routers in c++.


Algorithm:

1. A router transmits its distance vector to each of its neighbors in a routing packet.
2. Each router receives and saves the most recently received distance vector from each of its
neighbors.
3. A router recalculates its distance vector when:

• It receives a distance vector from a neighbor containing different information than before.
• It discovers that a link to a neighbor has gone down.

➢ Dx(y) = Estimate of least cost from x to y


➢ C(x,v) = Node x knows cost to each neighbor v
➢ Dx = [Dx(y): y ∈ N ] = Node x maintains distance vector
➢ Node x also maintains its neighbors' distance vectors
➢ For each neighbor v, x maintains Dv = [Dv(y): y ∈ N ]

Program:

#include<stdio.h>
#include<iostream>
using namespace std;
struct node
{
unsigned dist[6];
unsigned from[6];
}DVR[10];
int main()
{
cout<<"\n\n-------------------- Distance Vector Routing Algorithm----------- ";
int costmat[6][6];
int nodes, i, j, k;
cout<<"\n\n Enter the number of nodes : ";
cin>>nodes;

cout<<"\n Enter the cost matrix : \n" ;


for(i = 0; i < nodes; i++)
{
for(j = 0; j < nodes; j++)
{
cin>>costmat[i][j];
costmat[i][i] = 0;
DVR[i].dist[j] = costmat[i][j];
DVR[i].from[j] = j;
}
}

for(i = 0; i < nodes; i++)


for(j = i+1; j < nodes; j++)
for(k = 0; k < nodes; k++)
if(DVR[i].dist[j] > DVR[i].dist[k] + DVR[k].dist[j])
{
DVR[i].dist[j] = DVR[i].dist[k] + DVR[k].dist[j];
DVR[j].dist[i] = DVR[i].dist[j];
DVR[i].from[j] = k;
DVR[j].from[i] = k;
}

for(i = 0; i < nodes; i++)


{
cout<<"\n\n For router: "<<i+1;
for(j = 0; j < nodes; j++)
cout<<"\t\n node "<<j+1<<" via "<<DVR[i].from[j]+1<<" Distance "<<DVR[i].dist[j];
}
cout<<" \n\n ";
return 0;
}

Output:
Result:
Hence the C++ program for distance vector routing has been implemented and the output has been
verified.

Das könnte Ihnen auch gefallen