简体   繁体   中英

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. I am in charge of writing the android application, and have written a functioning prototype using Android Studio and 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. 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. 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. That's quite simple to do in 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. Then either call QTcpSocket::waitForConnected or let yourself be informed asynchronously of the established connection by using the SIGNAL(connected()) of the QTcpSocket. If connected, you can call QTcpSocket::write or QTcpSocket::read. If you want to read asynchronously you can connect to SIGNAL(readyRead()) of the QTcpSocket.

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. More complex data could be packet into an XML format. 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.

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. 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.

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.

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