Hi all can you throw some light on the resolution of my problem,should be simple ,but can not figured out .Here is my server and client class,The problem is server does not show the JOption pane with result.

public class Client {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        try {
              Socket conToServer =  new Socket("localhost", 433);
        DataOutputStream toServer  = new DataOutputStream(conToServer.getOutputStream());
      String radius =   JOptionPane.showInputDialog("Enter radius");
       String height =  JOptionPane.showInputDialog("Enter height");

          int rad =  Integer.parseInt(radius.trim());
          int h =  Integer.parseInt(height.trim());

          toServer.writeInt(rad);
          toServer.write(h);
          toServer.flush();
          JOptionPane.showMessageDialog(null,"Numbers are sent to server for calculation: " + rad + " and " + h);
        } catch (IOException | HeadlessException | NumberFormatException e) {
            System.err.println(e);
        }

    }

}

public class Server {
    public static void main(String[] args) {

        try {
            ServerSocket server = new ServerSocket(433);
            while (true) {                
                Socket conn = server.accept();

                HandleClient thread = new HandleClient(conn);
                thread.start();
            }
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
 class HandleClient extends Thread
{

     private Socket conToClient;
     Double pi = 3.14;
     int r = 0;
     int h =0;
     Double surfArea;

    public HandleClient(Socket conToClient) {
        this.conToClient = conToClient;
    }
    public void run()
    {
        try {
            DataInputStream fromClient = new DataInputStream(conToClient.getInputStream());
            while (true) {                
               r = fromClient.readInt();
               h = fromClient.readInt();
                   surfArea =  2*(pi * r*r)+ 2*(pi*h*r);
                JOptionPane.showMessageDialog(null,surfArea);

            }
        } catch (Exception e) {
            System.err.println(e);
        }

    }

}

Recommended Answers

All 2 Replies

Client line 19 you use write, which just writes the low byte of its int parameter, but in the client you readInt, which neeeds 4 bytes, so the readInt is still waiting for the last 3 bytes.

Thank you so much.It's always a problem with misspelling or lossing the focus on intelli in Java .I knew it something small.

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.