Unsolved how to comunicate with not-qt procedure?
-
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 -
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. -
@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;
-
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.
-
@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 header00:69: i
00:20: ' '
00:68:00:61:00:76:00:65: have00: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 requst4 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?
-
@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/EndiannessAlso 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.
-
@mrjj very thanks
-
@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.