《Qt开发》TCP编程之客户端程序

《Qt开发》TCP编程之客户端程序_第1张图片

图1 显示界面

 《Qt开发》TCP编程之客户端程序_第2张图片

图2 网络助手辅助测试

 

1. 新建项目时要选择Network模块

2. 头文件中加入#include

声明QTcpSocket对象 QTcpSocket *tcpSocket;

声明如下函数和槽函数

void setQss();

public slots:

void currState();

void on_pBtnConn_clicked();

void on_pBtnSend_clicked();

void on_pBtnSel_clicked();

void on_pBtnSendTxt_clicked();

void recvMessage();  //接收数据

3. 在源文件构造函数中添加如下代码

setQss();

ui.lineEditIP->setText("192.168.57.138");

ui.lineEditPort->setText("7588");

tcpSocket = new QTcpSocket(this);

connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(recvMessage()));

connect(tcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(currState()));

//源文件中其他函数

void Client::setQss()

{

//读取样式表设置样式

QString strStyleSheet = "";

QFile file("Config/StyleSheet.qss");

file.open(QFile::ReadOnly);

if (file.isOpen())

{

strStyleSheet = QLatin1String(file.readAll());

}

this->setStyleSheet(strStyleSheet);

//遍历同类控件,设置样式

//QString str1 = "QWidget{background-color:qlineargradient(spread:pad, x1:0,y1:0,x2:0,y2:1, stop:0.1 rgb(107,128,169),stop:1 rgb(1,71,133))}";

//QString str2 = "background-color: rgb(236,220,171)";

//QString str3 = "background-color:rgb(255,255,255)";

//this->setStyleSheet(str1);

//ui.plainTextEdit->setStyleSheet(str3);

//QList allpBtns = this->findChildren();

//for (int i = 0; i < allpBtns.size(); i++)

//{

// allpBtns.at(i)->setStyleSheet(str2);

//}

//QList allLabels = this->findChildren();

//for (int i = 0; i < allLabels.size(); i++)

//{

// allLabels.at(i)->setStyleSheet(str2);

//}

//QList allLineEdit = this->findChildren();

//for (int i = 0; i < allLineEdit.size(); i++)

//{

// allLineEdit.at(i)->setStyleSheet(str3);

//}

}

//连接服务器槽函数

void Client::on_pBtnConn_clicked()

{

tcpSocket->abort();  //取消已有连接

tcpSocket->connectToHost(ui.lineEditIP->text(), ui.lineEditPort->text().toInt());

}

//连接状态发生变化时对应的槽函数

void Client::currState()

{

if (tcpSocket->state() == QAbstractSocket::UnconnectedState)

{

ui.plainTextEdit->setPlainText("The socket is not connected.");

}

if (tcpSocket->state() == QAbstractSocket::HostLookupState)

{

ui.plainTextEdit->setPlainText("The socket is performing a host name lookup.");

}

if (tcpSocket->state() == QAbstractSocket::ConnectingState)

{

ui.plainTextEdit->setPlainText("The socket has started establishing a connection.");

}

if (tcpSocket->state() == QAbstractSocket::ConnectedState)

{

ui.plainTextEdit->setPlainText("A connection is established.");

}

if (tcpSocket->state() == QAbstractSocket::ClosingState)

{

ui.plainTextEdit->setPlainText("The socket is about to close (data may still be waiting to be written).");

}

}

//客户端向服务器发送缓冲区的内容

void Client::on_pBtnSend_clicked()

{

QString sendMsg = ui.plainTextEdit->toPlainText();

tcpSocket->write(sendMsg.toUtf8());

}

//选择要发送的txt文件

void Client::on_pBtnSel_clicked()

{

QString filePath = QFileDialog::getOpenFileName(this,"选择文件", "", NULL);

ui.lineEditPath->setText(filePath);

}

//客户端向服务器发送txt文件

void Client::on_pBtnSendTxt_clicked()

{

QString filePath = ui.lineEditPath->text();

QFile file(filePath);

if (!file.open(QIODevice::ReadOnly | QIODevice::Text))

return;

int lineNum = 0;

while (!file.atEnd())

{

lineNum++;

QByteArray lineBytes = file.readLine();

tcpSocket->write(lineBytes);

}

file.close();

ui.plainTextEdit->setPlainText(QString::number(lineNum));

}

//接收服务器发来的信息

void Client::recvMessage()

{

QDataStream in(tcpSocket);

in.setVersion(QDataStream::Qt_4_6);

QByteArray recvBytes = tcpSocket->readAll();

ui.plainTextEdit->appendPlainText(recvBytes);

}

你可能感兴趣的:(Qt,qt,tcp/ip,开发语言)