2013-07-28 129 views
0

我已經在android中創建了一個web服務器,它工作正常,它沒有顯示任何錯誤,並且我有日誌說服務器在端口9000上啓動,但是當我輸入我的電話的IP時說服務器需要很長時間才能連接。網絡服務器需要太多的時間來響應android

Jhtts文件中:(在其中運行在服務器類)

package dolphin.developers.com; 

import java.io.File; 
import java.io.IOException; 
import java.net.InetAddress; 
import java.security.acl.Owner; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Environment; 
import android.widget.Toast; 
import dolphin.devlopers.com.R; 



public class JHTTS extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     // TODO Auto-generated method stub 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.facebook); 


     try{ 
      File documentRootDirectory = new File ("/sdcard/samer/"); 
      JHTTP j = new JHTTP(documentRootDirectory,9000); 
      j.start(); 




     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 



     } 

    } 
} 

JHTTP類:

package dolphin.developers.com; 

import java.io.File; 
import java.io.IOException; 
import java.net.InetAddress; 
import java.net.ServerSocket; 
import java.net.Socket; 

import android.util.Log; 

public class JHTTP extends Thread {   



      private File documentRootDirectory; 
      private String indexFileName = "index.html"; 
      private ServerSocket server; 
      private int numThreads = 50; 

      public JHTTP(File documentRootDirectory, int port, 
         String indexFileName) throws IOException { 

    if (!documentRootDirectory.isDirectory()) { 

    throw new IOException(documentRootDirectory 

        + " does not exist as a directory"); 
} 


        this.documentRootDirectory = documentRootDirectory; 
        this.indexFileName = indexFileName; 
        this.server = new ServerSocket(port); 
} 

     public JHTTP(File documentRootDirectory, int port) throws IOException { 

       this(documentRootDirectory, port, "index.html"); 
    } 

     public JHTTP(File documentRootDirectory) throws IOException { 

        this(documentRootDirectory, 80, "index.html"); 
    } 

     public void run() { 


      try { 
       Process process = Runtime.getRuntime().exec("su"); 
       process.waitFor(); 
       } catch (IOException e) { 
       e.printStackTrace(); 
       } catch (InterruptedException e) { 
       e.printStackTrace(); 
       } 


      for (int i = 0; i < numThreads; i++) { 

      Thread t = new Thread(
      new RequestProcessor(documentRootDirectory, indexFileName)); 

      t.start(); 
     } 


     System.out.println("Accepting connections on port " + server.getLocalPort()); 

     System.out.println("Document Root: " + documentRootDirectory); 

     while (true) { 

     try { 

     Socket request = server.accept(); 

     request.setReuseAddress(true); 

     RequestProcessor.processRequest(request); 

     } 




     catch (IOException ex) { 

     } 




} 


} 

     public static void main(String[] args) { 
// get the Document root 

     File docroot; 

     try { 

     docroot = new File("D:/"); 

    } 


     catch (ArrayIndexOutOfBoundsException ex) { 

      System.out.println("Usage: java JHTTP docroot port indexfile"); 
      return; 


      } 

// set the port to listen on 




     try { 


      int port; 


      port = 9000; 



     JHTTP webserver = new JHTTP(docroot, port); 

     webserver.start(); 

     } 


     catch (IOException ex) { 

     System.out.println("Server could not start because of an " 

     + ex.getClass()); 

     System.out.println(ex); 

     } 

     } 

     } 

請求處理器類:

package dolphin.developers.com; 

