• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reuse socket

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am uploading many files via an applet and a socket connection (multi-part). Once a file has been sent a call is made to socket.shutdownOutput() so an immediate response can be received indicating the status (bytes received, etc) of the last file transfer. After the response is received the streams and sockets are closed.

Then the next file is queued, a socket is recreated, and the streams are reassigned. So far everything functions well.

I am curious if is it possible and advisable to re-use the socket or are there issues such as timeout, etc.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That depends on the protocol you are using. It sounds like you are using HTTP. HTTP specifies that a single connection is used for a single request (or upload in your case). The Java runtime by default uses persistent HTTP connections to mitigate the inefficiency of creating new connections for each request.
Other protocols, for example, FTP, are more conversational and allow one to transfer more than one file per connection.
 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response. Yes you are correct. I am using http. In this case it makes sense to create a new socket for each file.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Robert Kennedy:
I am using http. In this case it makes sense to create a new socket for each file.



More than it makes sense, the HTTP specs require it. You make a GET, and the server returns one response. It can't return two. Similarly, when you make a PUT, you do one PUT.

There are many times when HTTP is not the answer, but in today's world of firewalls, NAT, etc. its often the only choice.

I've seen folks try to write the equivalent of TCP/IP on top of HTTP. Which is really silly, as under HTTP is of course, TCP/IP
 
Ranch Hand
Posts: 36
Eclipse IDE Java ME Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the same URL url.openConnection() to recreation the new connection. Make sure that you pass back the same session id, that way, the servlet will not take extra time to create a new session. In the case of HTTPS it will a be very expensive handshake. Session ID is passed on either by cookie or via url rewrite.

enjoy!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic