头函数
#ifndef SERVER_H
#define SERVER_H
#include
#include
#include "tcpclientsocket.h"
class Server:public QTcpServer
{
public:
Server(QObject parent=0,int port =0);
QList<TcpClientSocket> tcpClientSocketList;
public slots:
void updateClients(QString,int);
void slotDisconnected(int);
protected:
void incomingConnection(int socketDescriptor);
signals:
void updateServer(QString,int);
};
#endif SERVER_H
函数
#include "server.h"
Server::Server(QObject *parent,int port):QTcpServer(parent)
{
listen(QHostAddress::Any,port);
}
void Server::incomingConnection(int socketDescriptor)
{
TcpClientSocket *tcpClientSocket = new TcpClientSocket(this);
connect(tcpClientSocket,SIGNAL(updateClients(QString,int)),this,SLOT(updateClients(QString,int)));
connect(tcpClientSocket,SIGNAL(disconnected(int)),this,SLOT(slotDisconnected(int)));
tcpClientSocket->setSocketDescriptor(socketDescriptor);
tcpClientSocketList.append(tcpClientSocket);
}
void Server::updateClients(QString msg, int length)
{
emit updateServer(msg,length);
for(int i=0;i
{
QTcpSocket *item = tcpClientSocketList.at(i);
if(item->write(msg.toLatin1(),length)!=length)
{
continue;
}
}
}
void Server::slotDisconnected(int descriptor)
{
for(int i=0;i
{
QTcpSocket *item=tcpClientSocketList.at(i);
if(item->socketDescriptor()==descriptor)
{
tcpClientSocketList.removeAt(i);
return;
}
}
return;
}
其中出了个问题error: LNK2019: 无法解析的外部符号 "public: void __cdecl Server::updateServer(class QString,int)" (?updateServer@Server@@QEAAXVQString@@H@Z),该符号在函数 "public: void __cdecl Server::updateClients(class QString,int)" (?updateClients@Server@@QEAAXVQString@@H@Z) 中被引用
造着书模仿的,不知道为什么错