aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Socket reuse and security Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Socket reuse and security" Watch "Socket reuse and security" New topic
Author

Socket reuse and security

Tarun Sukhani
Ranch Hand

Joined: Aug 27, 2001
Posts: 53
My question is two-fold and concerns sockets:
A) Is there any notion of socket reuse in a socket-based solution for this assignment? That is, if we assign a thread to each server.accept call, is it necessary to close the socket once the thread's run() is over? Do all multi-threaded socket servers work this way?
B) I discovered that I could have a working solution without any policy files for a socket-based solution. Is this always the case; i.e., is it necessary to have a policy file for a socket-based client/server solution?
Thanks
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Hi,
Not sure about part B as my solution is going to use RMI.
As for part A)
I think you're on the right lines as you realise that creating a new thread every time a a request is accepted can be quite a load on resources. Although threads are not as resource hungry as processes they still take time to create and tear down. In order to optimise this the server can maintain a pool of worker threads to which it passes a request for servicing. Once the request has been serviced the thread returns to the pool so that it can be reused.
In order to speed things up on the client-side you can implement a connection pool. A pool of socket connections to the server is created and when needed the client requests a new socket it obtains one from the pool. Once its finished it returns it to the pool for another client to use.
The client is usually unaware that they are using a connection pool as the pool wraps up the object the client is trying to get access to.
There is a javaworld article (http://www.javaworld.com/javaworld/jw-06-1998/jw-06-object-pool.html) which provides some advice on object pools.
I hope this helps,
Cheers,
Steve
Tarun Sukhani
Ranch Hand

Joined: Aug 27, 2001
Posts: 53
Steve, thank you for your response. Unfortunately, I don't believe this answers my question completely. Although it is true that on the server side I can have a pool of worker threads to handle socket requests, this only saves memory and resources with thread creation, not socket creation, which was my principal concern.
On the client side, whenever I tried to reuse a socket I created after opening and closing its streams, it would not work; i.e., the application would hang indefinitely. What you seem to be describing in your post is multiple clients reusing a pool of socket connections, which is not the case here, since all we have is a single client making socket connections to the server, one socket connection per business method request. There is no intermediary proxy service that is accepting requests from multiple clients here.
If I'm off the mark, please rectify my statements. Otherwise, I guess I'm still looking for a response that addresses <B>socket</B> reuse, not thread reuse.

Originally posted by Steve Granton:
Hi,
Not sure about part B as my solution is going to use RMI.
As for part A)
I think you're on the right lines as you realise that creating a new thread every time a a request is accepted can be quite a load on resources. Although threads are not as resource hungry as processes they still take time to create and tear down. In order to optimise this the server can maintain a pool of worker threads to which it passes a request for servicing. Once the request has been serviced the thread returns to the pool so that it can be reused.
In order to speed things up on the client-side you can implement a connection pool. A pool of socket connections to the server is created and when needed the client requests a new socket it obtains one from the pool. Once its finished it returns it to the pool for another client to use.
The client is usually unaware that they are using a connection pool as the pool wraps up the object the client is trying to get access to.
There is a javaworld article (http://www.javaworld.com/javaworld/jw-06-1998/jw-06-object-pool.html) which provides some advice on object pools.
I hope this helps,
Cheers,
Steve
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Hi,
I was simply describing how socket re-use is normally used in the real world not in the assignment. If you client will have exclusive use of one socket connection then I'm not sure what the problem is. Surely its simply the case that once the server thread has finished its work it blocks on until the next request arrives.
I think the problem is that you are closing the stream. The Java API advises that a stream cannot be reopened once closed. So I think your problem is with the stream and not the socket. If you want the socket to be used with more than one request I think you'll need to avoid closing the stream associated with that socket.
I hope this is more helpful than my last post,
Cheers,
Steve
Tarun Sukhani
Ranch Hand

Joined: Aug 27, 2001
Posts: 53
Steve, that's what's frustating for me. When I tried not closing the socket streams on either end (client and server), I still could not reuse the streams after objects were sent and received. This confuses me. Do you have any clues or example code of socket (and thus stream) re-use?
Originally posted by Steve Granton:
Hi,
I was simply describing how socket re-use is normally used in the real world not in the assignment. If you client will have exclusive use of one socket connection then I'm not sure what the problem is. Surely its simply the case that once the server thread has finished its work it blocks on until the next request arrives.
I think the problem is that you are closing the stream. The Java API advises that a stream cannot be reopened once closed. So I think your problem is with the stream and not the socket. If you want the socket to be used with more than one request I think you'll need to avoid closing the stream associated with that socket.
I hope this is more helpful than my last post,
Cheers,
Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket reuse and security