i have been coding a client server program to calculate factorial of a no .
The client will pass the no to the server whose factorial is to be calculated.
Now the problem that i am encountering is that no matter what value i am passing to client(variable msg in client program), the output i am getting is 48 . Here is my program.
// client.java
import java.net.*;
import java.io.*;
public class client
{
public static void main(String args[])
{
try
{
Socket s = new Socket("localhost",2222);
InputStream is=s.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
OutputStream os=s.getOutputStream();
PrintWriter pw= new PrintWriter(os,true);
int msg=6;
pw.println(msg);
msg=br.read();
System.out.println("factorial="+msg);
s.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
//server.java
import java.net.*;
import java.io.*;
public class server
{
public static void main(String args[])
{
try
{
ServerSocket ss =new ServerSocket(2222);
System.out.println("Server is started");
while(true)
{
Socket s=ss.accept();
System.out.print("Connection request Received");
InputStream is=s.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
OutputStream os=s.getOutputStream();
PrintWriter pw= new PrintWriter(os,true);
int no=br.read();
int fact=1,i=0;
while(no>i)
{
fact=fact*no;
no--;
}
pw.println(fact);
s.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}