Here are the problems int you code
Client
, ui(new Ui::GuiClient()) //Add this
{
ui->setupUi(this);
}
GuiClient::GuiClient(QWidget *parent) : QMainWindow(parent)
, ui(new Ui::GuiClient()) //Add this
{
ui->setupUi(this);
}
To copy to clipboard, switch view to plain text mode
Server
{
...
private:
...
};
, 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");
}
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");
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks