用JAVA编写Web服务器
时间:2010-02-01
//WebServer.java 用JAVA编写Web服务器
import java.io.*;
import java.net.*;
public class WebServer
{
public static void main(String args[])
{
int i=1, PORT=8080;
ServerSocket server=null;
Socket client=null;
try
{
server=new ServerSocket(PORT);
System.out.println("Web Server is listening on port "+server.getLocalPort());
for (;;)
{
client=server.accept(); //接受客户机的连接请求
new ConnectionThread(client,i).start();
i++;
}
}
catch (Exception e) {System.out.println(e);}
}
}
/* ConnnectionThread类完成与一个Web浏览器的通信 */
class ConnectionThread extends Thread
{
Socket client; //连接Web浏览器的socket字
int counter; //计数器
public ConnectionThread(Socket cl,int c)
{
client=cl;
counter=c;
}
public void run() //线程体
{
try
{
String destIP=client.getInetAddress().toString(); //客户机IP地址
int destport=client.getPort(); //客户机端口号
System.out.println("Connection "+counter+":connected to "+destIP+" on port "+destport+".");
PrintStream outstream=new PrintStream(client.getOutputStream());
DataInputStream instream=new DataInputStream(client.getInputStream());
String inline=instream.readLine(); //读取Web浏览器提交的请求信息
System.out.println("Received:"+inline);
if (getrequest(inline))
{ //如果是GET请求
String filename=getfilename(inline);
File file=new File(filename);
if (file.exists())
{ //若文件存在,则将文件送给Web浏览器
System.out.println(filename+" requested.");
outstream.println("HTTP/1.0 200 OK");
outstream.println("MIME_version:1.0");
outstream.println("Content_Type:text/html");
int len=(int)file.length();
outstream.println("Content_Length:"+len);
outstream.println("");
sendfile(outstream,file); //发送文件
outstream.flush();
}
else
{ //文件不存在时
String msg1="<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:"+msg1.length()+2);
outstream.println("");
outstream.println(msg1);
outstream.flush();
}
}
//instream.close();
//outstream.close();
long m1=1; //延时
while (m1<11100000) {m1++;}
client.close();
}
catch (IOException e)
|