简体   繁体   中英

How can I implement a good Client-Server approach?

I'm developing a distributed application, and I need to connect a client Java based to a server C++ based. Both of them will need to send information to each other, but I need them to be able to do things while waiting for the information, and they don't know when they are gonna get new information, or send information.

How can I achieve this? Now I'm trying to implement a basic communication with Sockets, but I don't really get to communicate them. I have read that using sockets + threads is usually a good approach for client-server apps.

Could you please recommend me some web or book to read about this, or send me some example code to learn?

Do you think that i should use other approach, better than sockets? maybe a higher level library (i would need it for c++ and java) or a totally different way?

EDIT:

I will add some extra information.

What I would love to achieve is the following:

My C++ program has a main loop, where I would like to have a call like GetUpdatedDataFromRemoteDevice() where I read the new values of some numerical variables that previously got updated from the net (the socket, for example).

Eventually, the C++ program will need to send a message to the remote device, to tell him to send other kind of data, and after that, keep getting the updated values.

From the Java program (remote device) the application running is an interactive touchable screen, that cant get blocked by the network transmissions, because it must keep working for the user, so all the networking should be done in a separated thread.

That thread, should connect to the server, and when a button is pushed, start to send the data (4 changing numerical values) in a loop until another event happens.

It would be nice also to be easily re-connectable to the server.

ICE is a modern and good library for distributed applications:

  • many languages as C++ and Java
  • many platforms
  • GNU GPL
  • good performance
  • easy to use

First, you define the messages you want to exchange between server and client.
Then, you implement the C++ and Java source code to handle these messages.

More info at http://zeroc.com/ice.html
Have fun ;-)

EDIT: I have to use ACE in some projects. I can tell ACE is very old, maybe mature, but uses outdated C++ coding rules :-( Therefore ACE is not as easy to use as STL or BOOST . Moreover, ACE is not really efficient... I prefer ICE ;-)

I don't know what your application is but robust client server socket programming is pretty hairy task to do properly. Hardware byte order, String encoding, Network errors, retries, duplicate messages, acks etc.. require lots of good design and careful programming. You need to get it work well as single-threaded before even thinking using multiple threads.

Unless you need instant notifications from server to client I suggest that you use HTTP as protocol between client and server. Client can poll server occasionally for new messages.

Anyway the problem has been solved multiple times already.

http://activemq.apache.org/

http://www.rabbitmq.com/devtools.html

http://www.cs.wustl.edu/~schmidt/ACE-overview.html

I did something of this sort once. In my case it was easier to connect my C++ app to a local Java app using JNI and then have the two Java apps talk to each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM