Solved

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

Posted on 2015-02-13
3
110 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 26

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 26

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 60
How Complex Is This Java Course ? 9 63
Free Alternative to JIRA 4 58
maven project error 5 21
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now