和之前两个例子一样,程序比较简单,刚入门的可以看看,先上效果图。
下面是客户端:
myClient.h
#ifndef CLIENT_H
#define CLIENT_H#include <QDialog>QT_BEGIN_NAMESPACE
namespace Ui { class Client; }
QT_END_NAMESPACEclass QTcpSocket;
class QTimer;class Client : public QDialog
{Q_OBJECTpublic:Client(QWidget *parent = nullptr);~Client();private slots:void slot_readyRead();void slot_connected();void slot_timeout();// void slot_disconnected();void slot_error();void on_connectButton_clicked(bool checked);void on_sendButton_clicked();void slot_sendDatatimeout();
// void on_sendTextEdit_cursorPositionChanged();// void on_sendTextEdit_textChanged();private:Ui::Client *ui;QTcpSocket *myClient;QTimer *timer;// qint8 flag;
};
#endif // CLIENT_H
myClent.cpp
#include "client.h"
#include "ui_client.h"
#include <QtNetwork>
#include <QDebug>
#include <QMessageBox>
#include <QTimer>Client::Client(QWidget *parent): QDialog(parent), ui(new Ui::Client)/*,flag(0)*/
{ui->setupUi(this);myClient = new QTcpSocket(this);connect(myClient,&QTcpSocket::readyRead,this,&Client::slot_readyRead);connect(myClient,&QTcpSocket::connected,this,&Client::slot_connected);connect(myClient,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(slot_error()));
// connect(myClient,&QTcpSocket::disconnected,this,&Client::slot_disconnected);ui->sendButton->setEnabled(false);ui->connectStatus->setText(tr(" "));ui->receiveStatus->setText(tr(" "));ui->sendStatus->setText(tr(" "));timer = new QTimer;connect(timer,&QTimer::timeout,this,&Client::slot_timeout);connect(timer,&QTimer::timeout,this,&Client::slot_sendDatatimeout);
}Client::~Client()
{delete ui;
}void Client::slot_readyRead()
{if(myClient->bytesAvailable() > 0){ui->recevieTextEdit->append(myClient->readAll());ui->receiveStatus->setText(tr("接收服务端的数据完成!!"));//timer->start(2000);}
}void Client::slot_connected()
{ui->connectStatus->setText(tr("连接成功"));ui->sendButton->setEnabled(true);
}void Client::slot_timeout()
{ui->receiveStatus->setText(tr(" "));
}void Client::slot_error()
{qDebug()<<myClient->errorString();ui->connectButton->setText(tr("连接"));ui->connectStatus->setText(tr("连接断开"));ui->sendStatus->setText(tr(" "));ui->receiveStatus->setText(tr(" "));myClient->close();//同上,都不会立即关闭套接字ui->connectButton->setChecked(false);//之前点击时checked=true,下一次点击时checked=false,//要想使下一次点击时是checked=true,就必须在中间设置一次checked=false
}//void Client::slot_disconnected()
//{
// ui->connectButton->setText(tr("连接"));
// ui->connectStatus->setText(tr("连接断开"));
// ui->sendStatus->setText(tr(" "));
// ui->receiveStatus->setText(tr(" "));
// myClient->disconnectFromHost();//等待所有的数据被写完之后,关闭套接字
// myClient->close();//同上,都不会立即关闭套接字
// flag = 1;
//}void Client::on_connectButton_clicked(bool checked)//第一次点击时为checked=true,第二次点击时checked=false,依次类推
{if(ui->IPLineEdit->text().isEmpty() || ui->portLineEdit->text().isEmpty()){QMessageBox::information(this,tr("提示"),tr("请查看IP和端口是否输入"));return ;}if(checked){ui->connectButton->setText(tr("断开连接"));ui->connectStatus->setText(tr("连接中......"));myClient->connectToHost(ui->IPLineEdit->text(),ui->portLineEdit->text().toInt());}else{ui->connectButton->setText(tr("连接"));ui->connectStatus->setText(tr("连接断开"));ui->sendStatus->setText(tr(" "));ui->sendButton->setEnabled(false);myClient->close();}
}void Client::on_sendButton_clicked()
{QByteArray text = ui->sendTextEdit->toPlainText().toUtf8();if(text.isEmpty()){QMessageBox::information(this,tr("提示"),tr("请输入要发送的数据"));return ;}qint64 bytes = myClient->write(text);if(bytes > 0){ui->sendTextEdit->clear();ui->sendStatus->setText(tr("发送数据成功!"));timer->start(2000);}else{ui->sendTextEdit->clear();ui->sendStatus->setText(tr("发送数据失败!"));timer->start(2000);}
}void Client::slot_sendDatatimeout()
{ui->sendStatus->setText(tr(" "));
}//void Client::on_sendTextEdit_cursorPositionChanged()
//{
// ui->sendStatus->setText(tr(" "));
//}//void Client::on_sendTextEdit_textChanged()
//{// ui->sendStatus->setText(tr(" "));
//}
服务端:
myServer.h
#ifndef SERVER_H
#define SERVER_H#include <QDialog>
#include <QAbstractSocket>QT_BEGIN_NAMESPACE
namespace Ui { class Server; }
QT_END_NAMESPACEclass QTcpServer;
class QTcpSocket;
class QTimer;class Server : public QDialog
{Q_OBJECTpublic:Server(QWidget *parent = nullptr);~Server();private slots:void slot_newConnect();void slot_readyRead();void slot_error(QAbstractSocket::SocketError);void slot_timeout();void on_listenButton_clicked();void on_disconnectButton_clicked();void on_sendButton_clicked();void slot_sendDatatimeout();// void on_sendTextEdit_cursorPositionChanged();private:Ui::Server *ui;QTcpServer *myServer;QTcpSocket *socketConnected;QTimer *timer;
};
#endif // SERVER_H
myServer.cpp
#include "server.h"
#include "ui_server.h"
#include <QtNetwork>
#include <QDebug>
#include <QMessageBox>Server::Server(QWidget *parent): QDialog(parent), ui(new Ui::Server)
{ui->setupUi(this);myServer = new QTcpServer(this);connect(myServer,&QTcpServer::newConnection,this,&Server::slot_newConnect);ui->disconnectButton->setEnabled(false);ui->connectStatus->setText(tr(" "));ui->receiveStatus->setText(tr(" "));ui->sendStatus->setText(tr(" "));timer = new QTimer;connect(timer,&QTimer::timeout,this,&Server::slot_timeout);connect(timer,&QTimer::timeout,this,&Server::slot_sendDatatimeout);
}Server::~Server()
{delete ui;
}void Server::slot_newConnect()
{ui->connectStatus->setText(tr("连接成功"));ui->disconnectButton->setEnabled(true);socketConnected = myServer->nextPendingConnection();connect(socketConnected,SIGNAL(readyRead()),this,SLOT(slot_readyRead()));connect(socketConnected,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(slot_error(QAbstractSocket::SocketError)));myServer->close();
}void Server::slot_readyRead()
{if(socketConnected->bytesAvailable() > 0){ui->receiveTextEdit->append(socketConnected->readAll());ui->receiveStatus->setText(tr("接收数据成功!"));timer->start(2000);}
}void Server::slot_error(QAbstractSocket::SocketError)//在连接过程中,突然被断开,客户端主动断开连接时会调用该函数,服务端自己断开连接,不会调用该函数
{qDebug()<<socketConnected->errorString();ui->connectStatus->setText(tr("连接中断"));ui->receiveStatus->setText(tr(" "));ui->sendStatus->setText(tr(" "));ui->disconnectButton->setEnabled(false);socketConnected->disconnectFromHost();myServer->close();
}void Server::slot_timeout()
{ui->receiveStatus->setText(tr(" "));
}void Server::on_listenButton_clicked()
{if(!myServer->listen(QHostAddress::LocalHost,7878)){qDebug()<<"监听失败!!";ui->connectStatus->setText(tr("监听失败"));}ui->connectStatus->setText(tr("监听成功!!!"));
}void Server::on_disconnectButton_clicked()
{ui->connectStatus->setText(tr("连接断开"));ui->sendStatus->setText(tr(" "));ui->receiveStatus->setText(tr(" "));ui->disconnectButton->setEnabled(false);socketConnected->abort();//立即断开myServer->close();
}void Server::on_sendButton_clicked()
{if(ui->sendTextEdit->toPlainText().isEmpty()){QMessageBox::information(this,tr("提示"),tr("请输入要发送的数据!!"));return ;}qint64 bytes = socketConnected->write(ui->sendTextEdit->toPlainText().toUtf8());if(bytes > 0){ui->sendTextEdit->clear();ui->sendStatus->setText(tr("发送数据成功!!"));timer->start(2000);}else{ui->sendTextEdit->clear();ui->sendStatus->setText(tr("发送数据失败!!"));timer->start(2000);}//ui->sendTextEdit->clear();//为什么在这里的时候达不到先显示数据发送的结构,再将提示小时
}void Server::slot_sendDatatimeout()
{ui->sendStatus->setText(tr(" "));
}//void Server::on_sendTextEdit_cursorPositionChanged()
//{
// ui->sendStatus->setText(tr(" "));
//}
运行服务端的程序,点击监听按钮,运行客户端,输入IP和端口,IP是本地地址,端口为7878,点击连接按钮,连接成功后,窗口下面均有连接成功的提示,此时可以客户端和服务端之间相互发送消息,也可以在客户端和服务端任意断开连接后重连,窗口消息发送与接收都有是否成功的提示,没有输入IP和端口之前,若点击了连接按钮,会有提示。