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
Solved

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

Posted on 2015-02-13
3
113 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
  • 2
3 Comments
 
LVL 27

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 27

Accepted Solution

by:
dpearson earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 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