qt版的TCP客户端代码
时间: 2024-05-05 07:22:09 浏览: 145
以下是一个基本的QT TCP客户端代码示例:
```cpp
#include <QtNetwork>
#include <QtWidgets>
class TcpClient : public QDialog
{
Q_OBJECT
public:
TcpClient(QWidget *parent = nullptr);
private slots:
void start();
void stop();
void readData();
void displayError(QAbstractSocket::SocketError socketError);
private:
QLabel *hostLabel;
QLineEdit *hostLineEdit;
QLabel *portLabel;
QLineEdit *portLineEdit;
QLabel *statusLabel;
QPushButton *startButton;
QPushButton *stopButton;
QTcpSocket *tcpSocket;
QTextEdit *textEdit;
};
TcpClient::TcpClient(QWidget *parent)
: QDialog(parent), tcpSocket(new QTcpSocket(this))
{
hostLabel = new QLabel(tr("Host name:"));
hostLineEdit = new QLineEdit("127.0.0.1");
portLabel = new QLabel(tr("Port number:"));
portLineEdit = new QLineEdit("8080");
statusLabel = new QLabel(tr("Status: Not connected"));
startButton = new QPushButton(tr("Connect"));
stopButton = new QPushButton(tr("Disconnect"));
textEdit = new QTextEdit;
textEdit->setReadOnly(true);
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->addWidget(hostLabel, 0, 0);
mainLayout->addWidget(hostLineEdit, 0, 1);
mainLayout->addWidget(portLabel, 1, 0);
mainLayout->addWidget(portLineEdit, 1, 1);
mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
mainLayout->addWidget(startButton, 3, 0);
mainLayout->addWidget(stopButton, 3, 1);
mainLayout->addWidget(textEdit, 4, 0, 1, 2);
stopButton->setEnabled(false);
connect(startButton, SIGNAL(clicked()), this, SLOT(start()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
}
void TcpClient::start()
{
QString host = hostLineEdit->text();
int port = portLineEdit->text().toInt();
tcpSocket->connectToHost(host, port);
if (tcpSocket->waitForConnected()) {
startButton->setEnabled(false);
stopButton->setEnabled(true);
statusLabel->setText(tr("Status: Connected"));
} else {
QMessageBox::critical(this, tr("Error"), tcpSocket->errorString());
}
}
void TcpClient::stop()
{
tcpSocket->disconnectFromHost();
startButton->setEnabled(true);
stopButton->setEnabled(false);
statusLabel->setText(tr("Status: Not connected"));
}
void TcpClient::readData()
{
QByteArray data = tcpSocket->readAll();
textEdit->append(QString(data));
}
void TcpClient::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Error"), tr("The host was not found. Please check the host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Error"), tr("The connection was refused by the peer. Make sure the fortune server is running, and check that the host name and port settings are correct."));
break;
default:
QMessageBox::information(this, tr("Error"), tr("The following error occurred: %1.").arg(tcpSocket->errorString()));
}
startButton->setEnabled(true);
stopButton->setEnabled(false);
statusLabel->setText(tr("Status: Not connected"));
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TcpClient client;
client.show();
return a.exec();
}
#include "tcpclient.moc"
```
该代码使用QTcpSocket类来处理TCP连接。 它创建一个简单的界面,允许用户输入服务器主机名和端口号,并包括控制连接的开始和停止按钮。 一旦连接建立,它将显示从服务器接收到的任何数据。
请注意,该代码仅是一个基本示例,可能需要根据您的特定需求进行修改。
阅读全文