PDA

View Full Version : non blocking QTcpSocket



SIFE
29th May 2011, 02:26
I made simple chat based in client-server model, I am able to send messages from client to server but not in reverse. I tried to implement my chat in non blocking mode, but it seems not work for me.
6490

Santosh Reddy
29th May 2011, 03:52
Here are the problems int you code

Client


GuiClient::GuiClient(QWidget *parent) : QMainWindow(parent)
, ui(new Ui::GuiClient()) //Add this
{

ui->setupUi(this);
}


Server


class GuiServer : public QMainWindow
{
...
private:
...
QTcpSocket *client; //Add this
};

GuiServer::GuiServer(QWidget *parent) : QMainWindow(parent)
, ui(new Ui::GuiServer())
, client(0)
{

ui->setupUi(this);
}

void GuiServer::readMsg()
{
// TcpSocket* client = (QTcpSocket*)sender(); //This will not work, and don't use this in your code, until you completely understand how signals, slots work.

if(client) //user stored member variable, should be 0 if no client is connected
{
... //Add all existing code here
}
}

void GuiServer::incomingConnection()
{
if(client) //user stored member variable, should be 0 if no client is connected
delete client;
client = server->nextPendingConnection();
...
}

void GuiServer::sendMsg()
{
// TcpSocket* client = (QTcpSocket*)sender(); //This will not work, and don't use this in your code, until you completely understand how signals, slots work.

if(client) //user stored member variable, should be 0 if no client is connected
{
... //Add all existing code here
}
}

void GuiServer::disconnected()
{
client = 0; //no client present
qDebug("A client disconnected");
}



Also this kind of server will be able to talk to only one client at a time. Multiple client handling is not implement, if tired to connect, server may crash

SIFE
1st June 2011, 02:06
Just two newbie question's:

GuiServer::GuiServer(QWidget *parent) : QMainWindow(parent)
, ui(new Ui::GuiServer())
, client(0)
Are you here just instancing client object or it is some thing else.

TcpSocket* client = (QTcpSocket*)sender();
What is this exactly mean, because I just copy it from other source code and no look after search to found what is mean.