DV 알고리즘 을 모 의 하 는 프로그램

컴퓨터 네트워크 - 위 에서 아래로 방법, 제4 장 프로 그래 밍 작업, 아 날로 그 DV 알고리즘.네 개의 노드 가 있 습 니 다. 데 이 터 는 책 에 있 는 것 입 니 다. 한 Eclipse 에서 네 개의 노드 를 실행 할 수 있 습 니 다. 콘 솔 을 잠 그 는 것 이 좀 번 거 롭 습 니 다.
 
import java.net.*;
import java.util.*;


public class Node0{
	int[] m = new int[4];
	int[] i = new int[4];
	int fasong = 1;
	int jieshou = 0;
	int gaibian = 0;
	int n = 0;
	int flag = 0;
	DatagramSocket serverSocket = new DatagramSocket(9000);;
	String send;
	byte[] sendData;
	byte[] receiveData;
	DatagramPacket sendPacket;
	DatagramPacket receivePacket;
	StringTokenizer st;
	public  void init0() throws Exception{
		
		 m[0] = 0;
		 m[1] = 1;
		 m[2] = 3;
		 m[3] = 7;
	     send=m[0]+" "+m[1]+" "+m[2]+" "+m[3]+" ";
		 System.out.println("       :");
		 System.out.println("   		 node1		 node2		 node3");
		 System.out.println("0		1		3		7");
		 System.out.println("----		----		----		----");
		 System.out.println();
	     sendData = new byte[100];
	     receiveData = new byte[100];
	     sendData = send.getBytes();
	     InetAddress sendto = InetAddress.getByName("localhost");
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9001);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
	     serverSocket.send(sendPacket);
	     
	}
	public void update0() throws Exception{
		 send=m[0]+" "+m[1]+" "+m[2]+" "+m[3];
	     sendData = new byte[100];
	     sendData = send.getBytes();
	     InetAddress sendto = InetAddress.getByName("localhost");
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9001);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
	     serverSocket.send(sendPacket);
	     fasong++;
	}
	public static void main(String args[]) throws Exception
	{
		 new Node0();
	}
	public Node0() throws Exception{
		try
		{
			Thread.currentThread();
			Thread.sleep(20000);//   
		}
		catch(Exception e){}
		init0();
		while(true){
			flag = 0;
			receivePacket = new DatagramPacket(receiveData,receiveData.length );
	     	serverSocket.receive(receivePacket);
	     	jieshou++;
	     	String sentence=new String(receivePacket.getData());
	     	st = new StringTokenizer(sentence);
	     	n = 0;
	     	while(st.hasMoreTokens() && n<4){
	     		i[n] = Integer.parseInt(st.nextToken());
	     		n++;
	     	}
     		if(i[0] == 1){
     			if(m[1]+i[2]<m[2]){
     				m[2] = m[1] + i[2];
     				flag = 1;
     				System.out.println("     1-》2   ");
     			}
     			if(m[1]+i[3]<m[3]){
     				m[3] = m[1] + i[3];
     				flag = 1;
     				System.out.println("     1-》3   ");
     			}
     		}
     		if(i[0] == 2){
     			if(m[2]+i[2]<m[1]){
     				m[1] = m[2] + i[2];
     				flag = 1;
     				System.out.println("     2-》1   ");
     			}
     			if(m[2]+i[3]<m[3]){
     				m[3] = m[2] + i[3];
     				flag = 1;
     				System.out.println("     2-》3   ");
     			}
     		}
     		if(i[0] == 3){
     			if(m[3]+i[2]<m[1]){
     				m[1] = m[3] + i[2];
     				flag = 1;
     				System.out.println("     3-》1   ");
     			}
     			if(m[3]+i[3]<m[2]){
     				m[2] = m[3] + i[3];
     				flag = 1;
     				System.out.println("     3-》2   ");
     			}
     		}
     		if(flag == 1){
     			update0();
     			System.out.println(m[0]+"		"+m[1]+"		"+m[2]+"		"+m[3]);
     			System.out.println("----		----		----		----");
     			 System.out.println("         ");
     			 
     		}
 	 		System.out.println();System.out.println();
     	}
 		
	
	}
	     
}

 
import java.net.*;
import java.util.*;
public class Node1{
	int[] m = new int[4];
	int[] i = new int[4];
	int n;
	int flag;
	DatagramSocket serverSocket;
	String send;
	byte[] sendData;
	byte[] receiveData;
	DatagramPacket sendPacket;
	DatagramPacket receivePacket;
	StringTokenizer st;
	public  void init1() throws Exception{
		 m[0] = 1;
		 m[1] = 1;
		 m[2] = 1;
		 m[3] = 999;
	     send=m[0]+" "+m[1]+" "+m[2]+" "+m[3]+" ";
	     System.out.println("       :");
		 System.out.println("   		 node0		 node2		 node3");
		 System.out.println("1		1		1		999");
		 System.out.println("----		----		----		----");
		 System.out.println();
	     sendData = new byte[100];
	     receiveData = new byte[100];
	     sendData = send.getBytes();
	     InetAddress sendto = InetAddress.getByName("localhost");
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
	     serverSocket.send(sendPacket);
	}
	public void update1() throws Exception{
		 send=m[0]+" "+m[1]+" "+m[2]+" "+m[3];
	     sendData = new byte[100];
	     sendData = send.getBytes();
	     InetAddress sendto = InetAddress.getByName("localhost");
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
	     serverSocket.send(sendPacket);
	}
	public static void main(String args[]) throws Exception
	{
		new Node1();
	}
	public Node1() throws Exception{
		serverSocket = new DatagramSocket(9001);
		try
		{
		Thread.currentThread();
		Thread.sleep(20000);//   
		}
		catch(Exception e){}
		init1();
		while(true){
			flag = 0;
			receivePacket = new DatagramPacket(receiveData,receiveData.length );
	     	serverSocket.receive(receivePacket);
	     	String sentence=new String(receivePacket.getData());
	     	st = new StringTokenizer(sentence);
	     	n = 0;
	     	while(st.hasMoreTokens() && n<4){
	     		i[n] = Integer.parseInt(st.nextToken());
	     		n++;
	     	}
     		if(i[0] == 0){
     			if(m[1]+i[2]<m[2]){
     				m[2] = m[1] + i[2];
     				flag = 1;
     				System.out.println("     0-》2   ");
     			}
     			if(m[1]+i[3]<m[3]){
     				m[3] = m[1] + i[3];
     				flag = 1;
     				System.out.println("     0-》3   ");
     			}
     		}
     		if(i[0] == 2){
     			if(m[2]+i[1]<m[1]){
     				m[1] = m[2] + i[1];
     				flag = 1;
     				System.out.println("     2-》1   ");
     			}
     			if(m[2]+i[3]<m[3]){
     				m[3] = m[2] + i[3];
     				flag = 1;
     				System.out.println("     2-》3   ");
     			}
     		}
     		if(flag == 1){
     			update1();
     			System.out.println(m[0]+"		"+m[1]+"		"+m[2]+"		"+m[3]);
     			System.out.println("----		----		----		----");
     			System.out.println("         ");System.out.println();
     		}
     		
		}
	}
	     
}

 
import java.net.*;
import java.util.*;
public class Node2{
	int[] m = new int[4];
	int[] i = new int[4];
	int n;
	int flag;
	DatagramSocket serverSocket;
	String send;
	byte[] sendData;
	byte[] receiveData;
	DatagramPacket sendPacket;
	DatagramPacket receivePacket;
	StringTokenizer st;
	public  void init2() throws Exception{
		 
		 m[0] = 2;
		 m[1] = 3;
		 m[2] = 1;
		 m[3] = 2;
	     send=m[0]+" "+m[1]+" "+m[2]+" "+m[3]+" ";
	     System.out.println("       :");
		 System.out.println("   		 node0		 node1		 node3");
		 System.out.println("2		3		1		2");
		 System.out.println("----		----		----		----");
		 System.out.println();
	     sendData = new byte[100];
	     receiveData = new byte[100];
	     sendData = send.getBytes();
	     InetAddress sendto = InetAddress.getByName("localhost");
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9001);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
	     serverSocket.send(sendPacket);
	}
	public void update2() throws Exception{
		 send=m[0]+" "+m[1]+" "+m[2]+" "+m[3];
	     sendData = new byte[100];
	     sendData = send.getBytes();
	     InetAddress sendto = InetAddress.getByName("localhost");
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9001);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
	     serverSocket.send(sendPacket);
	}
	public static void main(String args[]) throws Exception
	{
		
		Node2 node = new Node2();
	}
	public Node2() throws Exception{
		serverSocket = new DatagramSocket(9002);
		try
		{
		Thread.currentThread();
		Thread.sleep(20000);//   
		}
		catch(Exception e){}
		init2();
		while(true){
			flag = 0;
			receivePacket = new DatagramPacket(receiveData,receiveData.length );
	     	serverSocket.receive(receivePacket);
	     	String sentence=new String(receivePacket.getData());
	     	st = new StringTokenizer(sentence);
	     	n = 0;
	     	while(st.hasMoreTokens() && n<4){
	     		i[n] = Integer.parseInt(st.nextToken());
	     		n++;
	     	}
     		if(i[0] == 0){
     			if(m[1]+i[1]<m[2]){
     				m[2] = m[1] + i[1];
     				flag = 1;
     				System.out.println("     0-》1   ");
     			}
     			if(m[1]+i[3]<m[3]){
     				m[3] = m[1] + i[3];
     				flag = 1;
     				System.out.println("     0-》3   ");
     			}
     		}
     		if(i[0] == 1){
     			if(m[2]+i[1]<m[1]){
     				m[1] = m[2] + i[1];
     				flag = 1;
     				System.out.println("     1-》0   ");
     			}
     			if(m[2]+i[3]<m[3]){
     				m[3] = m[2] + i[3];
     				flag = 1;
     				System.out.println("     1-》3   ");
     			}
     		}
     		if(i[0] == 3){
     			if(m[3]+i[1]<m[1]){
     				m[1] = m[3] + i[1];
     				flag = 1;
     				System.out.println("     3-》0   ");
     			}
     			if(m[3]+i[2]<m[2]){
     				m[2] = m[3] + i[2];
     				flag = 1;
     				System.out.println("     3-》2   ");
     			}
     		}
     		if(flag == 1){
     			update2();
     			System.out.println(m[0]+"		"+m[1]+"		"+m[2]+"		"+m[3]);
     			System.out.println("----		----		----		----");
     			System.out.println("         ");System.out.println();
     		}
		}
	}
	     
}

 
package com.hzy;

