0

I have a server to get a response headers through which I detect the type of device. Is there any way I can get the Internet speed through response headers or any other method ?

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
    }
}
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
  • Indenting and formatting code. Required: Research work that author did so that it will be easy to answerable – Abhinav Singh Maurya Jul 21 '15 at 07:31
  • Checkout this: http://jmeter.apache.org/ and this: http://stackoverflow.com/questions/4127836/how-to-detect-internet-connection-speed-with-java – Ian2thedv Jul 21 '15 at 07:39
  • One of many _many_ articles on why [you shouldn't parse the user-agent string](http://ryanmorr.com/the-state-of-browser-detection/) – Stephen P Jul 21 '15 at 07:44
  • so how can detect the speed of internet ??? tell me any way possible that i can add to my code – Vansh Ahuja Jul 21 '15 at 08:19

1 Answers1

0

You didn't specify what protocol the server is using; I suppose is HTTP since you're catching "User-Agent" and "Accept". If I'm correct, there's no header with the information you're looking for, as you can check on https://en.wikipedia.org/wiki/List_of_HTTP_header_fields.

Andrea Iacono
  • 772
  • 7
  • 20