java客户端服务器socket编程实现问题

时间:2014-03-27 10:03:22

标签: java swing sockets

我想在java中实现计算器客户端服务器程序,我的客户端有GUI,客户端将数据发送到服务器和服务器返回到该答案。 但简单的问题是进入gui的任何东西都不会去服务器

我正在实施这个问题,但它无法正常工作...... 在这里,我试图发送7到服务器。 任何人都可以了解如何操纵代码

无论我做了什么。

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

2 个答案:

答案 0 :(得分:1)

在客户端的main方法中,几乎可以立即关闭套接字和输出流。 GUI在另一个线程(不在主线程中)中工作。当您单击发送(或任何按钮将数据发送到服务器)时,它将找到套接字和流关闭,不会发送任何内容。

答案 1 :(得分:1)

在客户端中,您可以连接到服务器并立即关闭连接:

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

如果删除这些行,您会发现一切正常。

此外,您的连接必须始终打开,而您的客户端工作,服务器必须读取while(true)循环中的值。