Client Server Communication in Java - Which Approach to Use?

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

  • Establish a connection and store it in memory until the operation completes and the client receives a response.
  • Establish a connection, send data, close the connection. Now the processing is in progress, and once it is complete, the server can establish a connection with the client to send data.
  • Establish a connection, send data, close the connection. Processing is in progress. the client asks the server every n minutes / seconds if the operation is complete. If processing is complete, the client retrieves the data.

I was wondering which approach would be the best use case. Maybe some kind of "de facto" standard for solving this problem? How expensive is it to open a socket in Java? Solution 1. Seems pretty frustrating to me, but 2. and 3. could do. The problem with solution 2. is that the server needs to know which port the client is listening on, while solution 3. adds some network overhead.

+2


a source to share


2 answers


I see an immediate issue with option 2. If the client is behind a firewall, it may well be allowed to connect and make the request, but the server may be denied to connect back to cilent.



As you say, option 1 looks a little unpleasant (not too nasty, but it can work well), so among the options listed, I would go for option 3. Perhaps the server could estimate the time remaining from processing, and tell the client, in each survey, about when it takes time to check.

0


a source


  • good enough
  • won't work in many situations, for example wne client is under firewall, NAT, etc. The server usually accepts incoming connections all over the place, desktops usually don't
  • better than 1 because you won't have any problems if the connection fails.
  • 1 + 3 solutions - make long connection waits, with intermittent sleep and reconnect. I mean: connect to server, wait 30 seconds for data, if no data, sleep for 10 seconds, loop.


Opening sockets is sometimes expensive, but not as expensive as your processing.

+2


a source







All Articles