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. how to comunicate with not-qt procedure?
Forum Updated to NodeBB v4.3 + New Features

how to comunicate with not-qt procedure?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 2.0k Views 2 Watching
  • 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.
  • chaochaoC Offline
    chaochaoC Offline
    chaochao
    wrote on last edited by
    #1

    i write a demo for tcp communicate between qt and not-qt.

    qt:

       _tcpSocket  = _tcpserver->nextPendingConnection();
      //  _tcpserver ->close();                         
    
       QByteArray block;
       QDataStream out(&block,QIODevice::WriteOnly);
    
       out.setVersion(QDataStream::Qt_5_7);
       out<<tr("i have get your requst");
    
       _tcpSocket->write(block);
    
    

    not-qt:
    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(sock_fd < 0)
    {
    syslog(LOG_ERR, "%s:%d, create socket failed", FILE, LINE);
    exit(1);
    }
    if(connect(sock_fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr)) < 0)
    {
    syslog(LOG_ERR, "%s:%d, connect socket failed", FILE, LINE);
    exit(1);
    }
    //receive data
    recvlen = 0;
    retlen = 0;
    ptr = recvbuf;
    leftlen = RECV_BUF_SIZE -1;
    //do
    {
    retlen = recv(sock_fd, ptr, leftlen, 0) ;
    if(retlen < 0)
    {
    if(errno == EINTR)
    retlen = 0;
    else
    exit(1);
    }
    recvlen += retlen;
    leftlen -= retlen;
    ptr += retlen;
    }
    //while(recvlen && leftlen);
    printf("receive data is : %s", recvbuf);

    no-qt run on linux like this ./client;
    but recvbuf is null.
    how can i get the right data("i have get your requst")? thank for your help

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Maybe you should make a small ./testserver using native calls also , so you
      can check if your client code do work. When they can talk (and send),
      you can try with Qt as server.

      chaochaoC 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Maybe you should make a small ./testserver using native calls also , so you
        can check if your client code do work. When they can talk (and send),
        you can try with Qt as server.

        chaochaoC Offline
        chaochaoC Offline
        chaochao
        wrote on last edited by
        #3

        @mrjj i have tried this client procedure on linux. the server you mentioned is writed with unix network.```
        int sfd,cfd;
        struct sockaddr_in saddr,caddr;
        int socklen = sizeof( struct sockaddr_in);

        memset(&saddr,0,sizeof(struct sockaddr_in));
        memset(&caddr,0,sizeof(struct sockaddr_in));

        saddr.sin_family = AF_INET;
        saddr.sin_port = htons(4567);
        saddr.sin_addr.s_addr = htonl(INADDR_ANY);

        sfd = socket(AF_INET, SOCK_STREAM, 0);
        bind(sfd, (struct sockaddr *)&saddr, sizeof( struct sockaddr_in)) ;
        listen(sfd, 5)

        while(1)
        {
        unsigned char rbuffer[20]={0};
        unsigned char sbuffer[]="i have recived your request\n";

            cfd = accept(sfd, (struct sockaddr *)&caddr, &socklen); 
            send(cfd,sbuffer,strlen(sbuffer),0);
        
        	recv(cfd, rbuffer, 20, 0) ; 
        
        	printf("reply is %s\n",rbuffer);
        }
        

        return 0;

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Ok
          So you are 100% sure client can read from a server ?
          It should also work with Qt then.

          You are using a DataStream and it might include some header. Since you are not
          using a DataStream in other end , it might give something unexpected results.

          You could use a tcp sniffer and see what it actually writes.

          chaochaoC 1 Reply Last reply
          0
          • mrjjM mrjj

            Ok
            So you are 100% sure client can read from a server ?
            It should also work with Qt then.

            You are using a DataStream and it might include some header. Since you are not
            using a DataStream in other end , it might give something unexpected results.

            You could use a tcp sniffer and see what it actually writes.

            chaochaoC Offline
            chaochaoC Offline
            chaochao
            wrote on last edited by
            #5

            @mrjj thank you ,i did it by tcpdump.like you say,the is a data-header and the data has their own dormat.

            00:00:00:2c:
            0-3 header

            00:69: i
            00:20: ' '
            00:68:00:61:00:76:00:65: have

            00:20:' '
            00:67:00:65:00:74: get
            00:20:' '
            00:79:00:6f:00:75:00:72: your
            00:20:' '
            00:72:00:65:00:71:00:75:00:73:00:74 requst

            4 bytes header. and one character has been transfer by tow bytes.is there a more efficient way to resolve the qt datastream on no-qt precedure?

            mrjjM 1 Reply Last reply
            0
            • chaochaoC chaochao

              @mrjj thank you ,i did it by tcpdump.like you say,the is a data-header and the data has their own dormat.

              00:00:00:2c:
              0-3 header

              00:69: i
              00:20: ' '
              00:68:00:61:00:76:00:65: have

              00:20:' '
              00:67:00:65:00:74: get
              00:20:' '
              00:79:00:6f:00:75:00:72: your
              00:20:' '
              00:72:00:65:00:71:00:75:00:73:00:74 requst

              4 bytes header. and one character has been transfer by tow bytes.is there a more efficient way to resolve the qt datastream on no-qt precedure?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @chaochao
              Well I would just skip header
              once full data have been read.
              Its very fast. ptr += headerlength;

              Also please notice that the stream also controls byteorder so if you send between differnt
              platforms, there might be something more to handle.
              https://en.wikipedia.org/wiki/Endianness

              Also there is another rawwrite that writes the data unmodified.
              http://doc.qt.io/qt-5/qdatastream.html#writeRawData
              "Writes len bytes from s to the stream. Returns the number of bytes actually written, or -1 on error. The data is not encoded."

              Might be better for your use case.

              chaochaoC 1 Reply Last reply
              0
              • mrjjM mrjj

                @chaochao
                Well I would just skip header
                once full data have been read.
                Its very fast. ptr += headerlength;

                Also please notice that the stream also controls byteorder so if you send between differnt
                platforms, there might be something more to handle.
                https://en.wikipedia.org/wiki/Endianness

                Also there is another rawwrite that writes the data unmodified.
                http://doc.qt.io/qt-5/qdatastream.html#writeRawData
                "Writes len bytes from s to the stream. Returns the number of bytes actually written, or -1 on error. The data is not encoded."

                Might be better for your use case.

                chaochaoC Offline
                chaochaoC Offline
                chaochao
                wrote on last edited by
                #7

                @mrjj very thanks

                mrjjM 1 Reply Last reply
                1
                • chaochaoC chaochao

                  @mrjj very thanks

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @chaochao
                  Np. If it works sending as raw. Please notice you might get issues if you send from very
                  different platforms. ( the Endianness ) Might not be issue in your use case.

                  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