Important: Please read the Qt Code of Conduct -

how to comunicate with not-qt procedure?

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


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

    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(sock_fd < 0)
    syslog(LOG_ERR, "%s:%d, create socket failed", FILE, LINE);
    if(connect(sock_fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr)) < 0)
    syslog(LOG_ERR, "%s:%d, connect socket failed", FILE, LINE);
    //receive data
    recvlen = 0;
    retlen = 0;
    ptr = recvbuf;
    leftlen = RECV_BUF_SIZE -1;
    retlen = recv(sock_fd, ptr, leftlen, 0) ;
    if(retlen < 0)
    if(errno == EINTR)
    retlen = 0;
    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

  • Lifetime Qt Champion

    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.

  • @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)

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

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

    return 0;

  • Lifetime Qt Champion

    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.

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

    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 there a more efficient way to resolve the qt datastream on no-qt precedure?

  • Lifetime Qt Champion

    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.

    Also there is another rawwrite that writes the data unmodified.
    "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.

  • @mrjj very thanks

  • Lifetime Qt Champion

    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.

Log in to reply