Read data from mirth connect - tcp server qt creator -


im stuck problem, need read message sent mirth connect (normal text or hl7) recieve in software , write message in data base, tcpserver code:

myserver::myserver(qobject *parent): qobject(parent) { server = new qtcpserver(this);  connect(server,signal(newconnection()),this,slot(newconnection()));  if(!server->listen(qhostaddress::any,1234)) {     qdebug() << "server not start!"; } else {    qdebug() << "server started"; } }   void myserver::newconnection() { qtcpsocket *socket = server->nextpendingconnection(); qdebug() << "client agree"; qstring data = qtextcodec::codecformib(1015)->tounicode(socket->readall());  qdebug() << socket->readall(); socket->flush(); socket->waitforbyteswritten(3000); socket->close();  } 

i think problem socket->readall(); line take bytes sent client , write in console , show nothing, can me pls?

you should think network connection process has various phases: connecting, payload transfer, closing. network information transferred using packets of limited size. time interval between packets large enough in scale of cpu clock. possible have significant timeouts. so, use cpu other tasks between packets, networking functions triggered events.

the signal qtcpserver::newconnection() means tcp connection established (only few packets exchanged start). so, nothing can read qtcpsocket. beginning.

from point should work pointer qtcpsocket. can handled either

  • asynchronously (handle , exit handler allow dispatching of other evens in thread mandatory in case of gui thread keep ui in responsive state) or
  • synchronously (here thread in sleep state between packets).

you can find useful examples in documentation of qabstractsocket base class of qtcpsocket.

the simpler blocking connection:

int numread = 0, numreadtotal = 0; char buffer[50];  forever {     numread  = socket->read(buffer, 50);      // whatever array      numreadtotal += numread;     if (numread == 0 && !socket->waitforreadyread())         break; } 

there various signals triggered specific events. example qabstractsocket::readyread() triggered when new data available. so, asynchronous usage:

// slot connected qabstractsocket::readyread() void myserver::readyreadslot() {     qbytearray data = socket->readall();     .... } 

there signals qabstractsocket::disconnected(), qabstractsocket::error(). when socket disconnected useful delete delayed socket->deletelater() called disconnected socket handler.

it needed careful asynchrous solution, since possible have simultaneously many connections. so, many different instances of qtcpsocket should managed.


Comments