服务端
package com.company;
import java.net.ServerSocket;
import java.net.Socket;
public class WebServer {
public static void main(String[] args) {
//创建两个端口号和连接次数的变量
int i = 1, PORT = 8080;
ServerSocket server = null; //创建ServerSocket对象
Socket client = null; //创建Socket对象
try {
server = new ServerSocket(PORT); //为对象ServerSocket赋值
//输出相应的信息
System.out.println("Web Server is listening on port "+ server.getLocalPort());
for (;;) { //通过无限循环来不断的接收监听
client = server.accept(); //获取客户机的连接请求
//创建CommunicateThread对象并启动
new CommunicateThread(client, i).start();
i++; //实现变量i的自增
}
} catch (Exception e) {
System.out.println(e);
}
}
}
CommunicateThread类:
package com.company;
import java.io.*;
import java.net.Socket;
public class CommunicateThread extends Thread {
//创建成员变量
Socket client; //创建连接Web浏览器的Socket类对象
int counter; //创建计数器对象
//构造函数
public CommunicateThread (Socket cl,int c){
//为成员变量赋值
client = cl;
counter = c;
}
public void run(){ //实现run方法
try{
//获取客户端的IP地址变量
String destIP = client.getInetAddress().toString();
int destport = client.getPort(); //获取客户机端口号
//创建PrintSream输出流对象
PrintStream outstream = new PrintStream(client.getOutputStream());
//创建输入流过滤对象
DataInputStream instream = new DataInputStream(client.getInputStream());
//读取WEB浏览器提交的请求信息
String inline = instream.readLine(); //为什么用不了readLine
System.out.println("Received:" + inline);
//判断请求信息
if (getrequest(inline)) { //当为GET请求时
String filename = getfilename(inline); //获取文件的名字
File file = new File(filename); //创建网页的file对象
if(file.exists()){ //当文件存在,则回送给浏览器
//设置返回信息
System.out.println(filename + " requested.");
outstream.println("HTTP/1.O 200 OK");
outstream.println("MIME_version:1.0");
outstream.println("Cont_Type:text/html");
int len =(int) file.length();
outstream.println("Content_Length:" + len);
outstream.println("");
sendfile(outstream, file); //调用sendfile()方法发送文件
outstream.flush();
} else { //当文件不存在时
//创建显示的内容
String notfound = "<html><head><title>Not Found</title> " +
" </head><body><h1>Error 404-file not found</h1></body> " +
" </html>";
//设置返回的信息
outstream.println("HTTP/1.0 404 no found");
outstream.println("Content_Type:text/html");
outstream.println("Content_Length:"+notfound.length()+2);
outstream.println("");
outstream.println(notfound);
outstream.flush();
}
}
long time = 1;
while (time < 11100000) {
time++;
} //延时
client.close();
}catch (IOException e) {
System.out.println("Exception:" + e);
}
}
boolean getrequest(String s) { //判断请求的类型是否为GET
if (s.length() > 0) {
if (s.substring(0, 3).equalsIgnoreCase("GET"))
return true;
}
return false;
}
String getfilename(String s) { //实现文件名的获取
String f = s.substring(s.indexOf(' ') + 1);
f = f.substring(0, f.indexOf(' '));
try {
if (f.charAt(0) == '/')
f = f.substring(1);
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Exception:" + e);
}
if (f.equals(""))
f = "index.html";
return f;
}
void sendfile(PrintStream outs, File file) { //实现发送文件到浏览器的功能
try {
//对象file的输入流对象
DataInputStream in = new DataInputStream(new FileInputStream(file));
int len = (int) file.length(); //获取文件的长度
byte buf[] = new byte[len]; //创建字节数组
in.readFully(buf); //读取内容存储到数组buf中
outs.write(buf, 0, len); //输出数组buf中的内容
//输入流和输出流
outs.flush();
in.close();
} catch (Exception e) {
System.out.println("Error retrieving file.");
System.exit(1);
}
}
}