Java program for the Implementation of BOOTP Protocol - C C++ Java Programs - Examples

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Wednesday, October 12, 2011

Java program for the Implementation of BOOTP Protocol


package com.gnec;

//import java.io.*;
import java.net.*;
import java.util.*;

public class DHCP
{
            public static int serverPort=67;
            public static DatagramSocket ds;
            public static String address[]={ new String("0050486281A0"),
                                                             new String("0050486281B1"),
                                                             new String("0050486281C2")
                                                            };
            public static void DHCPServer()throws Exception
            {
                        byte b[]=new byte[1024];
                        byte r[];
                        DatagramPacket p;
                        String ip[]={ new String("192.168.1.11"),
                                          new String("192.168.1.12"),
                                          new String("192.168.1.13")
                                         };
                        String gateway=new String("192.168.1.1");
                        String bootfile=new String("Startup.dat");
                        System.out.println("This is the DHCP Server");
                        //boolean bool=true;
                        while(true)
                        {
                                    for(int i=0;i<1024;i++)
                                    b[i]=0;
                                    p=new DatagramPacket(b,b.length);
                                    ds.receive(p);
                                    String req=new String(b,0,p.getLength());
                                    System.out.println("Got the Request:"+req);
                                    int x=-1;
                                    for(int i=0;i<address.length;i++)
                                    {
                                                if(req.equals(address[i]))
                                                x=i;
                                    }
                                    String res;
                                    if(x==-1)
                                                res=new String("0.0.0.0");
                                    else
                                                res=ip[x]+":"+gateway+":"+bootfile;
                                    r=res.getBytes();
                                    ds.send(new DatagramPacket(r,r.length,p.getAddress(),p.getPort()));
                        }
            }

            public static void DHCPClient(int x)throws Exception
            {         
                        byte rec[]=new byte[1024];
                        byte b[];
                        DatagramPacket p ;
                        String req;
                        System.out.println("This is the DHCP Client");
                        if(x>=address.length)
                                    req=new String("000000000000");
                        else
                                    req=address[x];
                        System.out.println("Sending Request HA:"+req);
                        b=req.getBytes();
                        ds.send(new DatagramPacket(b,b.length,InetAddress.getLocalHost(),serverPort));
                        p=new DatagramPacket(rec,rec.length);
                        ds.receive(p);
                        String res=new String(rec,0,p.getLength());
                        StringTokenizer st=new StringTokenizer(res,":");
                        String ip=null;
                        String gateway=null;
                        String bootfile=null;
                        String str=null;
                        int i=0;
                        while(st.hasMoreTokens())
                        {
                                    str=st.nextToken();
                                    if(i==0)
                                                ip=str;
                                    else if(i==1)
                                                gateway=str;
                                                else if(i==2)
                                                            bootfile=str;
                                    i++;
                        }
                        if(i==1)
                        {
                                    System.out.println("Negative Response from the server");
                        }
                        else
                        {
                                    System.out.println("Got Response:");
                                    System.out.println("IP Address:"+ip);
                                    System.out.println("Gateway:"+gateway);
                                    System.out.println("Boot File:"+bootfile);
                        }
            }

            public static void main(String args[]) throws Exception
            {         
                        if(args.length==1)
                        {

                                    ds=new DatagramSocket();
                                    DHCPClient(Integer.parseInt(args[0]));
                        }
                        else
                        {
                                    ds=new DatagramSocket(serverPort);
                                    DHCPServer();
                        }
            }
}
Expected Output:
E:\gnec\test>java DHCP
This is the DHCP Server
Got the Request:0050486281A0

E:\gnec\test>java DHCP 0
This is the DHCP Client
Sending Request HA:0050486281A0
Got Response:
IP Address:192.168.1.11
Gateway:192.168.1.1
Boot File:Startup.dat
(2)
E:\gnec\test>java DHCP
This is the DHCP Server
Got the Request:0050486281A0
Got the Request:0050486281B1

E:\gnec\test>java DHCP 1
This is the DHCP Client
Sending Request HA:0050486281B1
Got Response:
IP Address:192.168.1.12
Gateway:192.168.1.1
Boot File:Startup.dat
(3)
E:\gnec\test>java DHCP
This is the DHCP Server
Got the Request:0050486281A0
Got the Request:0050486281B1
Got the Request:0050486281C2
Got the Request:000000000000

E:\gnec\test>java DHCP 3
This is the DHCP Client
Sending Request HA:000000000000
Negative Response from the server

No comments:

Post a Comment