简体   繁体   English

如何实现良好的客户端-服务器方法?

[英]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. 我正在开发分布式应用程序,并且需要将基于客户端Java的服务器连接到基于C++的服务器。 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. 现在,我正在尝试与Sockets进行基本通信,但是我并没有真正进行通信。 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? 也许是更高级别的库(对于c ++和java,我将需要它)还是完全不同的方式?

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). 我的C ++程序有一个主循环,在这里我想进行一个类似GetUpdatedDataFromRemoteDevice()的调用,在其中读取以前从网络(例如套接字)更新的一些数字变量的新值。

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. 最终,C ++程序将需要向远程设备发送一条消息,告诉他发送其他类型的数据,然后,继续获取更新后的值。

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. 从Java程序(远程设备)开始,正在运行的应用程序是一个可触摸的交互式屏幕,它不能被网络传输阻止,因为它必须一直为用户工作,因此所有联网都应在单独的线程中进行。

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. 该线程应该连接到服务器,并在按下按钮时开始循环发送数据(4个不断变化的数值),直到发生另一个事件为止。

It would be nice also to be easily re-connectable to the server. 可以轻松地重新连接到服务器也很不错。

ICE is a modern and good library for distributed applications: ICE是一个现代的,很好的分布式应用程序库:

  • many languages as C++ and Java 许多语言,例如C ++和Java
  • many platforms 许多平台
  • GNU GPL 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. 然后,您实现C ++和Java源代码来处理这些消息。

More info at http://zeroc.com/ice.html 有关更多信息,请访问http://zeroc.com/ice.html
Have fun ;-) 玩得开心 ;-)

EDIT: I have to use ACE in some projects. 编辑:我必须在某些项目中使用ACE 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 ;-) 我可以说ACE很老,也许很成熟,但是使用了过时的C ++编码规则:-(因此ACE不像STLBOOST那样易于使用。此外,ACE并不是真正高效的...我更喜欢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. 硬件字节顺序,字符串编码,网络错误,重试,重复消息,Ack等。需要大量好的设计和仔细的编程。 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. 除非您需要从服务器到客户端的即时通知,否则建议您使用HTTP作为客户端和服务器之间的协议。 Client can poll server occasionally for new messages. 客户端可以偶尔轮询服务器以获取新消息。

Anyway the problem has been solved multiple times already. 无论如何,这个问题已经被解决了好多次了。

http://activemq.apache.org/ http://activemq.apache.org/

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

http://www.cs.wustl.edu/~schmidt/ACE-overview.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. 以我为例,使用JNI将C ++应用程序连接到本地Java应用程序更容易,然后使两个Java应用程序相互通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实现多个客户端 - 服务器聊天 - how to implement multiple client-server chat 我在哪里可以获得在websocket中编写客户端服务器应用程序的好例子? - where can i get good example to write client-server application in websocket? 除了 Spring Oauth 和外部 Oauth 提供者之外,我如何在 SpringBoot 客户端-服务器通信中实现 Oauth2? - How can i implement Oauth2 in SpringBoot client-server comunnication beside of Spring Oauth and external Oauth providers? 如何在多线程 Swing MVC 客户端-服务器程序中实现 SwingWorker? - How to implement SwingWorker in multithreaded Swing MVC client-server program? 服务器如何与客户端服务器系统中的特定客户端通信? - How can the server communicate to a specific client in a Client-Server system? 如何使用Java线程模拟客户端服务器应用程序? - How can I simulate a Client-Server application using Java threads? 客户端-服务器程序中逻辑的服务器端脚本编写方法 - Approach for server side scripting of logic in client-server program 如何使用此客户端服务器程序? - How to use this client-server program? 如何在客户端-服务器应用程序中解决此SocketException? - How to resolve this SocketException in client-server application? 如何在genymotion上运行客户端服务器? - How to run the client-server on genymotion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM