Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have followed the fortune client/server examples, you should have a QTcpServer (<code>Rfserver</code>) with a QThread subclass (<code>Rfdevice</code>, its instance variable is called <code>thread</code> in the following code) that contains a QTcpSocket (<code>listenSocket</code>).</p> <p>Having said that, in your server class, listen for incoming connections, my setup looks like this:</p> <pre><code>void Rfserver::incomingConnection(int socketDescriptor){ if(thread){ //if thread exists, there is probably still an open connection if(thread-&gt;listenSocket){//if thread exists and the listenSocket is filled, there is definately an open connection if(thread-&gt;listenSocket-&gt;state() == QAbstractSocket::UnconnectedState){ //but alas, it could just be in the unconnected state, if so kill it. this-&gt;disconnect(); thread-&gt;terminate(); thread=0; connected=false; }//otherwise, do nothing, because the software is happily connected to a device } } if(!thread){ //if no thread exists, we are by no means connected thread = new rfdevice(socketDescriptor, this); //set up a new thread //this first connection communicates the string from your socket to the server parent...use it if you want. connect( thread, SIGNAL(RemoteButton(QString)),this,SLOT(remoteButton(QString)),Qt::BlockingQueuedConnection); connect( thread, SIGNAL(error(QTcpSocket::SocketError)),this,SLOT(tcpError(QTcpSocket::SocketError)),Qt::AutoConnection); connect( thread, SIGNAL(finished()), this, SLOT(threadZero())); //I have a threadZero function that deletes all the data then schedules the socket for deletion. thread-&gt;start(); connected=true; QString *welcome = new QString("Enter your password:\r\n"); echoCommand(welcome); //this is a function you will implement that sends the welcome message to the pending device. } } </code></pre> <p>Okay, so now, when a device tries to connect to the server the device is presented with <code>"Enter your password:\r\n"</code>. Your device will respond to this with a password and username perhaps. But the Qt side of things would look like this:</p> <pre><code>/* FUNCTION:read this is a polling runloop that listens for data as long as the socket is connected or connecting. If a write is ever scheduled, it will be called from this runloop.. */ void Rfdevice::read(void){ while((listenSocket-&gt;state() == QAbstractSocket::ConnectedState) || (listenSocket-&gt;state() == QAbstractSocket::ConnectingState)){ //if there is data available to send write it to the socket if(dataToSend) this-&gt;write(); if(listenSocket-&gt;waitForReadyRead(50)) readBytes(); //wait for 50ms for data from the device //if there is ever data available to be read, read it. } } </code></pre> <p>Your device responds with a username/password in the format <code>username---password\r\n</code>. Then the socket does this:</p> <pre><code>/* FUNCTION:readBytes this is like a callback function because it only gets called when there is data available for read. It basically converts the data to a string. */ void Rfdevice::readBytes(void){ QByteArray newData; newData = listenSocket-&gt;readAll(); QString *recieved = new QString(newData); QStringList userAndPass = recieved.split("---");//this is your delimiter QString username = userAndPass.at(0); QString password = userAndPass.at(1); //NOW, check the username and password vs your SQL or wherever it's saved. } </code></pre> <p>The pseudo-code is pretty complete on the particulars. Hopefully you can put it all together! Let me know if you need more code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload