JAVA programming (19) ----- setting up a Server Client that queries information supports concurrent Multithreading
Design and develop a server program that provides traffic violations and weather conditions in major cities. If you enter# JT # Sichuan A 12345Indicates that the user needs to query the license plate numberSichuan A 12345Of the carAllViolation information. If you enter# TQ # ChengduIt indicates that the weather in Chengdu needs to be queried. The server is designed as a multi-thread server that supports concurrent queries.
Automatically implement multi-state feedback based on input information
1 server code
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;/*** class: Server * @ author Abe */public class Server {// thread pool private static ExecutorService pool = Executors. newFixedThreadPool (10); // string of the sub-map key; Value: MyInfoprivate static Map
TempWeatherInfoMap = new HashMap
(); Private static Map
TemptrafficInfoMap = new HashMap
(); // String of the parent map key; Value: subclass map @ SuppressWarnings ("rawtypes") private static Map
TempInfoMap = new HashMap
();/*** Static initialization block, two subclass map values */static {tempWeatherInfoMap. put ("Beijing", new WeatherInfo ("clear",-3, 8, 20,450); tempWeatherInfoMap. put ("Shanghai", new WeatherInfo ("Overcast", 6, 12, 40,360); tempWeatherInfoMap. put ("Chengdu", new WeatherInfo ("clear", 7, 15, 60, 70); tempWeatherInfoMap. put ("Guangzhou", new WeatherInfo ("clear", 18, 24, 80,110); tempWeatherInfoMap. put ("Shenzhen", new WeatherInfo ("Sunny", 19, 25, 80, 25); tempInfoMap. put ("TQ", tempWea TherInfoMap); temptrafficInfoMap. put ("Chuan A11111", new TrafficInfo ("Chuan A11111", "parking", 0); temptrafficInfoMap. put ("Chuan A22222", new TrafficInfo ("Chuan A22222", "red light", 6); temptrafficInfoMap. put ("Chuan A33333", new TrafficInfo ("Chuan A33333", "Drunk driving", 12); temptrafficInfoMap. put ("Chuan A44444", new TrafficInfo ("Chuan A44444", "speeding", 3); tempInfoMap. put ("JT", temptrafficInfoMap);}/*** main method: Start the server to open the port * @ param args * @ throws IOExcepti On */@ SuppressWarnings ("resource") public static void main (String [] args) throws IOException {ServerSocket server = new ServerSocket (5173); System. out. println ("the server is successfully started... ");/*** after receiving the new message, obtain a thread running Handler method in the thread pool */while (true) {Socket socket = server.accept(registry.pool.exe cute (new Handler (socket ));}} /*** Information Processing Method ** @ author Abe */private static class Handler implements Runnable {private Socket socket = null ;/ /Obtain the passed Socket public Handler (socket Socket) {this. socket = socket;} // rewrite the run () method @ SuppressWarnings ("unchecked") @ Overridepublic void run () {try {InputStream in = socket. getInputStream (); BufferedReader br = new BufferedReader (new InputStreamReader (in); String tempIn = br. readLine (); String [] str = tempIn. split ("#"); MyInfo info = (tempWeatherInfoMap = tempInfoMap. get (str [1]). get (str [2]); // if (Info! = Null) {OutputStream out = socket. getOutputStream (); ObjectOutputStream oos = new ObjectOutputStream (out); oos. writeObject (info);} // if an Exception occurs, the MyInfo main class is returned.} 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 (); // close socket} catch (IOException e) {e. printStackTrace ();}}}}}}
2. Client
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. principal;/*** Client * @ author Abe **/public class Client {@ SuppressWarnings ("resource") public static void main (String [] args) {pipeline SC = new pipeline (System. in); Socket client = null; try {// create a connection to the server Socket client = new Socket ("192.168.1.101", 5173); System. out. println ("# TQ # city query city weather. "); System. out. println (" # JT # license plate number query vehicle violation information. "); System. out. println ("Enter the project you want to query:"); String str = SC. nextLine (). trim (); // send and receive the I/O operation 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 (); // returns a Class Object of MyInfo or a subclass object with automatic polymorphism to print System. out. println (myInfo);} catch (Exception e) {e. pri NtStackTrace ();} finally {if (client! = Null) {try {client. close (); // close socket} catch (IOException e) {e. printStackTrace ();}}}}}
3. The main class MyInfo of the returned information object
Package com. lovo. exam2; import java. io. serializable;/*** class: information parent class * @ author Abe **/public class MyInfo implements Serializable {/*** version Code */private static final long serialVersionUID =-2572124654089624049L; public MyInfo () {}@ Overridepublic String toString () {return "incorrect input ...... Please try again ~ ";}}
4. One of the return object subclasses
Package com. lovo. exam2; import java. io. serializable;/*** class: traffic violation information * @ author Abe * parent class: information */public class TrafficInfo extends MyInfo implements Serializable {/*** version Code */private static final long serialVersionUID =-strong; private String licensePlate; // vehicle license private String violationInformation; // private int point of violation information; // cumulative deduction for this cycle/*** constructor * @ param licensePlate * @ param violationInformation * @ param point */public TrafficInfo (String licensePlate, string violationInformation, int point) {this. licensePlate = licensePlate; this. violationInformation = violationInformation; this. point = point ;}@ Overridepublic String toString () {String str = "vehicles with the license plate number" + licensePlate + ", \ r \ n violation information: "+ violationInformation +" \ r \ n cumulative points for the current period: "+ point; return str ;}}
5. Return Object Information 2
Package com. lovo. exam2; import java. io. serializable;/*** class: Weather Information * @ author Abe * parent class: information */public class WeatherInfo extends MyInfo implements Serializable {/*** version Code */private static final long serialVersionUID =-18043424605383896L; private String status; // What weather private int lowerTemperature; // minimum temperature private int upperTemperature; // maximum temperature private int humidity; // humidity private int pmTwoDotFive; // PM2.5public 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;} @ Overridepublic String toString () {String str = status + "\ r \ n" + "temperature: "+ lowerTemperature +"-"+ upperTemperature +" ℃ \ r \ n "+" humidity: "+ humidity +" % \ r \ n "+" Air Quality :"; if (pmTwoDotFive <= 35) {str + = "";} else if (pmTwoDotFive <= 75) {str + = "";} else if (pmTwoDotFive <= 115) {str + = "mild pollution";} else if (pmTwoDotFive <= 150) {str + = "moderate pollution ";} else {str + = "heavy pollution";} return str ;}}