Solved

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

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 50
Should localization be done inside spring controller 5 26
Error in @AspectJ Based AOP with Spring 2 13
spring jars download 1 27
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

777 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