Server Client for querying information is established to support concurrent multithreading

Source: Internet
Author: User
Tags key string

Design and develop a server program that can provide traffic violations, major city weather, zip code query. If the user enters #jt# chuan a 12345 in the query, it means that the user needs to inquire about all the illegal information of the car with the license plate number of Chuan a 12345; if the user enters #tq# Chengdu when querying, it indicates that the user needs to inquire about the weather in Chengdu; If the user enters #yb# Chengdu when querying, Indicates that the user needs to query the postcode of Chengdu. Provide at least three items that can be queried for each feature and design the server as a multithreaded server capable of supporting concurrent queries.

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);
Child map key String, value: MyInfo
private static Map Tempweatherinfomap = new HashMap ();
private static Map Temptrafficinfomap = new HashMap ();
private static Map Temppostcodeinfomap = new HashMap ();
Parent class Map Key string, Value: sub-class Map
@SuppressWarnings ("Rawtypes")
private static Map Tempinfomap = new HashMap ();

/**
* Static initialization block, two sub-class map assignment
*/
static {
Tempweatherinfomap.put ("Beijing", New Weatherinfo ("Clear",-3, 8, 20, 450));
Tempweatherinfomap.put ("Shanghai", New Weatherinfo ("Yin", 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 ("clear", 19, 25, 80, 25));
Tempinfomap.put ("TQ", Tempweatherinfomap);

Temptrafficinfomap.put ("Chuan A11111", New Trafficinfo ("Chuan A11111", "Disorderly Parking", 0));
Temptrafficinfomap.put ("Chuan A22222", New Trafficinfo ("Chuan A22222", "Red Light", 6));
Temptrafficinfomap.put ("Chuan A33333", New Trafficinfo ("Chuan A33333", "Wine Drive", 12));
Temptrafficinfomap.put ("Chuan A44444", New Trafficinfo ("Chuan A44444", "Speeding", 3));
Tempinfomap.put ("JT", Temptrafficinfomap);

Temppostcodeinfomap.put ("Chengdu", New Postcode (61010));
Temppostcodeinfomap.put ("Beijing", New Postcode (61011));
Temppostcodeinfomap.put ("Shanghai", New Postcode (61012));
Temppostcodeinfomap.put ("Guangzhou", New Postcode (61013));

Tempinfomap.put ("YB", Temppostcodeinfomap);

}

/**
* Main method: Start the server open port
* @param args
* @throws IOException
*/
@SuppressWarnings ("resource")
public static void Main (string[] args) throws IOException {
ServerSocket Server = new ServerSocket (5173);
SYSTEM.OUT.PRINTLN ("Server started successfully ...");
/**
* Receive new information on online constructor get a thread to run the handler method
*/
while (true) {
Socket socket = server.accept ();
Pool.execute (new Handler (socket));
}
}

/**
* Information Processing methods
* @author Abe
*/
private static class Handler implements Runnable {
Private socket socket = NULL;
Get incoming sockets
Public Handler (socket socket) {
This.socket = socket;
}

Override the Run () method
@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]);//input correct return MyInfo subclass

if (info! = null) {
OutputStream out = Socket.getoutputstream ();
ObjectOutputStream oos = new ObjectOutputStream (out);
Oos.writeobject (info);
}
Input like exception returns MyInfo main class
} 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 ();
}
}
}
}
}
}

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;

/**
* Client
* @author Abe
*
*/
public class Client {

@SuppressWarnings ("resource")
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);

Socket client = null;
try {
Create a socket that connects to the server
Client = new Socket ("127.0.0.1", 5173);

System.out.println ("#TQ # City Check city weather. ");
System.out.println ("#JT # license plate number check vehicle violation information. ");
System.out.print ("Please enter the item you want to inquire about:");
String str = sc.nextline (). Trim ();
Send received I/O operations
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 an MyInfo class object or subclass object automatic polymorphic implementation printing
System.out.println (MyInfo);
} catch (Exception e) {
E.printstacktrace ();
} finally {
if (client! = NULL) {
try {
Client.close ();//Close socket
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
}

Package com.lovo.exam2;

Import java.io.Serializable;

/**
* Class: Information of the parent class
* @author Abe
*
*/
public class MyInfo implements serializable{
/**
* Version Code
*/
Private static final long serialversionuid = -2572124654089624049l;

Public MyInfo () {
}

@Override
Public String toString () {
Return "wrong input ... Please reconnect ~ ";
}
}

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 = -9115317419990754392l;

Private String licenseplate;//Vehicle licence
Private String violationinformation;//violation information
private int point;//This period cumulative deduction points

/**
* Constructor
* @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 = "license plate number" + licenseplate
+ "The vehicle, \ r \ n violation information:" + violationinformation + "\ r \ n This period cumulative deduction:"
+ point;
return str;
}
}

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.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 + "\ n" + "Temperature:" + Lowertemperature + "-" +
Uppertemperature + "℃\n" + "Humidity:" + humidity +
"%\n" + "air quality:";
if (pmtwodotfive <= 35) {
STR + + "excellent";
}
else if (pmtwodotfive <= 75) {
str + = "Liang";
}
else if (pmtwodotfive <= 115) {
str + = "mildly polluted";
}
else if (pmtwodotfive <= 150) {
str + = "moderate pollution";
}
else {
str + = "heavy pollution";
}
return str;
}
}

Package com.lovo.exam2;

Import java.io.Serializable;

/**
* Class: Postcode information
*
* @author Abe Parent class: Information
*/
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 "ZIP Code:" + postcode;
}

}

Server Client for querying information is established to support concurrent multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.