关闭

利用socket网络编程,实现的简易CS计算器

标签: java计算器socket网络编程
611人阅读 评论(0) 收藏 举报
分类:

包含socket服务端、客户端,实现简易计算器:

1、服务端:

package com.boco.calculatorBySocket.server;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import com.boco.calculatorBySocket.Calculation;
import com.boco.calculatorBySocket.CalculationPojo;

/**
 * 计算器服务端
 * @author Administrator
 *
 */
public class Server {
	
	public static ServerSocket serverSocket;
	
	public Server(){
		try {
			this.serverSocket = new ServerSocket(5678);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		try {
			Server server = new Server();
			while(true){
				Socket client=serverSocket.accept();
				System.out.println(serverSocket.getInetAddress() );
				InputStream is=client.getInputStream();
				ObjectInputStream ois=new ObjectInputStream(is);
				Object o = ois.readObject();
				CalculationPojo pojo = (CalculationPojo) o;
				if(pojo.getWay().equals("end"))
					break;
				int result = Calculation(pojo.getWay(),pojo.getValue1(),pojo.getValue2());
				System.out.println("result  =   "+result);
				OutputStream os=client.getOutputStream();
		        DataOutputStream dos=new DataOutputStream(os);
		        dos.writeInt(result);
		        dos.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static int Calculation(String way , int a , int b ){
		Calculation cal = new Calculation();
		if("+".equals(way)){
			return cal.plus(a, b);
		}else if("-".equals(way)){
			return cal.minus(a, b);
		}else if("*".equals(way)){
			return cal.mul(a, b);
		}else if("/".equals(way)){
			return cal.Divide(a, b);
		}else{
			return 63353;
		}
	}


}


2、客户端:

package com.boco.calculatorBySocket.client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.boco.calculatorBySocket.CalculationPojo;
/**
 * 计算器客户端
 * @author Administrator
 *
 */
public class Client extends JFrame implements  ActionListener{
	
	
	
	JPanel JPanel_center=new JPanel();
	JPanel JPanel_south=new JPanel();
	
	//JLabel JLabel_add=new JLabel("+");
	String s[]={"+","-","*","/","%"};
	JComboBox ways=new JComboBox(s);
	JLabel JLabel_equal=new JLabel("=");
	JTextField JTextField_num1=new JTextField(10);
	JTextField JTextField_num2=new JTextField(10);
	JTextField JTextField_result=new JTextField(10);
	JButton JButton_cal=new JButton("计算");


	
	public Client(){
		this.getContentPane().add(JPanel_center,BorderLayout.CENTER);
		this.getContentPane().add(JPanel_south,BorderLayout.SOUTH);
		JPanel_center.add(JTextField_num1);
        JPanel_center.add(ways);
        JPanel_center.add(JTextField_num2);
        JPanel_center.add(JLabel_equal);
        JPanel_center.add(JTextField_result);
        JPanel_south.add(JButton_cal);
        JButton_cal.addActionListener(this);
        ways.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		try {
			
			String value1=JTextField_num1.getText();
			String value2=JTextField_num2.getText();
			if(value2.equals("") && value1.equals("")){
				return;
			}
			Socket server =  new Socket(InetAddress.getLocalHost(),5678);
			String way=(String)ways.getSelectedItem();
			OutputStream os = server.getOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(os);
			int item1 = Integer.parseInt(value1);   
			int item2 = Integer.parseInt(value2);
			CalculationPojo pojo = new CalculationPojo(way,item1,item2);
			
			out.writeObject(pojo);
			out.flush();
			InputStream is =server.getInputStream();
			DataInputStream dis = new DataInputStream(is);
			int result= dis.readInt();
			JTextField_result.setText(String.valueOf(result));
			
			os.close();
			out.close();
			is.close();
			dis.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	public static void main (String[] args) {
		Client cc=new Client();
	   	cc.pack();
	   	cc.setVisible(true);
	}
}


3、计算器:

package com.boco.calculatorBySocket;

public class Calculation {
	
	
	
	/**
	 * 加
	 * @param a
	 * @param b
	 * @return
	 */
	public int plus (int a,int b){
		return a+b;
	}
	
	/**
	 * 减
	 * @param a
	 * @param b
	 * @return
	 */
	public int minus (int a,int b){
		return a-b;
	}
	
	/**
	 * 乘
	 * @param a
	 * @param b
	 * @return
	 */
	public int mul (int a,int b){
		return a*b;
	}

	/**
	 * 除
	 * @param a
	 * @param b
	 * @return
	 */
	public int Divide (int a,int b){
		return a/b;
	}
}


4:实体:

package com.boco.calculatorBySocket;

import java.io.Serializable;

public class CalculationPojo implements Serializable{
	
	public String way;
	
	public int value1;
	
	public int value2;
	
	public CalculationPojo(String way, int value1 , int value2){
		this.way = way;
		this.value1 = value1;
		this.value2 = value2;
	}

	public String getWay() {
		return way;
	}

	public void setWay(String way) {
		this.way = way;
	}

	public int getValue1() {
		return value1;
	}

	public void setValue1(int value1) {
		this.value1 = value1;
	}

	public int getValue2() {
		return value2;
	}

	public void setValue2(int value2) {
		this.value2 = value2;
	}

}


 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:12222次
    • 积分:257
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:4篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论