Important: Please read the Qt Code of Conduct -

QTcpServer send data to std C client

  • I'm in QT 4.8, linux ubuntu. This is my code for send a new Qstring "shipping" after a client request .... the client is writing in std C.

    @void dlogprocess::sendFortune()

    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()),clientConnection, SLOT(deleteLater()));
    QString request;
    if (clientConnection->waitForReadyRead(10000))
        QByteArray block;
            QDataStream out(&block, QIODevice::ReadWrite);
            out << clientConnection->readAll();
            request = block;
            qDebug() <<  "request: " << request;
    qDebug() <<  spedizione.toUtf8().data();
    qDebug() <<  sizeof(shipping);
    clientConnection->write(shipping.toUtf8().data());  <----shipping is a private Qstring


    the client.c code ......

    @#include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <stdlib.h>

    int main() {
    int sd;
    struct sockaddr_in server_addr;
    char buff[500];

    struct hostent *hp;
    hp = gethostbyname("");

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(51041);
    server_addr.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr)) -> s_addr;

    if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    printf("socket error\n");

    if(connect(sd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    printf("server error\n");

    send(sd, "hello", strlen("hello"), 0);

    recv(sd, buff, sizeof(buff), 0);
    printf("Your value is: %s\n", buff);

    I have try more way to obtain request == "hello" but without success ..... Instead I get the QString "shipping" correctly and without any kind of error.

    How I can obtain my "hello" word in my Qt code??

    I apreciate any suggest.

  • Qt Champions 2017

    You are sending the 'hello' from client.So this data will be received at Server. So you should print the received string at server side.


    @QString str = clientConnection->readAll();
    qDebug() << "receive string ="<<str@

  • yes I try this ....... but obtain only a "" string ..... obviusly I try to insert \0 at the end of my "hello\0" messsge top .......

  • hello,

    i had similar problem using QTcpsocket : responses were not accurate and randomly false.

    try using socket->readLine() instead of readAll() like

    @while (bytesAvailable) {

  • thanks ...... I didn't try this code yet ...... but I have already try it in the past, ..... other project ...... I not remember it ...... very thanks for refresh my memory ..... tomorrow I can post the result.

  • The same result .... empty string ..... If run my server.c in those pc, obtain the right process .... and a correct string "hello" .... I try to use wireshark .... I'm no expert but in wiresherk you see my client.c that sends 72 bytes of request to port 51040, the port 51040 is seen that takes 72 bytes, then you see that the port responds with a message of xxx bytes, the message is received in full by my client.c ....

    The point is that in wireshark I do not see anything abnormal .... I do not understand why QT does not take the string "hello" sent by client.c

  • hi,

    for the record, here what i do in my app to read a tcp response :

    @ QString resp;
    while(socket->bytesAvailable() > 0)

    Also, can you provide more code ? how do you connect ? do you use signal/slots for reading ? connecting ?

Log in to reply