Search code examples
javac++qtsocketstcp

Data isn't transmitted between QTcpSockets and Java Sockets


So I have two applications, one is written in C++ using Qt5 and the other one is written in Java. The C++ application acts as a client that connects to a ServerSocket object in the java application which then instantiates a socket from the connection. When I then try to send a string to the server, I either have to send the string repetitively (like 20000 times) or the server doesn't receive anything at all.

QString s = " This is a test string that will be sent to the server upon connection\n";
QTcpSocket *sock = new QTcpSocket;
sock->connectToHost(QHostAddress::LocalHost, 5000, QIODevice::ReadWrite);
sock->waitForConnected(5000);
QTextStream out(sock);
while(!sock->waitForBytesWritten(3000)) {
        out << s << endl;
}

Where the code above would not yield any results, the following yields segmented strings as well as duplicate received strings for the server:

QString s = " This is a test string that will be sent to the server upon connection\n";
QTcpSocket *sock = new QTcpSocket;
sock->connectToHost(QHostAddress::LocalHost, 5000, QIODevice::ReadWrite);
sock->waitForConnected(5000);
QTextStream out(sock);
do {
        out << s << endl;
} while(!sock->waitForBytesWritten(3000) | 1);

The server is a simple java program like this:

ServerSocket ss = new ServerSocket(5000);
Socket s = ss.accept();
DataInputStream in = new DataInputStream(s.getInputStream());
while (true){
        String tmp = in.readUTF();
        System.out.println(tmp);
        if (tmp.contains(" ")){
              break;
}

I would expect the output to be a simple This is a test string that will be sent to the server upon connection; however, the result is either nothing or something along these lines:

his is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

 This is a test string that will be sent to the server upon connection

Do you have any idea as to how I can resolve this issue?


Solution

  • So the solution was to read the data into a byte[] and then convert using a the string constructor to a string as such:

    ServerSocket ss = new ServerSocket(5000);
    
    Socket s = ss.accept();
    
    DataInputStream in = new DataInputStream(s.getInputStream());
    
    byte[] b = new byte[1024];
    in.read(b);
    
    String tmp = new String(b, "UTF8");
    System.out.println(b);
    System.out.println(tmp);
    

    I have no idea why, but it is the solution I arrived at. The client almost stayed the same although minor changes were made:

    QString s = "This is a test string that will be sent to the server upon connection\n";
    
    QTcpSocket *sock = new QTcpSocket;
    
    sock->connectToHost(QHostAddress::LocalHost, 5000, QIODevice::ReadWrite);
    
    sock->waitForConnected(5000);
    
    
    do {
            sock->write(s.toUtf8());
            sock->flush();
    
            sock->waitForBytesWritten();
    } while (sock->isValid() && sock->bytesToWrite() > 0);