검색결과 리스트
QTcpSocket에 해당되는 글 1건
- 2017.12.19 QTcpSocket example
1 include header file
> #include <QTcpSocket>
#include <qabstractsocket.h>
2. declare a variable
QTcpSocket *tcpClient = Q_NULLPTR;
3 connect slot
connect(tcpClient, SIGNAL(connected()), this, SLOT(connectedToDM()));//check connected server
connect(tcpClient, SIGNAL(readyRead()), this, SLOT(ReceiveServerData())); //receive data from server
connect(tcpClient, SIGNAL(disconnected()), this, SLOT(disconnectedfromDM())); //receive signal when disconnected
connect(tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));//// check socket error from server
4. try to connect to server
tcpClient->connectToHost(m_dm_ip, 8000);
5. try to disconnect from server
tcpClient->disconnectFromHost();
6. send message to server
qint64 result = tcpClient->write(data.constData());
7. receive data from server
void MainWindow::ReceiveServerData()
{
QByteArray servermsg = tcpClient->readAll();
QString msg = QString::fromUtf8(servermsg.constData());
}
8. control socket error
void MainWindow::displayError(QAbstractSocket::SocketError socketError)
{
Log();
if (socketError == QTcpSocket::RemoteHostClosedError)
return;
RECENT COMMENT