File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Anyway to speedup TCP connection? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Anyway to speedup TCP connection?" Watch "Anyway to speedup TCP connection?" New topic
Author

Anyway to speedup TCP connection?

Arun Bommannavar
Ranch Hand

Joined: Jan 11, 2003
Posts: 53
I notice that to make the initial TCp Socket connection, it is taking about 4.5 sec.

Socket tcpSocket = new Socket(ipAddress,tcpPort);
where ipAddress is a string "xxx.xxx.xxx.xxx" and tcpPort is an int.

Is this about expected time for a TCP Socket connection? or is there something I am missing and could be faster?

Regards
Arun
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd say you're at the mercy of the network and the partner system. There's not much you can do to improve on those. 4.5 seconds seems like a long time, though, considering your browser connects to remote web sites dozens of times per page sometimes to get all the graphics and CSS files and such and still shows pages faster than that.
[ November 28, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
I'd say you're at the mercy of the network and the partner system. There's not much you can do to improve on those. 4.5 seconds seems like a long time, though, considering your browser connects to remote web sites dozens of times per page sometimes to get all the graphics and CSS files and such and still shows pages faster than that.


Unless the browser uses "keep-alive" for the connection, which it probably does.

Moving to our Internet Protocols forum...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
How would I code a keep alive myself?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35223
    
    7
HTTP keep-alive works by sending several requests sequentially over the same connection. Normally, HTTP is connection-less, meaning a connection is opened, a request sent, the response read and the connection closed. If a page triggers subsequent requests (e.g. for images, CSS, JavaScript), then each of those items requires a new connection, which introduces a lot of overhead. Keep-alive lets the browser use a single connection for multiple subsequent requests, which can be noticeably faster.

But that's something the HTTP protocol (version 1.1) adds, not something that works for all TCP/IP connections (although the principle may be applicable, e.g. when using database connection pools).
[ November 29, 2006: Message edited by: Ulf Dittmer ]

Android appsImageJ pluginsJava web charts
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ah, HERE is some help from Sun. This is pretty cool:

When the application finishes reading the response body or when the application calls close() on the InputStream returned by URLConnection.getInputStream(), the JDK's HTTP protocol handler will try to clean up the connection and if successful, put the connection into a connection cache for reuse by future HTTP requests.


Guess we get that for free. What a deal.

Now that I think about it, I have a program that downloads a set of files from a server. I can see the first one take a couple seconds to connect but the others have virtually no delay between files.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: Anyway to speedup TCP connection?
 
Similar Threads
SQLServerException: TCP/IP connection to the host has failed.
SSL Communication between Java and VC++
Socket Connection Problem
tcp socket communication-persistence connection
SCJD? RMI or serialized objects over TCP socket connections?