I have two projects, one for the server and the other for the client:
Server:
int main(int argc, char *argv[])
{
Server s;
s.start(9999);
return a.exec();
}
{
Q_OBJECT
public:
explicit Server
(QObject *parent
= 0);
signals:
public slots:
void start(int port);
void newConnection();
void sendMsg();
private:
QList<QTcpSocket*> m_clientList;
};
{
QObject::connect(&m_server,
SIGNAL(newConnection
()),
this,
SLOT(newConnection
()));
QObject::connect(&m_timer,
SIGNAL(timeout
()),
this,
SLOT(sendMsg
()));
m_timer.setInterval(1000);
m_timer.start();
}
void Server::start(int port)
{
}
void Server::newConnection()
{
qDebug() << "NEW CONNECTION!";
m_clientList.append(m_server.nextPendingConnection());
qDebug() << m_clientList.count();
}
void Server::sendMsg()
{
{
socket->write("test");
qDebug() << "send: " << socket->peerAddress();
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Server s;
s.start(9999);
return a.exec();
}
class Server : public QObject
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
signals:
public slots:
void start(int port);
void newConnection();
void sendMsg();
private:
QTcpServer m_server;
QList<QTcpSocket*> m_clientList;
QTimer m_timer;
};
Server::Server(QObject *parent) :
QObject(parent)
{
QObject::connect(&m_server,SIGNAL(newConnection()),this,SLOT(newConnection()));
QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(sendMsg()));
m_timer.setInterval(1000);
m_timer.start();
}
void Server::start(int port)
{
m_server.listen(QHostAddress::Any,port);
}
void Server::newConnection()
{
qDebug() << "NEW CONNECTION!";
m_clientList.append(m_server.nextPendingConnection());
qDebug() << m_clientList.count();
}
void Server::sendMsg()
{
foreach(QTcpSocket* socket, m_clientList)
{
socket->write("test");
qDebug() << "send: " << socket->peerAddress();
}
}
To copy to clipboard, switch view to plain text mode
Client:
int main(int argc, char *argv[])
{
Client c;
c.connectToHost("localhost",9999);
return a.exec();
}
{
Q_OBJECT
public:
explicit Client
(QObject *parent
= 0);
signals:
public slots:
void connectToHost
(QString hostName,
int port
);
void read();
private:
};
{
QObject::connect(&m_client,
SIGNAL(readyRead
()),
this,
SLOT(read
()));
}
void Client
::connectToHost(QString hostName,
int port
) {
m_client.connectToHost(hostName,port);
}
void Client::read()
{
in >> msg;
qDebug() << msg;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Client c;
c.connectToHost("localhost",9999);
return a.exec();
}
class Client : public QObject
{
Q_OBJECT
public:
explicit Client(QObject *parent = 0);
signals:
public slots:
void connectToHost(QString hostName, int port);
void read();
private:
QTcpSocket m_client;
};
Client::Client(QObject *parent) :
QObject(parent)
{
QObject::connect(&m_client,SIGNAL(readyRead()),this,SLOT(read()));
}
void Client::connectToHost(QString hostName, int port)
{
m_client.connectToHost(hostName,port);
}
void Client::read()
{
QDataStream in(&m_client);
in.setVersion(QDataStream::Qt_4_0);
QString msg;
in >> msg;
qDebug() << msg;
}
To copy to clipboard, switch view to plain text mode
It should print 'test' on the client, but it prints an empty string. What am I missing?
Bookmarks