Advance Networking Lab File
Submitted to:               Submitted By:
Ms. Anu Gupta               Gaurav Mishra
                             A2305207151
                             Roll No – 544
                             Sec-6CS3
                             Batch - 2007-11
                       INDEX
S. No.   Description           Date   Signature
 1. Write a program to obtain a character through keyboard using buffered
reader object.
import java.io.*;
public class test
{
 public static void main(String args[]) throws IOException
 {
  InputStreamReader ip=new InputStreamReader(System.in);
  BufferedReader ob=new BufferedReader(ip);
  System.out.println("enter a character");
  char c=(char)ob.read();
  System.out.println("you have entered "+c);
 }
 }
Output:
D:\jdk\bin>javac test.java
D:\jdk\bin>java test
enter a character
s
You have entered s
 2. Write a program to obtain a string through keyboard using buffered
reader object.
import java.io.*;
public class test2
{
 public static void main(String args[]) throws IOException
 {
  InputStreamReader ip=new InputStreamReader(System.in);
  BufferedReader ob=new BufferedReader(ip);
  System.out.println("enter the string");
 String c=ob.readLine();
 System.out.println("you have entered "+c);
}
}
Output:
D:\jdk\bin>javac test2.java
D:\jdk\bin>java test2
enter the string
java
you have entered java
3. Write a program to find out which of the first 1024 ports seem to be
hosting on a specified host.
import java.net.*;
import java.io.*;
public class soctest
{
 public static void main(String args[]) throws UnknownHostException
 {
  String host="local host";
  for(int i=0;i<=1024;i++)
  {
    try
   {
    Socket s=new Socket(host,i);
    System.out.println(host+"port number "+i);
    }
      catch(Exception e)
      { System.out.println("exception occured "+e);
      }
  }
 }
}
Output:
D:\jdk\bin>javac soctest.java
D:\jdk\bin>java soctest
exception occurred java.net.UnknownHostException: local host
4. Write a program to print the address and name of the local machine and
those of two well-known websites.
import java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws
UnknownHostException {
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("www.timesofindia.com");
System.out.println(Address);
InetAddress SW[] = InetAddress.getAllByName("www.espnstar.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}
Output:
D:\jdk\bin>javac InetAddressTest.java
D:\jdk\bin>java InetAddressTest
User-PC/10.0.125.165
www.timesofindia.com/203.199.93.69
www.espnstar.com/210.212.26.24
www.espnstar.com/210.212.26.19
5.Write a program to implement one-way chatting.
Client
import java.net.*;
import java.io.*;
class client1way
{
  static void p(String s)
  {
    System.out.println(s);
  }
  public static void main(String args[]) throws IOException, UnknownHostException
  {
    String s1,s2;
    Socket s = new Socket("localhost",1010);
    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    DataOutputStream ds1 = new DataOutputStream(s.getOutputStream());
    p("Say something");
    s1 = br1.readLine();
    ds1.writeBytes(s1 + '\n');
  }
}
 Server
import java.net.*;
import java.io.*;
class server1way
{
  static void p(String s)
  {
    System.out.println(s);
  }
  public static void main(String args[]) throws IOException,UnknownHostException
  {
    String s2;
    ServerSocket ss = new ServerSocket(1010);
    while(true)
    {
       Socket s = ss.accept();
       BufferedReader br1 = new BufferedReader(new
InputStreamReader(s.getInputStream()));
       s2 = br1.readLine();
       p("Friend Says");
            p(s2);
        }
    }
}
Output:
6. Write a program to implement two-way chatting.
Client
import java.io.*;
import java.net.*;
class client2way
{
  static void p(String s)
{
  System.out.println(s);
}
public static void main(String args[])throws IOException,UnknownHostException
{
  String s1,s2;
  Socket s=new Socket("Localhost",1010);
     //this line will execute only if there is a socket at client sid
BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in)); //input
from client side
 DataOutputStream dos=new DataOutputStream(s.getOutputStream()); //out from client
to server i.e request
 p("say something....");
 s1=bclient.readLine(); //to read from client end
 dos.writeBytes(s1+'\n');
 while(true)
   {
    BufferedReader br=new BufferedReader(new
InputStreamReader(s.getInputStream())); //in from client
    s2=br.readLine(); //to read from client end
    p("friend says");
    p(s2);
}}}
Server
import java.io.*;
import java.net.*;
class server2way
{
  static void p(String s)
{
  System.out.println(s);
}
public static void main(String args[])throws IOException,UnknownHostException
{
  String s1,s2;
  ServerSocket ss=new ServerSocket(1010);
  while(true)
    {
     Socket s=ss.accept();
     BufferedReader br=new BufferedReader(new
InputStreamReader(s.getInputStream())); //in from client
     s1=br.readLine(); //to read from client end
     p("friend says");
     p(s1);
BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in)); //input
from client side
  DataOutputStream dos=new DataOutputStream(s.getOutputStream()); //out from client
to server i.e request
  p("say something....");
 s2=bclient.readLine(); //to read from client end
  dos.writeBytes(s2+'\n');
 }}
}
Output:
7. Program to convert a lowercase string into uppercase string using Socket
programming.
Myserver45.java
import java.lang.*;
import java.net.*;
import java.io.*;
class myserver45
{
  static void p (String s)
  { System.out.println(s);
  }
  public static void main (String args[])throws IOException,UnknownHostException
  {
         String s1,s2;
         ServerSocket ss=new ServerSocket(1001);
         Socket s= ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
         DataOutputStream dos1= new DataOutputStream(s.getOutputStream());
         s1=br.readLine();
         p("friend says");
         p(s1);
         s2=s1.toUpperCase();
         dos1.writeBytes(s2+'\n');
  }
}
Myclient45.java
import java.io.*;
import java.net.*;
public class myclient45
{
  public static void main(String args[]) throws IOException,UnknownHostException
  {
    String sin,sout;
    Socket s= new Socket("localhost",1001);
  BufferedReader bclient= new BufferedReader(new InputStreamReader(System.in));
  DataOutputStream dos= new DataOutputStream(s.getOutputStream());
  BufferedReader bserver= new BufferedReader(new
InputStreamReader(s.getInputStream()));
  System.out.print("enter string: ");
  sin=bclient.readLine();
  dos.writeBytes(sin+"\n");
  sout=bserver.readLine();
  System.out.println(sout);
     s.close();
 }
}
OUTPUT:-
Serverside:-
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\amity>cd\
C:\>cd pro*\java\jd*\bin
C:\Program Files\Java\jdk1.6.0_18\bin>javac myclient45.java
C:\Program Files\Java\jdk1.6.0_18\bin>javac myserver45.java
C:\Program Files\Java\jdk1.6.0_18\bin>java myserver45
friend says
gaurav mittal
Client side:-
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\amity>cd\
C:\>cd pro*\java\jd*\bin
C:\Program Files\Java\jdk1.6.0_18\bin>java myclient45
enter string: gaurav mittal
GAURAV MITTAL
8. Program to get date and time from server side.
Client side:-
import java.io.*;
import java.net.*;
import java.util.*;
class DateClient
{
  static void p(String s)
  {
    System.out.println(s);
  }
  public static void main(String args[])throws IOException,UnknownHostException
  {
   String s1,s2;
   Socket s=new Socket("Localhost",1331);
     //this line will execute only if there is a socket at client side
    BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in));
    //input from client side
    DataOutputStream dos=new DataOutputStream(s.getOutputStream());
    //out from client to server i.e request
    p("TO get date and time from server.");
    p("Give a command for Date");
    s1=bclient.readLine(); //to read from client end
    dos.writeBytes(s1+'\n');
        BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
        //in from server to client
        s2=br.readLine(); //to read from Server End
        p("Server side Returns:-");
        p(s2);
        s.close();
    }
}
Server side:-
import java.io.*;
import java.net.*;
import java.util.*;
class DateServer
{
  static void p(String s)
  {
  System.out.println(s);
  }
 public static void main(String args[])throws IOException,UnknownHostException
 {
  String s1,s2;
  ServerSocket ss=new ServerSocket(1331);
  Date d1=new Date();
 while(true)
  {
   Socket s=ss.accept();
   BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
   s1=br.readLine(); //to read from client end
   p("Input From Client Side :-");
   p(s1);
 DataOutputStream dos=new DataOutputStream(s.getOutputStream());
   if(s1.equalsIgnoreCase("DATE"))
   {
    s2=d1.toString(); //to read from client end
   dos.writeBytes(s2+'\n');
   }
   else
   { s2="Not a Command!!!!!";
     dos.writeBytes(s2+'\n');
   }
  }
 }
}
OUTPUT:-
9. Program to encrypt a message at client side and decrypt it in server side.
Client side:-
import java.io.*;
import java.net.*;
class LocalClient
{
     public static void main(String args[])throws UnknownHostException,IOException
     {
           String sin,sout,s2="";
           char ch;
           System.out.println("Enter message : ");
           BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in));
           sin=bclient.readLine();
           for(int i=0;i<sin.length();i++)
           {
             ch=(char)(sin.charAt(i)+3);
             s2=s2+ch;
             }
           Socket s=new Socket("LocalHost",1001);
           DataOutputStream dos=new DataOutputStream(s.getOutputStream());
           dos.writeBytes(s2+'\n');
           s.close();
     }
}
Server side:-
import java.io.*;
import java.net.*;
public class LocalServer
{
     public static void main(String args[])throws IOException,UnknownHostException
     {
       try
        {
           String s1,s2="";
           char ch;
           ServerSocket SS=new ServerSocket(1001);
           System.out.println("\nListening Mode....");
           while(true)
           {
               Socket S=SS.accept();
         BufferedReader bin=new BufferedReader(new
InputStreamReader(S.getInputStream()));
               s1=bin.readLine();
               System.out.println("encrypted message is "+s1);
            for(int i=0;i<s1.length();i++)
            { ch=(char)(s1.charAt(i)-3);
                   s2=s2+ch;
                 }
                 System.out.println("decrypted message is "+s2);
             }
      }
      catch(Exception e)
     { }
    }
}
Output:-