import java.net.*; 
import java.io.*; 
import java.util.*; 


      public class RequestProcessor implements Runnable { 

      @SuppressWarnings("rawtypes") 
     private static List pool = new LinkedList(); 

      private File documentRootDirectory; 

      private String indexFileName = "index.html"; 

      public RequestProcessor(File documentRootDirectory, 

      String indexFileName) { 


     if (documentRootDirectory.isFile()) { 

     throw new IllegalArgumentException(

     "documentRootDirectory must be a directory, not a file"); 

     } 

     this.documentRootDirectory = documentRootDirectory; 

     try { 

    this.documentRootDirectory 

    = documentRootDirectory.getCanonicalFile(); 

    } 


    catch (IOException ex) { 

    } 

    if (indexFileName != null) this.indexFileName = indexFileName; 

    } 


    @SuppressWarnings("unchecked") 
    public static void processRequest(Socket request) { 
    synchronized (pool) { 
    pool.add(pool.size(), request); 
    pool.notifyAll(); 

    } 

    } 

    public void run() { 
// for security checks 

    String root = documentRootDirectory.getPath(); 
    while (true) { 
    Socket connection; 

    synchronized (pool) { 

    while (pool.isEmpty()) { 

    try { 

    pool.wait(); 
} 



catch (InterruptedException ex) { 


} 

} 


    connection = (Socket) pool.remove(0); 

     } 

    try { 


    String filename; 

    String contentType; 

    OutputStream raw = new BufferedOutputStream(

    connection.getOutputStream() 


    ); 


    Writer out = new OutputStreamWriter(raw); 




    Reader in = new InputStreamReader(

    new BufferedInputStream(

    connection.getInputStream() 

    ),"ASCII" 

    ); 


    StringBuffer requestLine = new StringBuffer(); 

    int c; 

    while (true) { 

    c = in.read(); 
    if (c == '\r' || c == '\n') break; 
    requestLine.append((char) c); 

    } 

      String get = requestLine.toString(); 
// log the request 

     System.out.println(get); 

     StringTokenizer st = new StringTokenizer(get); 

     String method = st.nextToken(); 

     String version = ""; 

     if (method.equals("GET")) { 

     filename = st.nextToken(); 

     if (filename.endsWith("/")) filename += indexFileName; 

     contentType = guessContentTypeFromName(filename); 

     if (st.hasMoreTokens()) { 

     version = st.nextToken(); 
} 
    File theFile = new File(documentRootDirectory, 
    filename.substring(1,filename.length())); 
    if (theFile.canRead() 
// Don't let clients outside the document root 
    && theFile.getCanonicalPath().startsWith(root)) { 

    DataInputStream fis = new DataInputStream(

    new BufferedInputStream(

    new FileInputStream(theFile) 
) 

    ); 
    byte[] theData = new byte[(int) theFile.length()]; 
    fis.readFully(theData); 
    fis.close(); 
    if (version.startsWith("HTTP ")) { // send a MIME header 

    out.write("HTTP/1.0 200 OK\r\n"); 


    Date now = new Date(); 
    out.write("Date: " + now + "\r\n"); 
    out.write("Server: JHTTP/1.0\r\n"); 
    out.write("Content-length: " + theData.length + "\r\n"); 
    out.write("Content-type: " + contentType + "\r\n\r\n"); 
    out.flush(); 

    } // end if 
// send the file; it may be an image or other binary data 
// so use the underlying output stream 
// instead of the writer 
raw.write(theData); 
raw.flush(); 

    } // end if 

    else { // can't find the file 

    if (version.startsWith("HTTP ")) { // send a MIME header 

    out.write("HTTP/1.0 404 File Not Found\r\n"); 

    Date now = new Date(); 

    out.write("Date: " + now + "\r\n"); 

    out.write("Server: JHTTP/1.0\r\n"); 

    out.write("Content-type: text/html\r\n\r\n"); 

    } 

    out.write("<HTML>\r\n"); 
    out.write("<HEAD><TITLE>File Not Found</TITLE>\r\n"); 
    out.write("</HEAD>\r\n"); 
    out.write("<BODY>"); 

    out.write("<H1>HTTP Error 404: File Not Found</H1>\r\n"); 
    out.write("</BODY></HTML>\r\n"); 
    out.flush(); 

    } 

    } 

    else { // method does not equal "GET" 

    if (version.startsWith("HTTP ")) { // send a MIME header 

    out.write("HTTP/1.0 501 Not Implemented\r\n"); 

    Date now = new Date(); 

    out.write("Date: " + now + "\r\n"); 

    out.write("Server: JHTTP 1.0\r\n"); 

    out.write("Content-type: text/html\r\n\r\n"); 

    } 

    out.write("<HTML>\r\n"); 
    out.write("<HEAD><TITLE>Not Implemented</TITLE>\r\n"); 
    out.write("</HEAD>\r\n"); 
    out.write("<BODY>"); 
    out.write("<H1>HTTP Error 501: Not Implemented</H1>\r\n"); 

    out.write("</BODY></HTML>\r\n"); 

    out.flush(); 

    } 

    } 

    catch (IOException ex) { 

    } 

    finally { 

    try { 

    connection.close(); 

    } 

    catch (IOException ex) {} 

} 
} // end while 

    } // end run 

    public static String guessContentTypeFromName(String name) { 

    if (name.endsWith(".html") || name.endsWith(".htm")) { 

    return "text/html"; 

    } 

    else if (name.endsWith(".txt") || name.endsWith(".java")) { 

    return "text/plain"; 


    } 


    else if (name.endsWith(".gif")) { 

    return "image/gif"; 

    } 

    else if (name.endsWith(".class")) { 

    return "application/octet-stream"; 

} 


    else if (name.endsWith(".jpg") || name.endsWith(".jpeg")) { 


    return "image/jpeg"; 

    } 

    else if (name.endsWith(".png")) { 


     return "image/png"; 

     } 

    else if (name.endsWith(".js")) { 


     return "text/javascript"; 

     } 

    else if (name.endsWith(".js")) { 


     return "text/javascript"; 

     } 

    else if (name.endsWith(".css")) { 


     return "text/css"; 

     } 

    else return "text/plain"; 

    } 


    } // end RequestProcessor 

logcat的窗口:

Accepting connections on port 9000 
Document Root: D:\ 

現在,當我在我的日誌連接到我的Web服務器,我得到

logcat的更新:

07-29 14:42:46.175: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol 
07-29 14:47:46.195: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol 

回答

1

日誌說,它接受端口80上的連接,但你的服務器在端口9000上運行 - 我猜那就是問題所在。

+0

對不起,我已編輯它.. – Prakhar

+0

看看新的代碼.. – Prakhar

+0

我在avd運行它是這個問題.. – Prakhar

相關問題