Aim: -
Create an application to find the factorial of given number using TCP/IP
Tools: -
NetBeans IDE, jdk
Program:-
Server.java
package
file_transfer;
import
java.io.*;
import
java.net.ServerSocket;
import
java.net.Socket;
import
sun.net.www.http.ChunkedOutputStream;
public
class server {
public static void main(String[] args)
throws Exception{
ServerSocket ss1=new ServerSocket(777);
Socket s1=ss1.accept();
System.out.println("Server is
Ready...");
BufferedReader kb1=new
BufferedReader(new InputStreamReader(s1.getInputStream()));
String fname=kb1.readLine();
System.out.println("Received
Number From Client : " + fname);
int num=Integer.parseInt(fname);
System.out.println("Counting
Factorial ...");
int fact= factorial(num);
System.out.println("Send Factorial
Value Of Number To Client ...");
PrintStream dos1=new
PrintStream(s1.getOutputStream());
dos1.println(String.valueOf(fact));
s1.close();
ss1.close();
}
public static int factorial(int n){
if(n==1)
return 1;
else
return n*factorial(n-1);
}
}
Client.java
package
file_transfer;
import
java.io.*;
import
java.net.Socket;
public
class client {
public static void main(String args[])
throws Exception{
Socket s=new
Socket("localhost", 777);
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Number To
Find Factorial : ");
String fname=br.readLine();
PrintStream dos=new
PrintStream(s.getOutputStream());
dos.println(fname);
System.out.println("Send Number To
Server...");
BufferedReader kb=new
BufferedReader(new InputStreamReader(s.getInputStream()));
String sts=kb.readLine();
System.out.println("Received
Factorial Value From Server....");
System.out.println("Factorial Of
" + fname + " is : " + Integer.parseInt(sts));
s.close();
}
}
Output:-
0 Comments