我在运行服务器程序时遇到运行时错误,尤其是在一个客户端也没有响应的情况下。我真的不明白为什么它也会影响服务器程序。我是Qt编程的初学者,我尽力在客户端和服务器程序中找到错误,但结果无济于事。这是我的中继服务器的代码。有谁可以帮我重新创建并检测出真正产生错误的是什么?我认为它与未闭合连接有关(例如,当关闭GPRS模块而没有关闭TCP连接或无响应程序时(例如,充当GPRS模块数据的接收器的其他客户端)。编码和解码过程确实有效。他们甚至可以通过中继服务器成功通信。但是我仍然想要摆脱真正的错误。我想从调试服务器程序开始。所以服务器程序接受连接并广播该消息到每个客户端连接(例如聊天服务器)
myrelayserver.h
#ifndef MYRELAYSERVER_H
#define MYRELAYSERVER_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
#include <QListIterator>
#include "mysocket.h"
#include <QObject>
#include <QTimer>
class MyRelayServer : public QObject
{
Q_OBJECT
public:
explicit MyRelayServer(QObject *parent = 0);
public slots:
void newConnection();
void disconnected(QTcpSocket* socket);
void broadcastMessage(QTcpSocket* socket);
private:
QTcpServer *mServer;
QList<QTcpSocket*> mList;
};
#endif // MYRELAYSERVER_H
myrelayserver.cpp
#include "myrelayserver.h"
MyRelayServer::MyRelayServer(QObject *parent) :
QObject(parent)
{
mServer = new QTcpServer(this);
connect(mServer,SIGNAL(newConnection()),this,SLOT(newConnection()));
if(!mServer->listen(QHostAddress::AnyIPv4,9998))
{
qDebug() << "Failed to open port.";
}
else
{
qDebug() << "Server Started";
}
}
void MyRelayServer::newConnection()
{
MySocket * socket = new MySocket(this);
socket->mSocket = mServer->nextPendingConnection();
socket->mSocket->write("hello client\r\n");
socket->mSocket->flush();
socket->mSocket->waitForBytesWritten(3000);
mList.append(socket->mSocket);
connect(socket->mSocket, SIGNAL(readyRead()), socket, SLOT(readyRead()));
connect(socket, SIGNAL(emitSocket(QTcpSocket*)), this, SLOT(broadcastMessage(QTcpSocket*)));
connect(socket->mSocket, SIGNAL(disconnected()), socket, SLOT(disconnected()));
connect(socket, SIGNAL(emit2Socket(QTcpSocket*)), this, SLOT(disconnected(QTcpSocket*)));
qDebug() << "\a\a\aNumber of Clients: " << mList.size();
}
void MyRelayServer::disconnected(QTcpSocket* socket){
QMutableListIterator<QTcpSocket*> i(mList);
QTcpSocket * ptr;
while(i.hasNext()){
ptr = i.next();
if (ptr == socket){
i.remove();
break;
}
}
qDebug() << "\a\a\aNumber of Clients: " << mList.size();
delete socket;
}
void MyRelayServer::broadcastMessage(QTcpSocket * socket)
{
QByteArray stream = socket->readAll();
QListIterator<QTcpSocket*> i(mList);
QTcpSocket * ptr;
while(i.hasNext()){
ptr = i.next();
if (ptr != socket){
ptr->write(stream);
ptr->flush();
}
}
}
mysocket.h
#ifndef MYSOCKET_H
#define MYSOCKET_H
#include <QObject>
#include <QTcpSocket>
class MySocket : public QObject
{
Q_OBJECT
public:
explicit MySocket(QObject *parent = 0);
QTcpSocket * mSocket;
signals:
void emitSocket(QTcpSocket * socket);
void emit2Socket(QTcpSocket * socket);
public slots:
void readyRead();
void disconnected();
};
#endif // MYSOCKET_H
mysocket.cpp
#include "mysocket.h"
MySocket::MySocket(QObject *parent) :
QObject(parent)
{
mSocket = new QTcpSocket(this);
}
void MySocket::readyRead()
{
emit emitSocket(mSocket);
}
void MySocket::disconnected()
{
emit emit2Socket(mSocket);
}
main.cpp
#include <QCoreApplication>
#include "myrelayserver.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "**********************************************\Relay Server\n\n**********************************************";
MyRelayServer server;
return a.exec();
}