编写一个网络计算程序。要求:
(1)客户端界面上有两个文本框,允许用户输入两个操作数,有4个复选框,分别是加、减、乘、除,一个按钮表示发送;
(2)服务器端监听数据,接受的信息包括操作数、操作符,根据操作数的类型(int、float、double)不同,选择不同的方法进行计算;
(3)将计算结果发回客户端,客户端在文本框内显示计算结果;
找了论坛的精华,只有计算的。
public class JServerSocket{
public static void main(String[] args){
int PORT=36547;
try{
ServerSocket serverSocket=new ServerSocket(PORT);
System.out.println("正在监听中");
while(true){
Socket socket=serverSocket.accept();
System.out.println(socket.getInetAddress());
new SocketServer(socket);
}
}catch(Exception exc){
exc.printStackTrace();
}
}
}
class SocketServer extends Thread{
private BufferedReader in;
private PrintWriter out;
private Socket socket;
public SocketServer(Socket s) throws IOException{
socket=s;
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
start();
}
public void run(){
try{
while(true){
double num1=Double.parseDouble(in.readLine());
double num2=Double.parseDouble(in.readLine());
int flags=Integer.parseInt(in.readLine());
double result=0;
switch(flags){
case 0:
result=num1+num2;
break;
case 1:
result=num1-num2;
break;
case 2:
result=num1*num2;
break;
case 3:
result=num1/num2;
break;
}
out.println(result);
}
}catch(Exception exc){
exc.printStackTrace();
}finally{
try{
socket.close();
}catch(IOException ex){}
}
}
}
客户端:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class JClientSocket extends JFrame implements ActionListener{
private JTextField jtfNum1,jtfNum2;
private JComboBox jcbFlags;
private JButton jbCompute;
private JLabel labelResult;
private JMenuBar jmenuBar;
private JMenu jmenuCtrl;
private JMenuItem jmenuConnect;
private BufferedReader in;
private PrintWriter out;
private Socket socket;
private int PORT=36547;
public JClientSocket(){
super("服务器端");
init();
this.setSize(350,150);
Dimension scrSize=this.getToolkit().getScreenSize();
Dimension frmSize=this.getSize();
int left=(scrSize.width-frmSize.width)/2;
int top=(scrSize.height-frmSize.height)/2;
this.setLocation(left,top);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
void init(){
jmenuBar=new JMenuBar();
jmenuCtrl=new JMenu("操作");
jmenuConnect=new JMenuItem("连接");
jmenuConnect.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.META_MASK));
jmenuConnect.addActionListener(this);
jmenuCtrl.add(jmenuConnect);
jmenuBar.add(jmenuCtrl);
this.setJMenuBar(jmenuBar);
Container container=this.getContentPane();
jtfNum1=new JTextField(10);
Vector<String> vector=new Vector<String>();
vector.addElement("+");
vector.addElement("-");
vector.addElement("*");
vector.addElement("/");
jcbFlags=new JComboBox(vector);
jtfNum2=new JTextField(10);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,3));
panel.add(jtfNum1);
panel.add(jcbFlags);
panel.add(jtfNum2);
container.add(panel,BorderLayout.NORTH);
labelResult=new JLabel("结果是:");
jbCompute=new JButton("计算");
jbCompute.setEnabled(false);
jbCompute.addActionListener(this);
container.add(labelResult,BorderLayout.CENTER);
JPanel panel2=new JPanel();
panel2.add(jbCompute);
container.add(panel2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jmenuConnect){
connectSocket();
}else if(e.getSource()==jbCompute){
getInfo();
}
}
void connectSocket(){
try{
socket=new Socket(InetAddress.getByName("localhost"),PORT);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
jbCompute.setEnabled(true);
}catch(Exception exc){
JOptionPane.showMessageDialog(null,exc.getMessage());
}
}
void getInfo(){
try{
double num1,num2;
String result;
int flags;
num1=Double.parseDouble(jtfNum1.getText());
num2=Double.parseDouble(jtfNum2.getText());
flags=jcbFlags.getSelectedIndex();
out.println(num1);
out.println(num2);
out.println(flags);
result=in.readLine();
labelResult.setText("结果是:"+result);
}catch(Exception exc){
JOptionPane.showMessageDialog(null,exc.getMessage());
}
}
public static void main(String[] args){
new JClientSocket();
}
}