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();
        }
    }
}

Try debugging the code by adding println statements that print out the values of all the variables as they are used.
The print out will help you see what the problem is.

Can you elaborate more as i don't know how to debug code in java.
But i will try now.

ok so i print the no in server.java just after

int no=br.read();

looks like it is not receiving the proper value.
In this case i sent msg=6 but no is having 54 value.

Add calls to System.out.println("var=" + var);
to the code anyplace that the code changes the value or uses a variable.
Change var to be the name of the variables that your code uses. Like no, fact, i, msg

also 48 is not the factorial of 54, its not the factorial of any number either :P

!4= 24 & !5 = 120

Here's the problem: you are writing your data to the output stream as ASCII text, but you are reading from your input streams as raw bytes. It's essential that you read and write using the same format, eg if you print to the output then you should use readLine to read the text input, then convert the text to an int value if necessary.
Like Norm says - add more System.out.println statements to show the values of your variables as the code is executing - that will help you see where it's going wrong.

@NormR1: yes i did that , i think the problem is that no variable in server.java is not getting proper value.
@Philippe.Lahaie: yes that's the problem.No matter what i am entering i am getting 48 as factorial.

The println() method converts the int before writing it. Read the API doc for the PrintWriter class's println and print methods. It is explained there.
To see what is sent, change the println of no by adding this to the end of the println statment: + " " + (char)no
That will print the value of no by casting it to a char. You should see a 6 printed.

after adding those line it shows no=6 but still the factorial=48 is the output

In this case i sent msg=6 but no is having 54 value.

That's what I was trying to explain. You print the value 6, which sends the character '6' to the output stream as an ASCII character. The ASCII code for '6' is the numeric value 54. At the other end you read a single binary byte from the stream, which gives you the numeric value 54.
Of couse that doesn't explain why your claculation seems to be wong, but it does explain why its using the wrong data.

Continue adding println staements to the server. Make sure the code in the server is working correctly by printing out the values of all the variables used. The print out will show what is wrong.

@NormR1 , @JamesCherrill :
Thanks a lot, i got it working.Followed jamesCherrill's advice and converted string to int.
Now i am getting correct output.
Here is the updated code :

//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);
            String msg="5";
            pw.println(msg);
            msg=br.readLine();
            int gh=Integer.parseInt(msg) ;
            System.out.println("factorial="+gh);
            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);
                String no= br.readLine();
                //System.out.print("no:"+ " " + (char)no);
                int no1=Integer.parseInt(no);
                int fact=1,i=0;
                while(no1>i)
                {
                    fact=fact*no1;
                    no1--;
                    //System.out.println("fact="+fact+"no="+no1);
                }
                pw.println(fact);
                s.close();
            }
        }

        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Its essential that you fix the I/O formats in both directions before worrying about the factorial calculation.
Your algorithm overflows an int value with 54 as the input, and actually results in a zero result. You print '0', then read it as a byte - which is ASCII 48.

@JamesCherill : ok thanks a lot, i understood.

Sorry - my post crossed with your last one somewhere in cyberspace!
Mark this solved now?

yes i have marked it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.