Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpServer send data to std C client

QTcpServer send data to std C client

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by
    #1

    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
    clientConnection->disconnectFromHost();
    

    }@

    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("173.254.8.211");

    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);
    close(sd);
    return;
    }@

    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.

    bkt

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      try

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • gfxxG Offline
        gfxxG Offline
        gfxx
        wrote on last edited by
        #3

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

        bkt

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Marty
          wrote on last edited by
          #4

          hello,

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

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

          @while (bytesAvailable) {
          result.append(socket->readLine());
          }@

          1 Reply Last reply
          0
          • gfxxG Offline
            gfxxG Offline
            gfxx
            wrote on last edited by
            #5

            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.

            bkt

            1 Reply Last reply
            0
            • gfxxG Offline
              gfxxG Offline
              gfxx
              wrote on last edited by
              #6

              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

              bkt

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Marty
                wrote on last edited by
                #7

                hi,

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

                @ QString resp;
                if(socket->waitForReadyRead((2000)))
                {
                while(socket->bytesAvailable() > 0)
                {
                resp.append(socket->readLine());
                }
                }@

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved