?
Solved

calculating roundtrip time for a http connection and socket for google etc

Posted on 2015-02-13
3
Medium Priority
?
122 Views
Last Modified: 2015-02-15
Hi,
I have a following piece of code which create an http connection to www.google.com :
 HttpURLConnection conn = null;
        boolean success = true;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestProperty("Connection", "close");
            conn.setConnectTimeout(50); //

            // put itmestamp here
            conn.connect();
            System.out.println("read timeout = "+conn.getReadTimeout());
            System.out.println("connectTimeout = " + conn.getConnectTimeout());

Open in new window


Here i have set connect time out manually. How do i find out exactly how much time it takes to connect.
Also i want to send some request data and receive response to calculate exactly how much time a rountrip takes.

How do i do it ?

Also same thing i want to do on a socket for which i have the following code :
SocketAddress sockaddr = new InetSocketAddress("www.google.com", 80);
        Socket socket = new Socket();
        socket.connect(sockaddr, 50);

        System.out.println("is socket connected = " + socket.isConnected());
        System.out.println("socket timeout = "+socket.getSoTimeout());
        socket.close();

Open in new window


But again here i want to send something on the socket and receive some result like a ping so as to know how much time it took.
thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 28

Expert Comment

by:dpearson
ID: 40609060
Record the time before you connect and check it after you connect:

            long timeBeforeConnect = System.currentTimeMillis() ;
            conn.connect();
            long elapsed = System.currentTimeMillis()  - start ;

Elapsed is how long it took to make the connection in milliseconds.  If it times out it'll be the time it took to timeout.

Same for the socket.

If you want to measure the roundtrip time to send some data over the pipe and get a response you can use the same pattern.  Record the time before you send the request.  Then check the time when you get the response - the difference is how long it took to do the roundtrip.

Doug
0
 

Author Comment

by:Rohit Bajaj
ID: 40609281
Hi
What request should i send over socket to calculat roundtrip time ? Plz give me an example for google.
0
 
LVL 28

Accepted Solution

by:
dpearson earned 2000 total points
ID: 40611200
Here's an example of how to format an HTTP request over a raw socket:
http://code.joejag.com/2012/how-to-send-a-raw-http-request-via-java.html

The example happens to also be using google as its test target.

Hope that helps,

Doug
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question