Portál AbcLinuxu, 9. května 2025 21:43
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(m_descriptor)) {
emit error(tcpSocket.error());
return;
}
QString in;
QTextStream ios(&tcpSocket);
ios << "Welcome" << endl;
ios.flush();
forever {
in = ios.readAll();
if(!in.isEmpty())
qDebug() << in;
}
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
Nevie niekto čo robím zle? Za každú radu vopred ďakujem.
Řešení dotazu:
connect(&tcpSocket, SIGNAL(readyRead()), this, SLOT(read()));
void ServerThread::read()
{
qDebug() << "Ready" << endl;
}
Oprav si následující metodu z serverthread.cpp
:
void ServerThread::run() { QTcpSocket tcpSocket; if (!tcpSocket.setSocketDescriptor(m_descriptor)) { emit error(tcpSocket.error()); return; } connect(&tcpSocket, SIGNAL(readyRead()), this, SLOT(read())); tcpSocket.write("Testuj\n"); tcpSocket.flush(); tcpSocket.waitForDisconnected(); qDebug() << "Close" << endl; }
Nyní klient dostane text Testuj
a server bude čekat než se odpojí tcpSocket.waitForDisconnected();
, pokud klient pošle serveru data, tak se zavolá fce read()
a na obrazovku se vypíše Ready
.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.