服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

webserver.java 用java编写web服务器

  //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)
    {
    system.out.println("exception:"+e);
    }
}
/* 获取请求类型是否为“get” */
boolean getrequest(string s)

  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;
}
/*把指定文件发送给web浏览器 */
void sendfile(printstream outs,file file)
{
try
  {
  datainputstream in=new datainputstream(new fileinputstream(file));
  int len=(int)file.length();
  byte buf[]=new byte[len];
  in.readfully(buf);
  outs.write(buf,0,len);
  /*
  dataoutputstream ou=new dataoutputstream(system.out);
  ou.write(buf,0,len);
  */
  outs.flush();
  in.close();
  }
catch (exception e)
  {
  system.out.println("error retrieving file.");
  system.exit(1);
  }
}

扫描关注微信公众号