写下这编文章只是为了让自己不忘记以前学过的知识,下面是基于Socket来完成浏览器的访问,就类似于tomcat(不过tomcat比我功能更全更强大),那下面来看代码:
很简单 先由 HttpServer 来接受浏览器的问访问之后就交给HttpThread类来响应。
HttpThread获取到io流并作出响应
Receiver这个类获主要取浏览器访问的地址
Answer这个就是响应类了
代码很简单主要就是了解下,刚学习服务器的同学现在应该不会对服务器有什么距离了吧!
很简单 先由 HttpServer 来接受浏览器的问访问之后就交给HttpThread类来响应。
package com.zx;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author zhouxiang
* @version Feb 21, 2013 10:22:55 AM
*/
public class HttpServer {
// 默认ROOT文件夹
public static String ROOT = "./wwwroot";
// 默认文件的文件名
public static String defaultPage = "index.html";
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8000);
while(true){
// 阻塞,等待浏览器的连接
Socket socket = server.accept();
System.out.println("Accept Connection.....\n");
// 启动服务线程
new HttpThread(socket).start();
}
}
}
HttpThread获取到io流并作出响应
package com.zx;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* @author zhouxiang
* @version Feb 21, 2013 10:33:52 AM
*/
public class HttpThread extends Thread {
private Socket socket;
public HttpThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = this.socket.getInputStream();
outputStream = this.socket.getOutputStream();
Receiver receiver = new Receiver(inputStream);
// 取得浏览器发过来的URL请求
String sURL = receiver.parese();
if (sURL.equals("/")) {
sURL = HttpServer.defaultPage;
}
Answer ans = new Answer(outputStream);
ans.send(sURL);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
if(socket != null ){
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Receiver这个类获主要取浏览器访问的地址
package com.zx;
import java.io.IOException;
import java.io.InputStream;
/**
* @author zhouxiang
* @version Feb 21, 2013 10:40:39 AM
*/
public class Receiver {
InputStream inputStream = null;
public Receiver(InputStream inputStream) {
this.inputStream = inputStream;
}
/**
* 这个方法的目的是将URL请求的文件返回
* @return
*/
public String parese(){
StringBuffer receiveStr = new StringBuffer(2048);
int i = 0;
byte[] bytes = new byte[2048];
try {
//从socket读出数据
i = inputStream.read(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
// 读出空数据
System.out.println("读取流异常......");
i = -1;
}
System.out.println("读取的字节长度:"+""+i);
for(int j = 0 ; j < i ; j++){
// 读出数据以字符方式保存到receiveStr
receiveStr.append((char)bytes[j]);
}
return getUri(receiveStr.toString());
}
/**
* 将收到的HTTP协议数据包分解,取出文件名
* @param receiveStr
* @return
*/
private String getUri(String receiveStr) {
int index1,index2;
index1 = receiveStr.indexOf(' ');
if(index1 != -1){
index2 = receiveStr.indexOf(' ',index1 + 1);
if(index2 > index1){
return receiveStr.substring(index1 + 1, index2);
}
}
return null;
}
}
Answer这个就是响应类了
package com.zx;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author zhouxiang
* @version Feb 21, 2013 11:54:10 AM
*/
public class Answer {
OutputStream out = null;
public Answer(OutputStream outputStream) {
this.out = outputStream;
}
public void send(String pageFile) {
// TODO Auto-generated method stub
byte[] bytes = new byte[2048];
FileInputStream fis = null;
try {
File file = new File(HttpServer.ROOT, pageFile);
if (file.exists()) {
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, 2048);
String sBody = new String(bytes);
String sendMessage = "HTTP/1.1 200 OK\r\n"+"Content-Type: text/html\r\n"+"Content-Length: "+ch+"\r\n"+
"\r\n"+sBody;
out.write(sendMessage.getBytes());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
代码很简单主要就是了解下,刚学习服务器的同学现在应该不会对服务器有什么距离了吧!