【问题标题】:java client server socket programming implementation issuesjava客户端服务器socket编程实现问题
【发布时间】:2014-03-27 10:03:21
【问题描述】:

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

【问题讨论】:

    标签: java swing sockets


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

      • 谢谢你..现在它工作了。但是先生,这里需要改变什么来使我的服务器类成为多线程的。
      • 可以that帮助你。
      猜你喜欢
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      相关资源
      最近更新 更多