⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpserver.java

📁 Java HTTP Server and Client - easy to configure (Client is multithreaded for load testing)
💻 JAVA
字号:
/** * Title:        <p> * Description:  <p> * Copyright:    Copyright (c) <p> * Company:      <p>  * @version 1.0 */import java.net.*;import java.io.*;import java.util.*;public class HTTPServer {  public static void main(String args[])  {    DataInputStream inServer;    int nServerReturn = 0;    String sReturnMsg;    BufferedWriter bwLogFile = null;    String sTempRead = null;    String sLogFile = null;    int nPort = 80;    Date now = null;    Properties cfgProps = new Properties();    // Read Config File    try    {       cfgProps.load(new FileInputStream("HTTPServer.cfg"));       sTempRead = cfgProps.getProperty("Port");       sLogFile = cfgProps.getProperty("LogFile");    }    catch (Exception e)    {//       System.out.println(e.toString());//       System.exit(0);       sTempRead = "8080";    }    if (sTempRead != null)    {       try       {          nPort = Integer.parseInt(sTempRead);       }       catch (Exception e)       {          System.out.println("'Port' in HTTPServer.cfg is not an integer!");       }    } // end if not null    if (sLogFile == null)    {       sLogFile = "HTTPServer.log";    }    try    {       bwLogFile = new BufferedWriter(new FileWriter(sLogFile, true));    }    catch (Exception e)    {       System.out.println(e);    }    System.out.println("Writing to Log file: " + sLogFile);    // Continue Listening    boolean bAlwaysUp = true;    ByteArrayOutputStream baosOutput = new ByteArrayOutputStream();    // Setup connection to server    try    {       ServerSocket ss = new ServerSocket(nPort);       now = new Date();       System.out.println(now.toString() + " Started Listening on Port " + nPort + "...");       bwLogFile.write(now.toString() + " Started Listening on Port " + nPort + "...");       bwLogFile.newLine();       bwLogFile.flush();       Socket sk = null;       while (bAlwaysUp)       {         //  sSize = "hi";         sk = ss.accept();         sk.setSoTimeout(1000);         System.out.println(now.toString() + " Accepted connection on Port " + nPort + " " +                    "from " + sk.getInetAddress() + "...");         bwLogFile.write(now.toString() + " Accepted connection on Port " + nPort + " " +           "from " + sk.getInetAddress() + "...");         bwLogFile.newLine();         bwLogFile.flush();         inServer = new DataInputStream(sk.getInputStream());         byte bRead[] = new byte[1024];         int TotalReceived = 0;         // Read Information from server         try         {            while ((nServerReturn = inServer.read(bRead)) > 0)            {               baosOutput.write(bRead, 0, nServerReturn);               if (nServerReturn > 0)               {                 TotalReceived += nServerReturn;               }            } // end while continuing read         }         catch (Exception e)         {           System.out.println("*** TIME OUT **");           // Time out read         }         System.out.println("Client Posted total of: " +TotalReceived);         String sRead = new String(baosOutput.toByteArray());         byte[] barray = baosOutput.toByteArray();         System.out.println(now.toString() + " Client Posted:\n" +sRead);/*         for (int x = 0; x < barray.length; x++)         {           System.out.print(barray[x] + ",");         }*/         bwLogFile.write(now.toString() + " Client Posted:\n" + sRead);         bwLogFile.newLine();         bwLogFile.flush();         sReturnMsg = sGetReply();         System.out.println(now.toString() + " Sending Message:\n" + sReturnMsg);         bwLogFile.write(now.toString() + " Sending Message:\n" + sReturnMsg);         bwLogFile.newLine();         bwLogFile.flush();Thread.sleep(500);/*         PrintStream psPostToCGI = new PrintStream(sk.getOutputStream());         psPostToCGI.print(sReturnMsg);         psPostToCGI.flush();*/      byte[] barray2 = sReturnMsg.getBytes();      DataOutputStream dOut = new DataOutputStream(sk.getOutputStream());      dOut.write(barray2);         System.out.println(now.toString() + " Message Sent Successfully.\n");         bwLogFile.write(now.toString() + " Message Sent Successfully.\n");         bwLogFile.newLine();         bwLogFile.flush();      //   sk.close();// ** FOR SSL         try         {            while ((nServerReturn = inServer.read(bRead)) > 0)            {               baosOutput.write(bRead, 0, nServerReturn);            } // end while continuing read         }         catch (Exception e)         {           System.out.println("*** TIME OUT **");           // Time out read         }         sRead = new String(baosOutput.toByteArray());         System.out.println(now.toString() + " Client Posted:\n" +sRead);     } // end while always on server     ss.close();     bwLogFile.close();   } // end try   catch(Exception e)   {     System.out.println("Exception Caught: " + e.toString());   }   System.out.println("Server shutting down...");  } // end main  private static String sGetReply()  {    String file = sGetFile();    String sReturnMsg = "HTTP/1.1 200 OK\r\n" +    "Server: Netscape-Enterprise/4.1\n" +    "Date: Thu, 12 Apr 2001 01:55:35 GMT\n" +    "Cache-Control: private, no-transform\n" +    "Content-Type: text/html\n" +    "Content-Length: " + file.length() + "\n" +    "Set-Cookie: SWEsessionID_NKKTJRQ=dxzmcgn5kjxdoot3kugteey1kpjlypt___REG; Version=1\n" +    "Connection: close\r\n\r\n" + file;  //     String sReturnMsg = "HTTP/1.0 200 Connection established\r\n" +    //                       "Proxy-agent: CacheFlow-Proxy/1.0\r\n\r\n";    return sReturnMsg;  } // end sGetReply  private static String sGetFile()  {    File f = new File("C:/temp/test.html");    return mario.search.FileToString.loadString(f);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -