2014-03-27 140 views
2

我想在java中实现计算器客户端服务器程序,我的客户端有GUI,客户端将数据发送到服务器并返回到该服务器。 但简单的问题是无论输入gui是不会去服务器java客户端服务器套接字编程实现问题

我正在实施这个问题,但它不会工作... 在这里,我试图发送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(); 
     } 
    } 
} 

回答

1

在您的客户端的主要方法你关闭套接字,并几乎立刻输出流。 GUI在另一个线程中工作(不在主线程中)。当你点击发送(或任何按钮发送数据到服务器)时,它会发现套接字和数据流关闭,不会发送任何东西。

+0

谢谢你...现在它工作 –

1

在客户端连接到服务器,并立即关闭连接在这里:

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

如果删除线,你会看到所有的作品。

此外,您的连接必须全部打开,而您的客户端正在工作,并且服务器必须读取while(true)循环中的值。

+0

谢谢你..现在工作。但先生需要改变这里,使我的服务器类多线程。 –

+0

可能[that](http://stackoverflow.com/questions/12588476/multithreading-socket-communication-client-server)可以帮助你。 – alex2410

+0

ohk ...我引用它... –