views:

94

answers:

2

I have a typical client server communication - Client sends data to the server, server processes that, and returns data to the client. The problem is that the process operation can take quite some time - order of magnitude - minutes. There are a few approaches that could be used to solve this.

  1. Establish a connection, and keep it alive, until the operation is finished and the client receives the response.
  2. Establish connection, send data, close the connection. Now the processing takes place and once it is finished the server could establish a connection to the client to send the data.
  3. Establish a connection, send data, close the connection. Processing takes place. client asks server, every n minutes/seconds if the operation is finished. If the processing is finished the client fetches the data.

I was wondering which approach would be the best way to use. Is there maybe some "de facto" standard for solving this problem? How "expensive" is opening a socket in Java? Solution 1. seems pretty nasty to me, but 2. and 3. could do. The problem with solution 2. is that the server needs to know on which port the client is listening, while solution 3. adds some network overhead.

A: 

I see an immediate problem with option 2. If the client is behind a firewall, he might very well be allowed to connect and do the request, but the server might be prevented to connect back to the cilent.

As you say, option 1 looks a bit nasty (not too nasty though, could work well), so among the options listed, I would go for option 3. Perhaps the server could estimate the time that's left of the processing, and hint the client, in each poll, of when it's about time to check back.

aioobe
the thing with notifying client about how much time is left for processing is a good hint - although I can make only very rough estimate...
markovuksanovic
+2  A: 
  1. is good enought
  2. will not work at many situations, for example wne client is under firewall, NAT, and so on. Server usually accepts incoming connections from everywhere, desktops usualy not
  3. better than 1 just because you will haven't problems when connection is lost
  4. solutions 1+3 - make long waiting connections, with periodical sleep and reconnect after. I mean: connect to server, wait 30 sec for data, if no data received, sleep for 10 sec, loop.

Opening sockets is sometimes expensive, but not so expensive that your data processing.

splix
Well 4. solution is not good for me... I need to have the port free as long as possible - I want to consume as little resources as possible, and reduce the amount of configuration that needs to be done. I need to have n clients to be able to connect to the server and pass in data for processing. As the processing is being done other clients should be able to put in their data for processing, using the same port. At the moment, I don't think it would be good if I had to configure port for each client that wants to connect.
markovuksanovic
What the reason for configuriong different port for each user???? it's enought to open only one port for listening, for all connections from all clients.And sleeping connections not requires much resources (if your using NIO, of cource). You can process ten and hundreds of thousands simultaneous connections at one time (if you have enouhg RAM)
splix