-
设计并开发一个服务器程序,该服务器能够提供交通违章、主要城市天气、邮政编码的查询。如果用户在查询时输入#JT#川A 12345,则表示用户需要查询的是车牌号为川A 12345的汽车的所有违章信息;如果用户在查询时输入#TQ#成都,则表示用户需要查询的是成都的天气;如果用户在查询时输入#YB#成都,则表示用户需要查询的是成都的邮政编码。请为每项功能至少提供三项可以查询的内容,并将服务器设计成能够支持并发查询的多线程服务器。
-
package com.lovo.exam2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 类:服务器
* @author Abe
*/
public class Server {
//线程池
private static ExecutorService pool = Executors.newFixedThreadPool(10);
//子map 键string ,值:MyInfo
private static Map tempWeatherInfoMap = new HashMap();
private static Map temptrafficInfoMap = new HashMap();
private static Map temppostcodeInfoMap = new HashMap();
//父类map 键string ,值:子类map
@SuppressWarnings("rawtypes")
private static Map tempInfoMap = new HashMap();
/**
* 静态初始化块,两个 子类map赋值
*/
static {
tempWeatherInfoMap.put("北京", new WeatherInfo("晴", -3, 8, 20, 450));
tempWeatherInfoMap.put("上海", new WeatherInfo("阴", 6, 12, 40, 360));
tempWeatherInfoMap.put("成都", new WeatherInfo("晴", 7, 15, 60, 70));
tempWeatherInfoMap.put("广州", new WeatherInfo("晴", 18, 24, 80, 110));
tempWeatherInfoMap.put("深圳", new WeatherInfo("晴", 19, 25, 80, 25));
tempInfoMap.put("TQ", tempWeatherInfoMap);temptrafficInfoMap.put("川A11111", new TrafficInfo("川A11111", "乱停车", 0));
temptrafficInfoMap.put("川A22222", new TrafficInfo("川A22222", "闯红灯", 6));
temptrafficInfoMap.put("川A33333", new TrafficInfo("川A33333", "酒驾", 12));
temptrafficInfoMap.put("川A44444", new TrafficInfo("川A44444", "超速", 3));
tempInfoMap.put("JT", temptrafficInfoMap);
temppostcodeInfoMap.put("成都", new PostCode(61010));
temppostcodeInfoMap.put("北京", new PostCode(61011));
temppostcodeInfoMap.put("上海", new PostCode(61012));
temppostcodeInfoMap.put("广州", new PostCode(61013));
tempInfoMap.put("YB", temppostcodeInfoMap);}
/**
* main方法:启动服务器 开启端口
* @param args
* @throws IOException
*/
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(5173);
System.out.println("服务器启动成功...");
/**
* 收到新信息,就在线程池里获取一个线程运行Handler方法
*/
while (true) {
Socket socket = server.accept();
pool.execute(new Handler(socket));
}
}
/**
* 信息处理方法
* @author Abe
*/
private static class Handler implements Runnable {
private Socket socket = null;
//获得传入的套接字
public Handler(Socket socket) {
this.socket = socket;
}
//重写run()方法
@SuppressWarnings("unchecked")
@Override
public void run() {
try {
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String tempIn = br.readLine();
String[] str = tempIn.split("#");
MyInfo info = (MyInfo) (tempWeatherInfoMap = (Map) tempInfoMap.get(str[1])).get(str[2]); //输入正确 返回MyInfo子类if (info != null) {
OutputStream out = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(info);
}
//输入如异常 就返回MyInfo主类
} catch (Exception e) {
try {
MyInfo info = new MyInfo();
e.printStackTrace();
OutputStream out;
out = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(info);
} catch (Exception e1) {
e1.printStackTrace();
}
} finally {
if (socket != null) {
try {
socket.close();//关闭套接字
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}package com.lovo.exam2;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;/**
* 客户端
* @author Abe
*
*/
public class Client {@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);Socket client = null;
try {
//创建连接到服务器的套接字
client = new Socket("127.0.0.1", 5173);System.out.println("#TQ#城市 查询城市天气。");
System.out.println("#JT#车牌号 查询车辆违章信息。");
System.out.print("请输入您要查询的项目 :");
String str = sc.nextLine().trim();
//发送接收的I/O操作
OutputStream out = client.getOutputStream();
PrintStream ps = new PrintStream(out);
ps.println(str);
InputStream in = client.getInputStream();
ObjectInputStream ois = new ObjectInputStream(in);
MyInfo myInfo = (MyInfo) ois.readObject();
//返回一个MyInfo的类对象 或子类对象 自动多态实现打印
System.out.println(myInfo);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null) {
try {
client.close();//关闭套接字
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}package com.lovo.exam2;
import java.io.Serializable;
/**
* 类: 信息的父类
* @author Abe
*
*/
public class MyInfo implements Serializable{
/**
* 版本编码
*/
private static final long serialVersionUID = -2572124654089624049L;
public MyInfo(){
}
@Override
public String toString() {
return "输入有误……请重新连接~";
}
}package com.lovo.exam2;
import java.io.Serializable;
/**
* 类:交通违章信息
* @author Abe
* 父类:信息
*/
public class TrafficInfo extends MyInfo implements Serializable{
/**
* 版本编码
*/
private static final long serialVersionUID = -9115317419990754392L;
private String licensePlate; //车辆牌照
private String violationInformation; //违章信息
private int point; //本周期累计扣分
/**
* 构造器
* @param licensePlate
* @param violationInformation
* @param point
*/
public TrafficInfo(String licensePlate,
String violationInformation, int point) {
this.licensePlate = licensePlate;
this.violationInformation = violationInformation;
this.point = point;
}@Override
public String toString() {
String str = "牌照号为" + licensePlate
+ "的车辆, 违章信息为:" + violationInformation + " 本期累计扣分:"
+ point;
return str;
}
}package com.lovo.exam2;
import java.io.Serializable;
/**
* 类:天气 信息
* @author Abe
* 父类:信息
*/
public class WeatherInfo extends MyInfo implements Serializable{
/**
* 版本编码
*/
private static final long serialVersionUID = -18043424605383896L;
private String status; //什么天气
private int lowerTemperature; //最低温度
private int upperTemperature; //最高温度
private int humidity; //湿度
private int pmTwoDotFive; //PM2.5
public WeatherInfo(String status, int lowerTemperature, int upperTemperature,
int humidity, int pmTwoDotFive) {
this.status = status;
this.lowerTemperature = lowerTemperature;
this.upperTemperature = upperTemperature;
this.humidity = humidity;
this.pmTwoDotFive = pmTwoDotFive;
}@Override
public String toString() {
String str = status + " " + "温度: " + lowerTemperature + "-" +
upperTemperature + "℃ " + "湿度: " + humidity +
"% " + "空气质量: ";
if(pmTwoDotFive <= 35) {
str += "优";
}
else if(pmTwoDotFive <= 75) {
str += "良";
}
else if(pmTwoDotFive <= 115){
str += "轻度污染";
}
else if(pmTwoDotFive <= 150) {
str += "中度污染";
}
else {
str += "重度污染";
}
return str;
}
}package com.lovo.exam2;
import java.io.Serializable;
/**
* 类:邮编 信息
*
* @author Abe 父类:信息
*/
public class PostCode extends MyInfo implements Serializable {private static final long serialVersionUID = 1L;
private int postcode;public PostCode(int postcode) {
super();
this.postcode = postcode;
}@Override
public String toString() {
return "邮编:" + postcode;
}}