cs2307protoarp

Upload: suresh-babu

Post on 24-Feb-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 cs2307protoarp

    1/4

    CS2307 Network Lab Socket Programming

    http://cseannauniv.blogspot.com Vijai Anand

    Exp# 5C ARP Client/Server

    Aim To know the physical address of a host when its logical address is known using ARP

    protocol.

    Algorithm

    Target/Server

    1. Create a server socket.2. Accept client connection.

    3. Read IP address from the client request

    4. Check its configuration file and compare with its logical address.

    5. If there is a match, send the host physical address.

    6. Stop

    Client

    1. Create a socket.

    2. Send IP address to the target machine

    3. Receive target's response

    4. If it is a MAC address then display it and go to step 6

    5. Display "Host not found"

    6. Stop

    Result

    Thus using ARP protoocl MAC address is obtained.

    http://cseannauniv.blogspot.com/http://cseannauniv.blogspot.com/
  • 7/25/2019 cs2307protoarp

    2/4

    CS2307 Network Lab Socket Programming

    http://cseannauniv.blogspot.com Vijai Anand

    Program

    //ARP Server -- arpserver.javaimport java.io.*;

    import java.net.*;

    class arpserver{

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

    {

    try

    {

    ServerSocket soc = new ServerSocket(2500); System.out.println("Server started");

    Socket client = null;

    client = soc.accept(); String str;

    PrintStream ps = newPrintStream(client.getOutputStream());

    BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

    Runtime r = Runtime.getRuntime();

    Process p = r.exec("ifconfig eth0");

    BufferedReader pin=new BufferedReader(new

    InputStreamReader(p.getInputStream()));

    String haddr = "";

    String ipaddr = br.readLine(); int flag = 0;

    while((str = pin.readLine())!=null) {

    System.out.println(str);

    if((str.indexOf("HWaddr")) != -1)

    {

    int tlen = str.length();

    int hlen = tlen - 19;

    haddr = str.substring(hlen,tlen); }

    else if ((str.indexOf(ipaddr)) != -1)

    { flag = 1;

    } }

    if (flag == 1)

    ps.println(haddr);

    ps.close();

    br.close();

    pin.close();

    client.close();

    http://cseannauniv.blogspot.com/http://cseannauniv.blogspot.com/
  • 7/25/2019 cs2307protoarp

    3/4

    CS2307 Network Lab Socket Programming

    http://cseannauniv.blogspot.com Vijai Anand

    soc.close();

    }

    catch(IOException io)

    {

    System.err.println("Exception : " + io.toString());

    }

    }}

    //ARP Client -- arpclient.java

    import java.io.*;

    import java.net.*;

    class arpclient

    { public static void main(String args[])

    {

    try {

    Socket client = new Socket("localhost", 2500); BufferedReader br = new BufferedReader(new

    InputStreamReader(System.in)); PrintStream ps = new

    PrintStream(client.getOutputStream());

    String ipaddr,haddr = null;

    BufferedReader sin = new BufferedReader(new

    InputStreamReader(client.getInputStream()));

    System.out.print("Enter the IP address : ");

    ipaddr = br.readLine(); ps.println(ipaddr);

    haddr = sin.readLine(); if (haddr == null)

    System.out.println("Host does not exist"); else

    System.out.println("Physical Address " + haddr);

    ps.close(); br.close();

    client.close();

    } catch(IOException io)

    { System.err.println(io.toString());

    }

    }}

    http://cseannauniv.blogspot.com/http://cseannauniv.blogspot.com/
  • 7/25/2019 cs2307protoarp

    4/4

    CS2307 Network Lab Socket Programming

    http://cseannauniv.blogspot.com Vijai Anand

    Output

    Server

    $ javac arpserver.java

    $ java arpserverServer startedeth0 Link encap:Ethernet HWaddr B8:AC:6F:1B:AB:06 inet addr:172.16.6.21 Bcast:172.255.255.255 Mask:255.0.0.0 inet6 addr: fe80::baac:6fff:fe1b:ab06/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:450 errors:0 dropped:0 overruns:0 frame:0

    TX packets:127 errors:0 dropped:0 overruns:0 carrier:0

    collisions:0 txqueuelen:1000 RX bytes:48118 (46.9 KiB) TX bytes:21025 (20.5 KiB) Interrupt:16

    Client

    $ javac arpclient.java

    $ java arpclient

    Enter the IP address : 172.16.6.21

    Physical Address B8:AC:6F:1B:AB:06

    http://cseannauniv.blogspot.com/http://cseannauniv.blogspot.com/