source file name: FactClient.java  

import java.net.*;  

import java.io.*;  

class FactClient

{  

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

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

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

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

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  

    System.out.println("enter any number");

String str="",str2="";  

str=br.readLine();  

dout.writeUTF(str);  

dout.flush();  

str2=din.readUTF();  

System.out.println(str2);  

  

dout.close();  

s.close();  

}

}  


source file name: FactServer.java

import java.net.*;  

import java.io.*;  

class FactServer

{  

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

{  

ServerSocket ss=new ServerSocket(3333);  

Socket s=ss.accept();  

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

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

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  

  System.out.println("server here..");

String str="",str2="";  


str=din.readUTF();  

 

int n=Integer.parseInt(str);

System.out.println("given no is: "+n);

int f=1;

for(;n>0;n--)

f=f*n;

System.out.println("Factorial value is: "+f);

str2="Factorial value is: "+f; 

dout.writeUTF(str2);  

dout.flush();  

  din.close();  

s.close();  

ss.close();  

}

}


output:

 D:\JavaPrograms\Networking>javac FactServer.java

D:\JavaPrograms\Networking>javac FactClient.java


server side view 

D:\JavaPrograms\Networking>java FactServer

server here..

given no is: 6

Factorial value is: 720


client side view  

D:\JavaPrograms\Networking>java FactClient

enter any number

6

Factorial value is: 720