2015-07-21 30 views
0

我有一個服務器來獲取響應頭,通過它我檢測到設備的類型。有什麼方法可以通過響應頭或其他方法獲得Internet速度嗎?如何從java中的響應頭中檢測客戶端的Internet速度?

Server_X:

public class Server_X { 

    static int count = 0; 

    public static void main(String args[]) { 

     Socket s = null; 
     ServerSocket ss2 = null; 
     System.out.println("Server Listening......"); 
     try { 

      // can also use static final PORT_NUM, when defined 
      ss2 = new ServerSocket(4445); 

     } catch (IOException e) { 
      e.printStackTrace(); 
      System.out.println("Server error"); 
     } 

     while (true) { 
      try { 
       s = ss2.accept(); 
       System.out.println("connection Established"); 
       ServerThread st = new ServerThread(s); 
       count++; 
       System.out.println("total connections :" + count); 
       st.start(); 
      } 

      catch (Exception e) { 
       e.printStackTrace(); 
       System.out.println("Connection Error"); 
      } 
     } 
    } 
} 

ServerThread:

class ServerThread extends Thread { 

    static String uagent, uaccept; 
    static String[] b; 
    static String[] c; 
    Server_X obj = new Server_X(); 
    String line = null; 
    BufferedReader is = null; 
    PrintWriter os = null; 
    Socket s = null; 

    public ServerThread(Socket s) { 
     this.s = s; 
    } 

    public void run() { 
     try { 
      is = new BufferedReader(new InputStreamReader(s.getInputStream())); 
      os = new PrintWriter(s.getOutputStream()); 

     } catch (IOException e) { 
      System.out.println("IO error in server thread"); 
     } 

     try { 
      line = is.readLine(); 
      while (line.compareTo("QUIT") != 0) { 

       os.println(line); 
       os.flush(); 
       // System.out.println(line); 

       line = is.readLine(); 
       b = line.split(":"); 
       if (b[0].equals("User-Agent")) { 
        uagent = b[1]; 
        // System.out.println(uagent); 

       } 
       c = line.split(":"); 
       if (c[0].equals("Accept")) { 
        uaccept = c[1]; 
        // System.out.println(uaccept); 
       } 
       UAgentInfo detect = new UAgentInfo(uagent, uaccept); 
      } 

     } catch (IOException e) { 

      line = this.getName(); // reused String line for getting thread name 
      // System.out.println("IO Error/ Client "+line+" terminated abruptly"); 
     } catch (NullPointerException e) { 
      line = this.getName(); // reused String line for getting thread name 
      // System.out.println("Client "+line+" Closed"); 
     } finally { 
      try { 
       System.out.println("Connection Closing.."); 
       if (is != null) { 
        is.close(); 
        // System.out.println(" Socket Input Stream Closed"); 
       } 

       if (os != null) { 
        os.close(); 
        // System.out.println("Socket Out Closed"); 
       } 
       if (s != null) { 
        s.close(); 
        // System.out.println("Socket Closed"); 
        obj.count--; 
        System.out.println("Toatal connections (after closing):" 
          + obj.count); 
       } 

      } catch (IOException ie) { 
       // System.out.println("Socket Close Error"); 
      } 
     }// end finally 
    } 
} 
+0

縮進和格式化代碼。 必需:研究工作,作者這樣做,這將是很容易回答 –

+0

簽出此:http://jmeter.apache.org/和這:http://stackoverflow.com/questions/4127836/how-to- detect-internet-connection-speed-with-java – Ian2thedv

+0

爲什麼[你不應該解析用戶代理字符串]的很多_many_文章之一(http://ryanmorr.com/the-state-of-browser-detection/ ) –

回答

0

你沒有指定服務器是什麼協議使用;我想你是抓住「用戶代理」和「接受」的HTTP。如果我是正確的,則沒有包含您要查找的信息的標題,因爲您可以查看https://en.wikipedia.org/wiki/List_of_HTTP_header_fields

+0

那麼如何檢測互聯網的速度?告訴我任何可能的方式,我可以添加到我的代碼 –

+0

我敢肯定,你可以谷歌「檢測網絡速度Java」... –

相關問題