import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SocketServer {
final private int serverPort = 3128;
private ExecutorService executorService;// 线程池
private final int POOL_SIZE = 200;// 单个CPU线程池大小
private ServerSocket serverSocket;
private static String encoding;
public static class SocketTask implements Runnable {
private Socket mSocket;
public SocketTask(Socket mSocket) {
this.mSocket = mSocket;
}
public void run() {
try {
mSocket.getInputStream();
InputStream is = mSocket.getInputStream();
DataOutputStream dos = null;
PrintWriter out = null;
dos = new DataOutputStream(mSocket.getOutputStream());
out = new PrintWriter(new OutputStreamWriter(dos)); // 不带自动刷新的Writer
InputStreamReader in = new InputStreamReader(is);
BufferedReader inread = new BufferedReader(in);
String line = inread.readLine();
line = line.replaceAll("GET /", "");
line = line.replaceAll(" HTTP/1.1", "");
System.out.println("line:" + line);
if (line.equals("404")) {
out.println("HTTP/1.1 404 Not found");
out.println();
out.flush();
} else if (line.equals("ok")) {
System.out.println("send ok");
out.println("HTTP/1.1 200 OK");
out.println("Content-Type:text/html;charset=" + encoding);
out.println();
out.println("<h1> Hello Http Server</h1>");
out.flush();
} else {
out.println("HTTP/1.1 200 OK");
out.println("Content-Type:text/html;charset=" + encoding);
out.println();
out.println("<h1>题目如下,重点是程序在3分钟之内计算完毕。</h1>"
+ "<br><br>一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数。"
+ "<br>例如:当N=3时,153就满足条件,因为 1^3 + 5^3 + 3^3 = 153,"
+ "<br>这样的数字也被称为水仙花数(其中,“^”表示乘方,5^3表示5的3次方,也就是立方)。"
+ "<br>当N=4时,1634满足条件,因为 1^4 + 6^4 + 3^4 + 4^4 = 1634。"
+ "<br>当N=5时,92727满足条件。实际上,对N的每个取值,可能有多个数字满足条件。<br>程序的任务是:求N=21时,所有满足条件的花朵数。<br>注意:这个整数有21位,它的各个位数字的21次方之和正好等于这个数本身。<br>如果满足条件的数字不只有一个,请从小到大输出所有符合条件的数字,每个数字占一行。<br>因为这个数字很大,请注意解法时间上的可行性。<br>要求程序在3分钟内运行完毕。(据说常规思路,普通电脑要运算几年。。。)"
+ "你看看有没有兴趣,周末可以回去讨论下。");
out.println();
// out.println(ReadFile());
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Thread exit!");
}
public static String ReadFile() {
File file = new File("/media/data/qq.txt");
try {
encoding = getcoding(file);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTXT = null;
String txt = "";
while ((lineTXT = bufferedReader.readLine()) != null) {
txt += lineTXT + "<br>";
}
read.close();
return txt;
} else {
System.out.println("找不到指定的文件!");
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
return "empty";
}
private static String getcoding(File file) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] head = new byte[3];
try {
inputStream.read(head);
} catch (IOException e) {
e.printStackTrace();
}
String code = "";
code = "gb2312";
if (head[0] == -1 && head[1] == -2)
code = "UTF-16";
if (head[0] == -2 && head[1] == -1)
code = "Unicode";
if (head[0] == -17 && head[1] == -69 && head[2] == -65)
code = "UTF-8";
System.out.println("get code:" + code);
return code;
}
}
public static void main(String[] args) {
new SocketServer().ServerStart();// 启动Socket线程
}
public void ServerStart() {
System.out.println("SocketServer>>>");
System.out.println("on port:" + serverPort);
SocketTask.ReadFile();
while (true) {
try {
Socket socket = null;
socket = serverSocket.accept();
System.out.println("Server got a client socket!");
executorService.execute(new SocketTask(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public SocketServer() {
try {
serverSocket = new ServerSocket(serverPort);
executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors() * POOL_SIZE);
} catch (Exception e) {
System.out.println("Servers is died!");
}
}
}
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SocketServer {
final private int serverPort = 3128;
private ExecutorService executorService;// 线程池
private final int POOL_SIZE = 200;// 单个CPU线程池大小
private ServerSocket serverSocket;
private static String encoding;
public static class SocketTask implements Runnable {
private Socket mSocket;
public SocketTask(Socket mSocket) {
this.mSocket = mSocket;
}
public void run() {
try {
mSocket.getInputStream();
InputStream is = mSocket.getInputStream();
DataOutputStream dos = null;
PrintWriter out = null;
dos = new DataOutputStream(mSocket.getOutputStream());
out = new PrintWriter(new OutputStreamWriter(dos)); // 不带自动刷新的Writer
InputStreamReader in = new InputStreamReader(is);
BufferedReader inread = new BufferedReader(in);
String line = inread.readLine();
line = line.replaceAll("GET /", "");
line = line.replaceAll(" HTTP/1.1", "");
System.out.println("line:" + line);
if (line.equals("404")) {
out.println("HTTP/1.1 404 Not found");
out.println();
out.flush();
} else if (line.equals("ok")) {
System.out.println("send ok");
out.println("HTTP/1.1 200 OK");
out.println("Content-Type:text/html;charset=" + encoding);
out.println();
out.println("<h1> Hello Http Server</h1>");
out.flush();
} else {
out.println("HTTP/1.1 200 OK");
out.println("Content-Type:text/html;charset=" + encoding);
out.println();
out.println("<h1>题目如下,重点是程序在3分钟之内计算完毕。</h1>"
+ "<br><br>一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数。"
+ "<br>例如:当N=3时,153就满足条件,因为 1^3 + 5^3 + 3^3 = 153,"
+ "<br>这样的数字也被称为水仙花数(其中,“^”表示乘方,5^3表示5的3次方,也就是立方)。"
+ "<br>当N=4时,1634满足条件,因为 1^4 + 6^4 + 3^4 + 4^4 = 1634。"
+ "<br>当N=5时,92727满足条件。实际上,对N的每个取值,可能有多个数字满足条件。<br>程序的任务是:求N=21时,所有满足条件的花朵数。<br>注意:这个整数有21位,它的各个位数字的21次方之和正好等于这个数本身。<br>如果满足条件的数字不只有一个,请从小到大输出所有符合条件的数字,每个数字占一行。<br>因为这个数字很大,请注意解法时间上的可行性。<br>要求程序在3分钟内运行完毕。(据说常规思路,普通电脑要运算几年。。。)"
+ "你看看有没有兴趣,周末可以回去讨论下。");
out.println();
// out.println(ReadFile());
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Thread exit!");
}
public static String ReadFile() {
File file = new File("/media/data/qq.txt");
try {
encoding = getcoding(file);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTXT = null;
String txt = "";
while ((lineTXT = bufferedReader.readLine()) != null) {
txt += lineTXT + "<br>";
}
read.close();
return txt;
} else {
System.out.println("找不到指定的文件!");
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
return "empty";
}
private static String getcoding(File file) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] head = new byte[3];
try {
inputStream.read(head);
} catch (IOException e) {
e.printStackTrace();
}
String code = "";
code = "gb2312";
if (head[0] == -1 && head[1] == -2)
code = "UTF-16";
if (head[0] == -2 && head[1] == -1)
code = "Unicode";
if (head[0] == -17 && head[1] == -69 && head[2] == -65)
code = "UTF-8";
System.out.println("get code:" + code);
return code;
}
}
public static void main(String[] args) {
new SocketServer().ServerStart();// 启动Socket线程
}
public void ServerStart() {
System.out.println("SocketServer>>>");
System.out.println("on port:" + serverPort);
SocketTask.ReadFile();
while (true) {
try {
Socket socket = null;
socket = serverSocket.accept();
System.out.println("Server got a client socket!");
executorService.execute(new SocketTask(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public SocketServer() {
try {
serverSocket = new ServerSocket(serverPort);
executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors() * POOL_SIZE);
} catch (Exception e) {
System.out.println("Servers is died!");
}
}
}