简体   繁体   English

通过Java中的套接字发送数据并使用QT套接字以C ++接收

[英]Sending data over sockets in Java and receiving in C++ with QT sockets

This may be a stupid question, but for a software engineering project it is necessary to have an android application send data to a backend application, and the backend application forward the data to a Desktop C++ application. 这可能是一个愚蠢的问题,但对于软件工程项目,必须让android应用程序将数据发送到后端应用程序,然后后端应用程序将数据转发到Desktop C ++应用程序。 I am in charge of writing the android application, and have written a functioning prototype using Android Studio and java. 我负责编写android应用程序,并使用Android Studio和java编写了可运行的原型。 I have a test Java application running on my desktop that is able to function as a backend for my testing purposes, as my team has yet to develop either the backend or desktop application. 我的台式机上运行着一个测试Java应用程序,它可以作为后端用于我的测试目的,因为我的团队尚未开发后端或桌面应用程序。 My teammate claims that we will need to rewrite the app using C++ and QT in order to be able to send data from the app to a C++ application using QT's sockets, but based on my (admittedly ignorant) understanding of socket based networking, the data should be sent as a byte stream between the sockets, so the interaction between the two languages should not be an issue. 我的队友声称,为了能够使用QT的套接字将数据从应用程序发送到C ++应用程序,我们将需要使用C ++和QT重写应用程序,但是基于我对基于套接字的网络的理解(无知),数据应该在套接字之间以字节流的形式发送,因此两种语言之间的交互不应该成为问题。 So, my question is am I able to perform socket based data transfer between two different languages? 所以,我的问题是我是否可以在两种不同语言之间执行基于套接字的数据传输?

Of course you can. 当然可以。 As far as I understand your question you need a Qt application which can connect to a Java based TCP server and send/receive data. 据我了解您的问题,您需要一个Qt应用程序,该应用程序可以连接到基于Java的TCP服务器并发送/接收数据。 That's quite simple to do in Qt. 这在Qt中非常简单。 Actually there are different ways. 实际上有不同的方法。 Here just one. 这里只有一个。

Instantiate a QTcpSocket, call QTcpSocket::connectToHost with the IP address or DNS name of the server machine and a the port or the server process. 实例化QTcpSocket,使用服务器计算机的IP地址或DNS名称以及端口或服务器进程调用QTcpSocket :: connectToHost。 Then either call QTcpSocket::waitForConnected or let yourself be informed asynchronously of the established connection by using the SIGNAL(connected()) of the QTcpSocket. 然后,调用QTcpSocket :: waitForConnected或通过使用QTcpSocket的SIGNAL(connected())异步通知已建立的连接。 If connected, you can call QTcpSocket::write or QTcpSocket::read. 如果已连接,则可以调用QTcpSocket :: write或QTcpSocket :: read。 If you want to read asynchronously you can connect to SIGNAL(readyRead()) of the QTcpSocket. 如果要异步读取,则可以连接到QTcpSocket的SIGNAL(readyRead())。

Of course you have to agree on the protocol of your data exchange. 当然,您必须就数据交换协议达成一致。 Easiest (and not very efficient) is to translate everything to text, so if eg the Java server wants to send you a number, it just sends a string representation of the number. 最简单(但效率不高)是将所有内容转换为文本,因此,例如,如果Java服务器想要向您发送数字,则只需发送数字的字符串表示形式。 More complex data could be packet into an XML format. 可以将更复杂的数据打包为XML格式。 Hope this helps. 希望这可以帮助。

Have a look at https://doc.qt.io/qt-5/qtcpsocket.html or https://doc.qt.io/qt-5/qtnetwork-fortuneclient-example.html for more information. 有关更多信息, 访问https://doc.qt.io/qt-5/qtcpsocket.htmlhttps://doc.qt.io/qt-5/qtnetwork-fortuneclient-example.html

You can have two completely different receivers on two ends of the socket, and one can be Java, while other can be Qt C++ or anything else you fancy. 套接字的两端可以有两个完全不同的接收器,一个可以是Java,而另一个可以是Qt C ++或您喜欢的其他任何一种。 This is not a problem. 这不是问题。

The biggest problem is in how do you actually communicate - what is your protocol? 最大的问题在于您如何实际交流-您的协议是什么? When you are sending data, what are you sending? 发送数据时,您要发送什么? If, for example, you use rich Java serialization capabilities on the sending side, and just send Java objects, you will have very hard time deciphering this on the C++ side. 如果,例如,您在发送端使用丰富的Java序列化能力,并只发送Java对象,你会是艰难的时间在C ++侧破译这一点。

In heterogeneous environments, the best option would be to use some messaging protocol for data exchange. 在异构环境中,最好的选择是使用某种消息传递协议进行数据交换。 If performance is not a concern, Google Protocol Buffers are often suggested, as they have both Java and C++ bindings. 如果不考虑性能,则通常建议使用Google协议缓冲区,因为它们具有Java和C ++绑定。

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

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