You are on page 1of 9

/*Write a program that demonstrate the use of URL class and its methods.

*/

import java.io.*;

import java.net.*;

class URLDemo

{ public static void main (String ar[]) throws IOException

String st="http://admissions.puchd.ac.in/";

if (st.length != 1)

System.err.println("usage: java URLDemo url");

return;

URL url= new URL (st);

System.out.println("Authority=" + uri.getAuthority());

System.out.println ("File="+uri.getFile());

System.out.println ("Host="+url.getHost()); System.out.println ("Path="+url.getPath());

System.out.println ("Port="+url.getPort());

System.out.println ("Protocol="+url.getProtocol());

System.out.println ("Query =" +url.getQuery());

System.out.println ("Ref=" +url.getRef());

System.out.println ("User Info = "+url.getUserInfo());

System.out.print("\n");

InputStream is = url.openStream();

int ch;

while ((chis.read()) != -1)

System.out.print ((char) ch);

is.close();

}
/*Write a TCP client-server program: the client accepts a number from the user and sends it to the
server, the server returns the factorial of that number to the client.*/

➢ Client1

import java.net.*;

import java.io.*;

import java.util.Scanner;

public class Client1

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

int num;

Socket s= new Socket("localhost",100);

Scanner sc= new Scanner(System.in);

DataOutputStream dos= new DataOutputStream(s.getOutputStream());

DataInputStream dis = new DataInputStream(s.getInputStream());

System.out.println("Enter a number");

num= sc.nextInt();

System.out.println("Client application is sending request value");

dos.writeUTF("" + num);

String ans =(String) dis.readUTF();

System.out.println("Client program received result from server");

System.out.println("Square of" +num+ "is: " +ans);

s.close();

}
➢ Server1
import java.net.*;

import java.io.*;

class Server1

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

int f=1,i,n;

ServerSocket ss = new ServerSocket(100);

Socket s=ss.accept();

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

DataInputStream dis= new DataInputStream(s.getInputStream());

System.out.println("Server is waiting for request input from client");

String str= (String)dis.readUTF();

System.out.println("Server received input from client");

n = Integer.parseInt(str);

for(i=1;i<=n;i++)

f=f*i;

dos.writeUTF(""+f);

System.out.println("Server sent the response");

ss.close();

s.close();

}
/*Write a client-server program that accepts a user name from the client and sends a greeting
message 'Hello, <username>' to the client.*/

➢ Client2

import java.net.*;

import java.io.*;

class Client2

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

Socket s=new Socket("localhost", 100);

DataOutputStream dos= new DataOutputStream(s.getOutputStream());

DataInputStream dis= new DataInputStream(s.getInputStream());

System.out.println("Client application is sending user name");

dos.writeUTF("Ravi Majithia");

s.close();

}
➢ Server2
import java.net.*;

import java.io.*;

class Server2

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

int f=1,i,n;

ServerSocket ss=new ServerSocket(100);

Socket s=ss.accept();

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

DataInputStream dis = new DataInputStream(s.getInputStream());

String str= (String)dis.readUTF();

System.out.println("Server says, Hello" + str);

ss.close();

s.close();

}
/*Write a TCP client-server program: the client accepts three number from the user and sends it to
the server, the server returns the largest among those numbers.*/

➢ Client3

import java.io.*;

import java.net.*;

class Client3

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

int nl,n2,n3;

DataInputStream dis = new DataInputStream(System.in);

System.out.println("Enter 3 numbers");

nl =Integer.parseInt(dis.readLine());

n2 = Integer.parseInt(dis.readLine());

n3 = Integer.parseInt(dis.readLine());

Socket s= new Socket("localhost", 100);

DataInputStream dis2 = new DataInputStream(s.getInputStream());

DataOutputStream dos2 = new DataOutputStream(s.getOutputStream());

dos2.writeUTF(nl + "," +n2+","+n3);

String ans = dis2.readUTF();

System.out.println("Largest element is : "+ ans);

s.close();

}
➢ Server3

import java.io.*;

import java.net.*;

class Server3

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

ServerSocket ss = (new ServerSocket(100));

Socket s=ss.accept();

DataInputStream dis2=new DataInputStream(s.getInputStream());

DataOutputStream dos2= new DataOutputStream(s.getOutputStream());

String str = dis2.readUTF();

String nums[]=str.split(",");

int a=Integer.parseInt(nums[0]);

int b=Integer.parseInt(nums[1]);

int c=Integer.parseInt(nums[2]);

int lar;

if(a>b && a>c)

lar=a;

if(b>a && b>c)

lar=b; }

if(c>a && c>b)

{ lar=c;

dos2.writeUTF(" "+lar);

ss.close();

s.close();

}
/*Write a TCP client-server program: the client sends a console number to the server, the server
returns the square of that number. */

➢ Client4

import java.net.*;

import java.io.*;

class Client4

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

Socket s= new Socket("localhost", 100);

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

DataInputStream dis= new DataInputStream(s.getInputStream());

System.out.println("Client application is sending request value");

dos.writeUTF("5");

String ans =(String)dis.readUTF();

System.out.println("Client program received result from server");

System.out.println("Square of 5 is : " + ans);

s.close();

}
➢ Server4

import java.net.*;

import java.io.*;

class Server4

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

ServerSocket ss = new ServerSocket(100);

Socket s=ss.accept();

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

DataInputStream dis = new DataInputStream(s.getInputStream());

System.out.println("Server is waiting for request input from client");

String str= (String)dis.readUTF();

System.out.println("Server received input from client");

int n= Integer.parseInt(str);

int sq = n*n;

dos.writeUTF(""+sq);

System.out.println("Server sent the response");

ss.close();

s.close();

You might also like