#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtNetwork/QLocalServer>
#include <QtNetwork/QLocalSocket>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QLocalServer server(&a);

#ifdef Q_WS_X11
    QDir tmp("/tmp");
    if (tmp.exists("test_localsocket"))
        tmp.remove("test_localsocket");
#endif

    qDebug("QLocalServer::listen()");
    if (server.listen("test_localsocket"))
    {
        qDebug("QLocalServer::waitForNewConnection()");
        if (server.waitForNewConnection(-1))
        {
            qDebug("QLocalServer::nextPendingConnection()");
            QLocalSocket *client = server.nextPendingConnection();

            if (client)
            {
                qDebug("QLocalSocket::write()");
                qint64 written = client->write("test data");
                qDebug("written %u bytes.", (int)written);

                qDebug("QLocalSocket::waitForBytesWritten()");
                client->waitForBytesWritten(-1);

                qDebug("QLocalSocket::close()");
                client->close();

                qDebug("delete QLocalSocket");
                delete client;

                return 0;
            }
        }
    }

    return -1;
}
