Home > Back-end >  QT TCP communications, for the first time send one character at a time, no problem, but send the sec
QT TCP communications, for the first time send one character at a time, no problem, but send the sec

Time:10-02

Void Server: : sendCharacterA ()
{
C="A";
//c=wsFrame [1085].
//const qint16 chType=1;
QDataStream out (& amp; ChA, QIODevice: : WriteOnly);
Out. SetVersion (QDataStream: : Qt_4_7);
The out & lt; <(qint16) 0;

The out & lt; QDebug () & lt; //qDebug () & lt; Out. Device () - & gt; Seek (0);
The out & lt; <(qint16) (chA. The size () - sizeof (qint16));
TcpServerConnection - & gt; Write (chA);

//recvCharacterA ();
//emit characterReturnA (ch)
ChA. Resize (0);
//connect (tcpServerConnection, SIGNAL (readyRead ()), and this, SLOT (recvCharacterA ()));
If (tcpServerConnection - & gt; WaitForReadyRead ()==false)
{
return;
}
The else
{
RecvCharacterA ();
}
}
Void Server: : recvCharacterA ()
{
QDataStream (in tcpServerConnection);
In setVersion (QDataStream: : Qt_4_7);
If (blockSize==0)
{
//UI - & gt; ServerStatusLabel - & gt; SetText (tr (" receiving character A function performs "));
If (tcpServerConnection - & gt; BytesAvailable () & lt; Sizeof (int) (qint16)) return;

In & gt;> BlockSize.

}
//qDebug () & lt; If (tcpServerConnection - & gt; BytesAvailable () & lt; BlockSize) return;

In & gt;> RecvChA;
//qDebug () & lt; QDebug () & lt; QDebug () & lt; The UI - & gt; ServerStatusLabel - & gt; SetText (recvChA);
If (recvChA!=c)
{
SendCharacterA ();
}
The else
{
QDebug () & lt; SendCharacterB ();
}

Void Server: : sendCharacterB ()
{
B="b".
QDataStream out (& amp; ChB, QIODevice: : WriteOnly);
Out. SetVersion (QDataStream: : Qt_4_7);
The out & lt; <(qint16) 0 & lt; //qDebug () & lt; Out. Device () - & gt; Seek (0);
The out & lt; <(qint16) (chB. The size () - sizeof (qint16));
QDebug () & lt;

TcpServerConnection - & gt; Write (chB);


//chB. Resize (0);
//recvCharacterB ();
If (tcpServerConnection - & gt; WaitForReadyRead ()==false)
{
return;
}
The else
{
RecvCharacterB ();
}
QDebug () & lt; }
Void Server: : recvCharacterB ()
{
//qDebug () & lt; //QByteArray resultB=sendCharacterB ();
QDataStream (in tcpServerConnection);
In setVersion (QDataStream: : Qt_4_7);
If (BblockSize==0)
{
If (tcpServerConnection - & gt; BytesAvailable () & lt; Sizeof (int) (qint16)) return;
In & gt;> BblockSize;
}
If (tcpServerConnection - & gt; BytesAvailable () & lt; BblockSize) return;

In & gt;> RecvChB;
QDebug () & lt; QDebug () & lt;
If (recvChB!=b)//echo characters and send characters are consistent
{
SendCharacterB ();
}
The else
{
The UI - & gt; ServerStatusLabel - & gt; SetText (tr (" shake hands can send frame "));
SendWsFrame ();
}
//recvChB=0;

}
  • Related