import java.net.*;
import java.util.*;
public class Node3{
	int[] m = new int[4];
	int[] i = new int[4];
	int n;
	int flag;
	DatagramSocket serverSocket;
	String send;
	byte[] sendData;
	byte[] receiveData;
	DatagramPacket sendPacket;
	DatagramPacket receivePacket;
	StringTokenizer st;
	public  void init3() throws Exception{
		 
		 m[0] = 3;
		 m[1] = 7;
		 m[2] = 999;
		 m[3] = 2;
	     send=m[0]+" "+m[1]+" "+m[2]+" "+m[3]+" ";

		 System.out.println("       :");
		 System.out.println("   		 node0		 node1		 node2");
		 System.out.println("3		7		999		2");
		 System.out.println("----		----		----		----");
		 System.out.println();
	     sendData = new byte[100];
	     receiveData = new byte[100];
	     sendData = send.getBytes();
	     InetAddress sendto = InetAddress.getByName("localhost");
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
	     serverSocket.send(sendPacket);
	}
	public void update3() throws Exception{
		 send=m[0]+" "+m[1]+" "+m[2]+" "+m[3];
	     sendData = new byte[100];
	     sendData = send.getBytes();
	     InetAddress sendto = InetAddress.getByName("localhost");
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
	     serverSocket.send(sendPacket);
	     sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
	     serverSocket.send(sendPacket);
	}
	public static void main(String args[]) throws Exception
	{
		Node3 node = new Node3();
	}
	public Node3() throws Exception{
		serverSocket = new DatagramSocket(9003);
				try
				{
				Thread.currentThread();
				Thread.sleep(20000);//   
				}
				catch(Exception e){}
				init3();
				while(true){
					flag = 0;
					receivePacket = new DatagramPacket(receiveData,receiveData.length );
			     	serverSocket.receive(receivePacket);
			     	String sentence=new String(receivePacket.getData());
			     	st = new StringTokenizer(sentence);
			     	n = 0;
			     	while(st.hasMoreTokens() && n<4){
			     		i[n] = Integer.parseInt(st.nextToken());
			     		n++;
			     	}
		     		if(i[0] == 0){
		     			if(m[1]+i[1]<m[2]){
		     				m[2] = m[1] + i[1];
		     				flag = 1;
		     				System.out.println("     0-》1   ");
		     			}
		     			if(m[1]+i[2]<m[3]){
		     				m[3] = m[1] + i[2];
		     				flag = 1;
		     				System.out.println("     0-》2   ");
		     			}
		     		}
		     		if(i[0] == 2){
		     			if(m[3]+i[1]<m[1]){
		     				m[1] = m[3] + i[1];
		     				flag = 1;
		     				System.out.println("     2-》0   ");
		     			}
		     			if(m[3]+i[2]<m[2]){
		     				m[2] = m[3] + i[2];
		     				flag = 1;
		     				System.out.println("     2-》1   ");
		     			}
		     		}
		     		if(flag == 1){
		     			update3();
		     			System.out.println(m[0]+"		"+m[1]+"		"+m[2]+"		"+m[3]);
		     			System.out.println("----		----		----		----");
		     			System.out.println("         ");System.out.println();
		     		}
     		
		}
	}
	     
}
 

좋은 웹페이지 즐겨찾기