Adv. NW Lab
Adv. NW Lab
SUBMITTED BY: NAME: CLASS: COURSE: BATCH: ENROLLMENT No.: Swati Gupta 6CS-8 B.Tech (CS & E) 2008 - 2012 A2305208222
INDEX
SR. NO. 1. 20/12/10 WAP to obtain a character through keyboard using BufferedReader. 2. 20/12/10 WAP to obtain a string from a BufferedReader object. 3. 03/01/11 WAP to print the IP address of two well known websites. (Use getLocalHost(), getByName(), getByAddress()) 4. 03/01/11 WAP to print the names of the local machine of two well known websites. (Use getLocalHost(), getByName(), getByAddress()) 5. 17/01/11 WAP for one-way chatting DATE NAME OF THE PROGRAM SIGN.
6.
17/01/11
7.
07/02/11
WAP to create server application in which client sends some string to the server and the server responds by converting the string to uppercase.
PROGRAM 1. WAP to obtain a character through keyboard using BufferedReader. import java.io.*; public class character { public static void main(String args[]) { try { BufferedReader br= new BufferedReader (new InputStreamReader(System.in)); char c; System.out.println("enter the character"); c=(char)br.read(); System.out.println(c); } catch(Exception e) { } } } OUTPUT:
PROGRAM 2. WAP to obtain a string from a BufferedReader object. import java.io.*; public class string { public static void main(String args[]) { try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s; System.out.println("ENTER THE STRING"); s=br.readLine(); System.out.println(s); } catch(Exception e) { } } } OUTPUT:
PROGRAM 3. WAP to print the IP address of two well known websites. (Use getLocalHost(), getByName(), getByAddress()) import java.net.*; class v { 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.MALAYALAMMANORAMA.com"); for (int i=0; i<SW.length; i++) { System.out.println(SW[i]); } } } OUTPUT:
PROGRAM 4. WAP to print the names of the local machine of two well known websites. (Use getLocalHost(), getByName(), getByAddress()) import java.net.*; class address { public static void main(String args[]) throws UnknownHostException { System.out.println("IP Add. of local host: "+(InetAddress.getLocalHost())); InetAddress Address1 = InetAddress.getByName("www.rediffmail.com"); System.out.println("IP Address of rediffmail is: "+ Address1); System.out.println(Address1.getHostName()); System.out.println("Site corresponding to IP Address" +Address1+" is: "+ Address1.getHostName()); System.out.println("IP Add. of local host: "+(InetAddress.getLocalHost())); InetAddress Address2 = InetAddress.getByName("www.amizone.net"); System.out.println("IP Address of rediffmail is: "+ Address2); System.out.println(Address2.getHostName()); System.out.println("Site corresponding to IP Address" +Address2+" is: "+ Address2.getHostName()); } }
OUTPUT:
PROGRAM 5. WAP for one-way chatting CLIENT: import java.io.*; import java.net.*; public class client { public static void main(String arg[]) throws IOException,UnknownHostException { Socket clientSocket; ObjectOutputStream out; ObjectInputStream in; String message; try { //1.creating a socket to connect to the server clientSocket=new Socket("localhost",1010); System.out.println("Connected to localhost in port 2005"); //2.get input and output streams out=new ObjectOutputStream(clientSocket.getOutputStream()); out.flush(); in=new ObjectInputStream(clientSocket.getInputStream()); //3. Communication via the input and output streams out.writeObject("Hello"); out.flush(); System.out.println("client>"+Hello); message=(String)in.readObject();
SERVER: import java.io.*; import java.net.*; public class Server { public static void main(String args[])throws IOException,UnknownHostException { ServerSocket serversocket; Socket connection=null; ObjectOutputStream out; ObjectInputStream in; String message; try {
//1.Creating a socket to connect to tjhe server serversocket=new ServerSocket(1010); //2. wait for the connection System.out.println("Waiting for the connection"); connection=serversocket.accept(); System.out.println("Connection recieved from"+connection.getLineAddress().getHostName); //3.get input and output Streams out=new ObjectOutputStream(connection.getOutputStream()); //4. communication via the input and output stream in=new ObjectInputStream(connection.getInputStream()); message=(String)in.readObject(); System.out.println("client>" + message); if(message.equals("Hello")) { out.writeObject("Hello"); out.flush(); System.out.println("Server>Hello"); } } catch (Exception e) { System.out.println("Exception found"); } } } OUTPUT:
PROGRAM 6. WAP for two-way chatting CLIENT: import java.io.*; import java.net.*; class chatclient1 { 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 bclient=new BufferedReader(new InputStreamReader(System.in)); //input from the client side DataOutputStream dos=new DataOutputStream(s.getOutputStream()); //out from client to server i.e. request p("say something..."); s1=bclient.readLine(); dos.writeBytes(s1+'\n'); while(true) { BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
SERVER: import java.io.*; import java.net.*; class chatserver1 { static void p(String s) { System.out.println(s); } public static void main(String args[]) throws Exception,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(); p("friend say.."); p(s1); BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in)); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); p("say something...."); s2=bclient.readLine(); //to read from client end dos.writeBytes(s2+'\n'); } } } OUTPUT:
PROGRAM 7. WAP to create server application in which client sends some string to the server and the server responds by converting the string to uppercase. CLIENT: import java.io.*; import java.net.*; class chatclient1 { 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 bclient=new BufferedReader(new InputStreamReader(System.in)); //input from the client side DataOutputStream dos=new DataOutputStream(s.getOutputStream()); //out from client to server i.e. request p("say something..."); s1=bclient.readLine(); dos.writeBytes(s1+'\n'); while(true) {
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); //IN from client s2=br.readLine(); p("friend say.."); p(s2); } } } OUTPUT:
SERVER: import java.io.*; import java.net.*; class chatserver1 { static void p(String s) { System.out.println(s); } public static void main(String args[]) throws Exception,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(); p("friend say.."); p(s1); BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in)); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); p("say something...."); s2=bclient.readLine(); //to read from client end dos.writeBytes(s2.toUpperCase()+'\n'); } } } OUTPUT: