本习题来自《java语言程序设计--进阶篇》第30章,网络编程的习题。
题目描述:为一个客户端编写一个服务器。客户端向服务器发送贷款信息(年利率、贷款年限和贷款总额)。服务器计算月偿还额和总偿还额,并把它们发回给客户端。将客户端程序命名为:Exercise30_1Client,将服务器程序命名为Exercise30_1Server。
package exercise.chapter30; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; /* * 贷款客户端 * 日期:2015.5.17 */ public class Exercise30_1Client extends JFrame { private JPanel center = new JPanel(); private JPanel south = new JPanel(); private JTextArea jta = new JTextArea(8, 10); private JButton jbt = new JButton("Submit"); //创建标签和文本域 private JLabel jlb1 = new JLabel("税率"); private JTextField rate = new JTextField(); private JLabel jlb2 = new JLabel("贷款年数"); private JTextField year = new JTextField(); private JLabel jlb3 = new JLabel("贷款总数"); private JTextField amount = new JTextField(); public Exercise30_1Client() { //设计GUI布局 center.setLayout(new GridLayout(3, 2)); south.setLayout(new BorderLayout()); center.add(jlb1); center.add(rate); center.add(jlb2); center.add(year); center.add(jlb3); center.add(amount); jta.setEditable(false); jta.setFont(new Font("", Font.BOLD, 15)); south.add(new JScrollPane(jta), BorderLayout.CENTER); south.add(jbt, BorderLayout.EAST); this.setLayout(new BorderLayout()); this.add(center, BorderLayout.CENTER); this.add(south, BorderLayout.SOUTH); this.setTitle("Exercise30_1Client"); this.setSize(400, 300); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设计socket try { Socket socket = new Socket("localhost", 8000); jbt.addActionListener(new MyActionListener(socket)); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new Exercise30_1Client(); } private class MyActionListener implements ActionListener { Socket socket = null; public MyActionListener(Socket socket) { this.socket = socket; } @Override public void actionPerformed(ActionEvent arg0) { try { DataOutputStream output = new DataOutputStream(socket.getOutputStream()); DataInputStream input = new DataInputStream(socket.getInputStream()); double rateStr = Double.parseDouble(rate.getText().trim()); double yearStr = Double.parseDouble(year.getText().trim()); double amountStr = Double.parseDouble(amount.getText().trim()); String message = rateStr + "@" + yearStr + "@" + amountStr; output.write(message.getBytes()); output.flush(); byte[] buf = new byte[1024]; int len = 0; //文本域清零 rate.setText(""); year.setText(""); amount.setText(""); double amount = input.readDouble(); //向jta中追加数据 jta.append("税率: " + rateStr + ", 贷款年数:" + yearStr + ", 贷款总额: " + amountStr + " ,偿还总额:" + amount); } catch (IOException e) { e.printStackTrace(); } } } }
import javax.swing.*; import java.awt.*; import java.io.*; import java.net.*; import java.util.*; /* *功能:贷款服务器 *日期:2015.5.17 */ public class Exercise30_1Server extends JFrame { private JTextArea jta = new JTextArea(); public Exercise30_1Server() { this.setTitle("Exercise30_1Server"); this.setSize(400, 300); this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jta.setFont(new Font("Font.ITALIC",Font.PLAIN, 15)); jta.setEditable(false); this.setLayout(new BorderLayout()); this.add(new JScrollPane(jta), BorderLayout.CENTER); jta.append("Server started at " + new Date() + '\n'); try { ServerSocket ss = new ServerSocket(8000); Socket socket = ss.accept(); //获取InetAddress类 InetAddress inetAddress = socket.getInetAddress(); jta.append("Client's hostname is: " + inetAddress.getHostName()); jta.append("\n"); jta.append("Client's ip is: " + inetAddress.getHostAddress()); jta.append("\n"); DataInputStream input = new DataInputStream(socket.getInputStream()); DataOutputStream output = new DataOutputStream(socket.getOutputStream()); byte[] buf = new byte[1024]; int len = 0; while((len = input.read(buf)) != -1) { double amount = getAmount(new String(buf, 0, len)); output.writeDouble(amount); jta.append(new String(buf, 0, len)); jta.append("\n"); } } catch (IOException e) { e.printStackTrace(); } } private static double getAmount(String str) { String[] str1 = str.split("@"); double rate = Double.parseDouble(str1[0]); double years = Double.parseDouble(str1[1]); double amount = Double.parseDouble(str1[2]); return rate * years * amount + amount; } public static void main(String[] args) { JFrame frame = new Exercise30_1Server(); } }