JAVA
网络
HttpServer类
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class HttpServer {
// shutdown command
private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
//the shutdown command received
private boolean shutdown = false;
public static void main(String[] args) {
HttpServer server = new HttpServer();
server.await();
}
public void await() {
ServerSocket serverSocket = null;
int port = 8080;
try {
serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// Loop waiting for a request
while (!shutdown) {
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try {
socket = serverSocket.accept();
input = socket.getInputStream();
output = socket.getOutputStream();
// create Request object and parse
Request request = new Request(input);
request.parse();
//create Response object
Response response = new Response(output);
response.setRequest(request);
response.sendStaticResource();
//Close the socket
socket.close();
//check if the previous URI is a shutdown command
shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
}
代码中包含了一个webroot目录——>(用以测试程序的静态资源)
http://machineName:port/staticResource
请求静态资源,在浏览器地址或者URL框中输入如上地址
machineName 是指运行这个应用程序的计算机名称或者IP地址
port为8080
staticResource是被请求文件的文件名,它一定是在WEB_ROOT 中
http://localhost:8080/index.html
使用同一台计算机测试应用程序并且让HttpServer对象发送index.html文件
private static final String SHUTDOWN COMMAND = "/SHUTDOWN";
在host:port后输入预定义的字符串,Web浏览器发送shutdown命令。该命令由HttpServer类中的SHUTDOWN静态final变量定义
http://localhost:8080/SHUTDOWN
HttpServer类中的await方法
方法名之所以不用wait,是因为wait是java.lang.Object中的一个方法名称
await 方法首先创建ServerSocket实例,然后进入while循环
serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
....
//Loop waiting for a request
while (!shutdown) {
...
}
socket = serverSocket.accept();
while 循环中的代码在ServerSocket中的accept方法处停止,直到8080端口收到HTTP请求
收到请求后,await方法就从accept方法返回的Socket获取java.io.InputStream java.io.OutputStream
input = socket.getInputStream();
output = socket.getOutputStream();
//create Request object and parse
Request request = new Request(input); await方法创建Request并调用它的parse方法解析HTTP请求的原始数据
request.parse();
然后创建Response,将Request赋给它,调用它的sendStaticResource方法
//create Response object
Response response = new Response(output);
response.setRequest(request);
response.sendStaticResource();
最后await方法关闭Socket并调用Request的geiUri方法来检验HTTP请求的URI是否为一条shutdown命令,如果是,shutdown变量被设置为true,程序退出while循环
//Close the socket
socket.close();
//check if the previous URI is a shutdown command
shutdown = request.getUri().equals(SHUTDOWN_COMMAND)
LIUNX
启动盘boot/root盘的制作
根文件系统
——>文件系统结构
——> /dev、/proc、/bin、/etc、/lib、/usr、/tmp
——>基本的应用程序:sh ls cp nv
——>最低限度的配置文件:rc inittab fstab
——>设备
——>基本程序运行所需的库函数
mke2fs -m 0 -i 2000 /dev/ram0
创建根文件系统
mount -t ext2 /dev/ram0 /mnt/floppy
把虚拟盘挂在节点/mnt 上
创建目录
cd /mnt/floppy
mkdir dev proc etc sbin bin lib mnt usr