#include #include #include #include int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpServer tcpServer; if (!tcpServer.listen(QHostAddress::LocalHost, 55666)) { qDebug() << QString("Unable to start the server: %1.") .arg(tcpServer.errorString()); return 0; } QObject::connect(&tcpServer, &QTcpServer::newConnection, &tcpServer, [&](){ auto tcpServerConnection = tcpServer.nextPendingConnection(); tcpServerConnection->setReadBufferSize(65536); if (!tcpServerConnection) { qDebug() << "Error: got invalid pending connection!"; return; } QObject::connect(tcpServerConnection, &QIODevice::readyRead, tcpServerConnection, [tcpServerConnection](){ qDebug() << "Ready read = " << tcpServerConnection->bytesAvailable(); }); QObject::connect(tcpServerConnection, &QAbstractSocket::errorOccurred, tcpServerConnection, [](QAbstractSocket::SocketError error){ qDebug() << "Error = " << error; }); QObject::connect(tcpServerConnection, &QTcpSocket::disconnected, tcpServerConnection, &QTcpSocket::deleteLater); }); qDebug() << "Server port: " << tcpServer.serverPort(); return a.exec(); }