Monday, June 16, 2014

SIMPLE NETWORK PROGRAMS


PROGRAM TO FIND IP-ADDRESS OF A COMPUTER:
import java.net.*;
class Inettest
{
    public static void main(String args[]) throws UnknownHostException
    {
        InetAddress Address=InetAddress.getLocalHost();
        System.out.println(Address);
        Address=InetAddress.getByName("www.sastra.edu");
        System.out.println(Address);
        InetAddress sw[]=InetAddress.getAllByName("www.google.com");
        for(int i=0;i<sw.length;i++)
            System.out.println(sw[i]);
    }
}

output:-
Image

Program for using Ping command:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class pingip
{
  public static void runSystemCommand(String command){

        try {
            Process p = Runtime.getRuntime().exec(command);
            BufferedReader inputStream = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));

            String s = "";
            // reading output stream of the command
            while ((s = inputStream.readLine()) != null) {
                System.out.println(s);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
      
        String ip = "google.com";
        runSystemCommand("ping " + ip);

  
    }
}
output:-
Image

Program to send messages to other users in a network:-
import java.io.*;
  import java.net.*;
  class netsend{
   public static void main(String args[]) throws Exception
    {
    try{
        Runtime r=Runtime.getRuntime();
        Process p=null;
        String msg;
        String TRIP;
        String cmd;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter ip address of remote host");
        TRIP=br.readLine();
        System.out.println("Enter the msg to be sent to remote host");
        msg=br.readLine();
        cmd="net send"+TRIP+" "+msg;
        p=r.exec(cmd);
        Thread.sleep(1000);
        System.out.println("msg  sent to the sysytem");
        }catch(Exception e){
          System.out.println(e);
        }
      }

    }
output:-


0 comments:

Post a Comment