質問

i've just made an own implementation of QTcpServer and overloaded the incomingConnection function.

void Server::incomingConnection(int handle) //Server inherits from QTcpServer
{
    qDebug()<<"Server::incomingConnection"<<handle;
    Thread *thread = new Thread(handle,this);
    connect(thread,SIGNAL(finished()),this,SLOT(deleteLater()));
    thread->start();
}

In the Thread i do the following things:

void Thread::run()
{
    qDebug() << m_socketDescriptor << "Starting Thread";
    m_socket = new QTcpSocket();
    if(!m_socket->setSocketDescriptor(m_socketDescriptor))
        return;

    connect(m_socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    connect(m_socket,SIGNAL(disconnected()),this,SLOT(disconnected()));

    qDebug() << m_socketDescriptor << "Client connected";

    exec();
}

Now i've a multithreaded server.

But how can i get access to the connected clients and send them data via. a gui?

Thank you in advance!

Regards

役に立ちましたか?

解決

You need to use some of the 'QIODevice' functions, such as write or << to send data to the client that is on the other end of the QTCPSocket.

So if you are serving webpages to a browser client, then you first listen (or use read commands) to their request, and then send over the appropriate response following the protocol that you are using.

So I would first set up this server on port 80 on your computer, and then open a browser to http://localhost . Then use qDebug to print out the requests from your browser.

void Thread::readyRead()
{
    qDebug() << Q_FUNC_INFO;
    qDebug() << m_socket.readAll();
}

After you have that working, decide how you are going to parse the request, and then how you are going to respond, or what data you want to serve.

Also, be sure to check out the TCP examples in the See also for QTCPSocket.

Hope that helps.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top