Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Anyway to speedup TCP connection?  RSS feed

 
Arun Bommannavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I code a keep alive myself?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!