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

📄 server.java

📁 S S D 1 练习的代码
💻 JAVA
字号:
/*
 * Created on 2008-3-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * SSD8 EX 1
 * @author Fantao
 * Server.java
 */

import java.io.*;  
import java.net.*;  
import java.util.*;  
  
  
public class Server {  
  
  /*  
   * Waiting for request from a client.   
   */  
	
  public Server() throws Exception {  
  
    /* server socket */  
    ServerSocket serverskt = new ServerSocket(8000);  
  
    /*  
     * open the socket for client connecting with.  
     */  
    while (true) {  
  
      /* client socket */  
      Socket clientskt = serverskt.accept();
  
      System.out.println("Accepting The Connection...\n");  
  
      /* reader and writer with client and file */ 
      
      FileInputStream inFromFile = null;  
      BufferedReader inFromClient = null;  
      DataOutputStream outToClient = null;  
  
      /* response the request */  
      try {  
        while (true) {  
  
          inFromClient = new BufferedReader(new InputStreamReader(  
              clientskt.getInputStream()));
          outToClient = new DataOutputStream(clientskt.getOutputStream());  
  
          System.out.println("Waiting for command...");  
          String cmd = inFromClient.readLine();  
          System.out.println("The request for: " + cmd);  
  
          /*  
           * If command error, write the error response and break. 
           *     
           */  
          
          StringTokenizer token = new StringTokenizer(cmd);  
          if (token.countTokens() < 2) {  
  
            System.out.println("Command style error!");  
            outToClient.writeBytes("HTTP/1.1 400 Bad Request\n\r");  
            break;  
  
          } else {  
  
            String method = token.nextToken();  
            String filename = token.nextToken();  
  
            if (!method.equals("GET")) {  
              System.out.println("Method error!");  
              outToClient  
                  .writeBytes("HTTP/1.1 400 Bad Request\n\r");  
              break;  
            }  
  
            if (filename.charAt(0) != '/') {  
              System.out.println("Filename command error!");  
              outToClient  
                  .writeBytes("HTTP/1.1 400 Bad Request\n\r");  
              break;  
            }  
  
            filename = filename.toUpperCase();  
  
            filename = (filename.equals("/") ? "INDEX.HTM"  
                : filename.substring(1));  
  
            File file = new File(filename);  
  
            if (!file.exists()) {  
              System.out.println("File not existed!");  
              outToClient  
                  .writeBytes("HTTP/1.1 404 Not Found\n\r");  
              break;  
            }  
  
            /*  
             * Response the right format request. 
             * write it to the client.    
             */ 
            
            System.out.println("Now respond the request...");  
            inFromFile = new FileInputStream(filename);  
  
            byte[] by = new byte[(int) file.length()];  
            inFromFile.read(by);  
  
            if (filename.endsWith(".HTM")) {  
              outToClient.writeBytes("HTTP/1.0 200 OK" + "\n\r");  
  
              outToClient.writeBytes("Content-Type: txt/html"  
                  + "\n\r");  
  
              outToClient.writeBytes("Content-Length: "  
                  + file.length() + "\n\r\n\r");  
            }  
  
            outToClient.write(by);  
            outToClient.flush();  
            outToClient.writeBytes("\n\r");  
            break;  
          }  
        }  
  
      } catch (Exception e) {  
        e.printStackTrace();  
      } finally {  
  
        /*  
         * Close all the reader and writer.   
         */ 

        System.out.println("Closing Connection...\n");  
  
        try {  
          if (inFromFile != null)  
            inFromFile.close();  
          if (inFromClient != null)  
            inFromClient.close();  
          if (outToClient != null)  
            outToClient.close();  
          if (clientskt != null)  
            clientskt.close();  
        } catch (IOException e) {  
        }  
      }  
    }  
  }  
  
  /*  
   * Open a server socket and request the responses from a client.  
   *  
   */
  
  public static void main(String[] args) {  
  
    try {  
      Server server = new Server();  
  
    } catch (Exception e) {  
        
      e.printStackTrace();  
    }  
  
  }  
}

⌨️ 快捷键说明

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