Pradipsinh Chavda
Pradipsinh Chavda

Reputation: 31

java client server socket programming implementation issues

I want to implement calculator client server program in java and my client having GUI ,client send the data to server and server return to that answer. but simple problem is whatever enter into gui is not going to server

I am implementing this problem ,but it wont work ... Here i am trying to send 7 to server. can anybody give idea how manipulating the code

whatever I done is.

CalculatorClient.java

package com.example.dca;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;

public class CalculatorClient extends JFrame implements ActionListener {

    public static Socket s;
    public static DataOutputStream d;
    public static BufferedReader in;
    public static PrintWriter out;
    static JPanel[] row = new JPanel[5];
    static JButton[] button = new JButton[19];
    static String[] buttonString = { "7", "8", "9", "+", "4", "5", "6", "-",
        "1", "2", "3", "*", ".", "/", "C", "v", "+/-", "=", "0" };
    static int[] dimW = { 300, 45, 100, 90 };
    static int[] dimH = { 35, 40 };
    static Dimension displayDimension = new Dimension(dimW[0], dimH[0]);
    static Dimension regularDimension = new Dimension(dimW[1], dimH[1]);
    static Dimension rColumnDimension = new Dimension(dimW[2], dimH[1]);
    static Dimension zeroButDimension = new Dimension(dimW[3], dimH[1]);
    boolean[] function = new boolean[4];
    static double temp0;
    static int status;
    static double temp1;
    static JTextArea display = new JTextArea(1, 20);
    public static StringBuffer toSend = new StringBuffer();
    static Font font = new Font("Times new Roman", Font.BOLD, 14);

    private static void initGUI() throws UnknownHostException, IOException {
        JFrame f = new JFrame("Calculator");

        setDesign();
        f.setSize(380, 250);
        f.setResizable(false);
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        GridLayout grid = new GridLayout(5, 5);
        f.setLayout(grid);
        FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
        FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
        for (int i = 0; i < 5; i++)
        row[i] = new JPanel();
        row[0].setLayout(f1);
        for (int i = 1; i < 5; i++)
        row[i].setLayout(f2);

        for (int i = 0; i < 19; i++) {
            button[i] = new JButton();
            button[i].setText(buttonString[i]);
            button[i].setFont(font);
        }
        button[0].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("7");
                String s = display.getText();

                System.out.println("inner" + s);
                if (out != null) {
                    out.println(s);
                    //out.println("jaym");
                    out.flush();
                }
                display.setText(" ");
            }
        });
        button[1].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("8");
            }
        });
        button[2].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("9");
            }
        });
        button[3].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.out.println("additionn");
                temp0 = Double.parseDouble(display.getText());
                // function[0] = true;
                display.setText("");
                System.out.println("temp0" + temp0);
                try {
                    sendTemp0(temp0);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        button[4].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("4");
            }
        });
        button[5].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("5");
            }
        });
        button[6].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("6");
            }
        });
        button[7].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                // temporary[0] = Double.parseDouble(display.getText());
                // function[1] = true;
                display.setText("");
            }
        });
        button[8].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("1");
            }
        });
        button[9].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("2");
            }
        });
        button[10].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("3");
            }
        });
        button[11].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub multiply
                // temporary[0] = Double.parseDouble(display.getText());
                // function[2] = true;
                display.setText("");
            }
        });
        button[12].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append(".");
            }
        });
        button[13].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub division

            }
        });
        button[18].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                display.append("0");
            }
        });

        display.setFont(font);
        display.setEditable(false);
        display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        display.setPreferredSize(displayDimension);

        for (int i = 0; i < 14; i++)
        button[i].setPreferredSize(regularDimension);
        for (int i = 14; i < 18; i++)
        button[i].setPreferredSize(rColumnDimension);
        button[18].setPreferredSize(zeroButDimension);

        row[0].add(display);
        f.add(row[0]);

        for (int i = 0; i < 4; i++)
        row[1].add(button[i]);
        row[1].add(button[14]);
        f.add(row[1]);

        for (int i = 4; i < 8; i++)
        row[2].add(button[i]);
        row[2].add(button[15]);
        f.add(row[2]);

        for (int i = 8; i < 12; i++)
        row[3].add(button[i]);
        row[3].add(button[16]);
        f.add(row[3]);

        row[4].add(button[18]);
        for (int i = 12; i < 14; i++)
        row[4].add(button[i]);
        row[4].add(button[17]);
        f.add(row[4]);

        f.setVisible(true);
    }

    CalculatorClient() throws UnknownHostException, IOException {
    }

    private static void sendTemp0(double dd) throws IOException {
        toSend.append(dd);
    }

    public static void setDesign() {
        // TODO Auto-generated method stub
        try {
            UIManager
            .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) { }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        // TODO Auto-generated method stub
    }

    public static void main(String[] arguments) throws UnknownHostException, IOException {
        initGUI();
        s = new Socket("localhost", 1234);
        out = new PrintWriter(s.getOutputStream());
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        s.close();
        out.close();
        in.close();

    }
}

CalculatorServer.java

package com.example.dca;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class CalculatorServer {
    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            ServerSocket ss=new ServerSocket(1234);
            Socket s=ss.accept();

            System.out.println("conection done bapuu!!!");
            //To read file name from the client
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            // to send content of the of file
            System.out.println("reader.....");
            DataOutputStream out = new DataOutputStream(s.getOutputStream());
            // to read file name
            System.out.println("writer....");
            String sss=in.readLine();

            if(sss!=null)
            System.out.println(sss);

            ss.close();
            s.close();
            in.close();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Upvotes: 2

Views: 3235

Answers (2)

alex2410
alex2410

Reputation: 10994

In Client you connect to server and instantly close connection here :

 s.close();
 out.close();
 in.close();

If you remove that lines, you'll see that all works.

Also your connection must be opened all time,while your client working, and server must read values in while(true) loop for example.

Upvotes: 1

peter.petrov
peter.petrov

Reputation: 39457

In your client's main method you're closing the socket and the output stream almost instantly. The GUI works in another thread (not in the main thread). When you click send (or whatever button sends the data to the server), it will find the socket and the stream closed and won't send anything.

Upvotes: 1

Related Questions