public class CellPhoneLocator {
/** The listening port of server. */
private int serverPort = 8080;
private int backlog = 50;
private InetAddress bindAddress = null;
private Map<String,String> questions = new HashMap<String,String>(5);
private boolean toStopServer = false;
//Open 843 to handle the policy request.
private PolicyService policyService = null;
/** The server socket */
private ServerSocket serverSocket = null;
public CellPhoneLocator() {
questions.put("130", "130’s from chinaunicom");
questions.put("131", "131’s from chinaunicom");
questions.put("133", "133’s from Chinatelecom");
questions.put("135", "135’s from China Mobile");
questions.put("138", "138’s from China Mobile");
}
private String locateNumber(String phoneNum) {
String location = this.questions.get(phoneNum);
return location==null ? (phoneNum + "’s Unknown") : location;
}
private class FlexStringReader extends InputStreamReader {
private StringBuffer buf = new StringBuffer();
public FlexStringReader(InputStream in) {
super(in);
}
public FlexStringReader(InputStream in, String charsetName) throws UnsupportedEncodingException {
super(in, charsetName);
}
//Make sure it works.
public String readLine() throws IOException {
int oneChar = 0;
buf.setLength(0);
while ((oneChar = this.read()) != -1) {
if (oneChar == ’\0’)
break;
buf.append((char) oneChar);
}
return buf.toString();
}
}
private class RequestHanlder implements Runnable {
private Socket sock = null;
public RequestHanlder(Socket pSock) {
this.sock = pSock;
}
private String command = "";
private String parameter = "";
public void run() {
FlexStringReader reader = null;
PrintWriter pw = null;
String line = null;
try {
System.out.println("Client:" + this.sock);
InputStream in = this.sock.getInputStream();
reader = new FlexStringReader(in, "UTF-8");
pw = new PrintWriter(this.sock.getOutputStream());
String policyRequest = reader.readLine();//new String(headers);
//FLash client will open two ports, one to retrieve the policy.
//Another to normal communicate to server.
if (policyRequest.startsWith("<policy-file-request/>")) {
pw.write("<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\0");
pw.flush();
System.out.println("Policy’s sent by current listening port:" + serverPort);
Thread.sleep(10000);
throw new IOException("A dirty return, never follow it dude.");
}
else {
pw.write("Hi " + this.sock + ",at your service, send me the cell phone please.\0");
}
pw.flush();
while ((line = reader.readLine()).length() > 0) {
this.parseRequest(line);
if ("where".equalsIgnoreCase(command)) {
pw.write(locateNumber(this.parameter)+ ’\0’);
pw.flush();
}
else if ("quit".equalsIgnoreCase(command) || "exit".equalsIgnoreCase(command)) {
break;
}
else {
pw.write("Oops, unknown command.\0");
pw.flush();
}
}
}
catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
//FIXME, try to close the I/O stream
try {
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void parseRequest(String line) {
command = "";
parameter = "";
StringTokenizer tokens = new StringTokenizer(line, " \t\n\r\f\0", false);
int count = tokens.countTokens();
if (count > 0) {
this.command = tokens.nextToken();
if (count > 1)
this.parameter = tokens.nextToken();
}
System.out.println("Command=" + this.command + "\tParameter=" + this.parameter);
}
}
/**
* Start the server first.
* @throws IOException
*/
责任编辑:小草