• Post Reply Bookmark Topic Watch Topic
  • New Topic

reuse socket  RSS feed

 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
T.A. Nguyen
Ranch Hand
Posts: 36
Eclipse IDE Java ME Oracle
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!