我创建了一个使用 UDP 协议的服务器和客户端类。在我的应用程序中,我实例化了 4 个服务器对象和 4 个具有不同端口的客户端对象。
问题是我同时从 4 个客户端向 4 个服务器发送数据,但是每个服务器只有在前一个服务器完成工作时才接收它的数据......
为什么会这样?
这是我的代码:
主要的:
std::thread thread;
client cl1(4235),cl2(4245),cl3(4255),cl4(4265);
void handler()
{
while(1){
cl1.SendData("HI1",4230);
cl2.SendData("HI2",4240);
cl3.SendData("HI3",4250);
cl4.SendData("HI4",4260);
std::this_thread::sleep_for(std::chrono::seconds(50));
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
server s1(4230);
server s2(4240);
server s3(4250);
server s4(4260);
thread = std::thread(handler);
return a.exec();
}
服务器类:
class server : public QObject
{
Q_OBJECT
public :
server(int port)
{
this->port = port;
udpSocket = new QUdpSocket();
udpSocket->bind(QHostAddress::LocalHost, port);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
};
public slots:
void readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
//processTheDatagram(datagram);
QDateTime dateTime;
dateTime = dateTime.currentDateTime();
qDebug() << QString::number(dateTime.time().hour()).rightJustified(2, '0') + ":" +//hour
QString::number(dateTime.time().minute()).rightJustified(2, '0') + ":" +//minute
QString::number(dateTime.time().second()).rightJustified(2, '0');//seconds
qDebug() << "\t\tdata received from:" << sender.toString() <<":"<<senderPort << " data:" << datagram;
std::this_thread::sleep_for(std::chrono::seconds(5));
qDebug() << QString::number(dateTime.time().hour()).rightJustified(2, '0') + ":" +//hour
QString::number(dateTime.time().minute()).rightJustified(2, '0') + ":" +//minute
QString::number(dateTime.time().second()).rightJustified(2, '0');//seconds
qDebug () << "Sending Response";
udpSocket->writeDatagram(("HI back from" + QString::number(port)).toUtf8(),sender,senderPort);
}
};
private:
QUdpSocket * udpSocket;
int port;
};
客户端类:
class client : public QObject
{
Q_OBJECT
public:
client(int port) {
this->port = port;
udpSocket = new QUdpSocket();
udpSocket->bind(QHostAddress::LocalHost, port);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
}
void SendData(QString Data,int serverport)
{
QDateTime dateTime;
dateTime = dateTime.currentDateTime();
udpSocket->writeDatagram(Data.toUtf8(),QHostAddress::LocalHost,serverport);
qDebug() << QString::number(dateTime.time().hour()).rightJustified(2, '0') + ":" +//hour
QString::number(dateTime.time().minute()).rightJustified(2, '0') + ":" +//minute
QString::number(dateTime.time().second()).rightJustified(2, '0')//sec
<< "\tclient : " <<port << " sending data to server:" << Data;
}
public slots:
void readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
//processTheDatagram(datagram);
QDateTime dateTime;
dateTime = dateTime.currentDateTime();
qDebug() << QString::number(dateTime.time().hour()).rightJustified(2, '0') + ":" +//hour
QString::number(dateTime.time().minute()).rightJustified(2, '0') + ":" +//minute
QString::number(dateTime.time().second()).rightJustified(2, '0');//seconds
qDebug() << "\t\tclient : " <<port << " data received from:" << sender.toString() <<":"<<senderPort << " data:" << datagram;
}
}
private:
QUdpSocket * udpSocket;
int port;
};
这是我的输出数据:
"18:50:07" client : 4235 sending data to server: "HI1"
"18:50:07" client : 4245 sending data to server: "HI2"
"18:50:07" client : 4255 sending data to server: "HI3"
"18:50:07" client : 4265 sending data to server: "HI4"
"18:50:07"
data received from: "127.0.0.1" : 4235 data: "HI1"
"18:50:07"
Sending Response
"18:50:12"
data received from: "127.0.0.1" : 4245 data: "HI2"
"18:50:12"
Sending Response
"18:50:17"
data received from: "127.0.0.1" : 4255 data: "HI3"
"18:50:17"
Sending Response
"18:50:22"
data received from: "127.0.0.1" : 4265 data: "HI4"
"18:50:22"
Sending Response