如何从java中的响应头检测客户端的Internet速度?

时间:2015-07-21 07:13:48

标签: java socketserver

我有一个服务器来获取响应头,我通过它检测设备的类型。有什么方法可以通过响应标头或任何其他方法获得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
    }
}

1 个答案:

答案 0 :(得分:0)

您没有指定服务器正在使用的协议;我想是HTTP,因为你正在捕捉" User-Agent"并且"接受"。如果我没有问题,那么您所查找的信息就没有标题,因为您可以查看https://en.wikipedia.org/wiki/List_of_HTTP_header_fields