程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 用JAVA編寫Web服務器

用JAVA編寫Web服務器

編輯:關於JAVA

//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);
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved