初学QT的TCP编程,有几点不明白。。
我是完全复制书上的程序的,虽然能运行成功了,但是代码中有几个地方不明白。。求求解。。
我不知道该怎么表达我想问的问题,只好把所有的程序粘贴出来,然后加些注释。。。
程序如下:
#ifndef SERVER_H
#define SERVER_H
#include <QtNetwork>
#include "tcpclientsocket.h"
class Server : public QTcpServer
{
Q_OBJECT
public:
Server(QObject *parent = 0,int port=0);
QList<TcpClientSocket*> tcpClientSocketList;
//这个尖括号代表什么意思??
signals:
void updateServer(QString,int);
public slots:
void updateClients(QString,int);
void slotDisconnected(int);
protected:
void incomingConnection(int socketDescriptor);
};
#endif
#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 < tcpClientSocketList.count();i++)
{
QTcpSocket *item = tcpClientSocketList.at(i);
if(item->write(msg.toLatin1(),length) != length)//这句有什么用,我觉得没啥用好像多余的
{ //直接item->write(msg.toLatin1(),length)不就行了吗??
continue;
}
}
}
void Server::slotDisconnected(int descriptor)
{
for(int i = 0;i < tcpClientSocketList.count();i++)
{
QTcpSocket *item = tcpClientSocketList.at(i);
if(item->socketDescriptor() == descriptor)
{
tcpClientSocketList.removeAt(i);
continue;
}
}
}
#include "tcpserver.h"
TcpClientSocket::TcpClientSocket(QObject *parent)
{
connect(this,SIGNAL(readyRead()) ,this,SLOT(dataReceived()));
connect(this,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
}
TcpClientSocket::~TcpClientSocket()
{
}
void TcpClientSocket::dataReceived()
{
while(bytesAvailable() > 0)
{
char buf[1024];
int length = bytesAvailable();
read(buf,length);
QString msg = buf;//这两条有什么用??都没有执行的函数的?是不是只是提醒的作用而已??
emit updateClients(msg,length);
}
}
void TcpClientSocket::slotDisconnected()
{
emit disconnected(this->socketDescriptor());//这条同上~~不明白~
}
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QtGui>
#include <QtNetWork>
#include "server.h"
class TcpServer : public QDialog
{
Q_OBJECT
public:
TcpServer(QWidget *parent = 0,Qt::WindowFlags f = 0);
~TcpServer();
public:
QListWidget *ListWidgetContent;
QLabel *LabelPort;
QLineEdit *LineEditPort;
QPushButton *pushButtonCreate;
int port;
Server *server;
public slots:
void slotCreateServer();
void updateServer(QString,int);
};
#endif // TCPSERVER_H
#include "tcpserver.h"
TcpServer::TcpServer(QWidget *parent,Qt::WindowFlags f)
: QDialog(parent,f)
{
setWindowTitle(tr("TCP Server"));
QVBoxLayout *vbMain = new QVBoxLayout( this );
ListWidgetContent = new QListWidget( this);
vbMain->addWidget( ListWidgetContent );
QHBoxLayout *hb = new QHBoxLayout( );
LabelPort = new QLabel( this );
LabelPort->setText(tr("Port:"));
hb->addWidget( LabelPort );
LineEditPort = new QLineEdit(this);
hb->addWidget( LineEditPort );
vbMain->addLayout(hb);
pushButtonCreate = new QPushButton( this);
pushButtonCreate->setText( tr( "Create" ) );
vbMain->addWidget( pushButtonCreate );
connect(pushButtonCreate,SIGNAL(clicked()),this,SLOT(slotCreateServer()));
port = 8010;
LineEditPort->setText(QString::number(port));
}
TcpServer::~TcpServer()
{
}
void TcpServer::slotCreateServer()
{
server = new Server(this,port);
connect(server,SIGNAL(updateServer(QString,int)),SLOT(updateServer(QString,int)));
//上面信号为什么和槽的都是一样的??这样代表什么意思??
pushButtonCreate->setEnabled(false);
}
void TcpServer::updateServer(QString msg, int length)
{
ListWidgetContent->addItem(msg.left(length));
}