C++、Qt学习交流群: 302558294(欢迎你的加入)
效果图:
原理:
用到的库:QTcpServer,QTcpServer,QHostAddress,QtNetwork。
源码:
服务器端:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtNetwork>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
#include <QByteArray>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public:
void init();
private slots:
void sendMessage(); //发送消息
void onReciveData(); //接收数据
void newListen(); //建立tcp监听事件
void acceptConnection(); //接收客户端连接
void showError(QAbstractSocket::SocketError); //错误输出
private:
Ui::MainWindow *ui;
private:
QTcpSocket *tcpSocket;
QTcpServer *tcpServer;
// QTimer *timer;
QByteArray mChat;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include <QObject>
#include <QString>
#include <QTextEdit>
#include <QHostAddress>
#include <QByteArray>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);
init();
setWindowTitle(QString::fromLocal8Bit("服务器端"));
connect(ui->sendBtn,SIGNAL(clicked(bool)),SLOT(sendMessage()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init()
{
// timer = new QTimer;
tcpServer = new QTcpServer;
tcpSocket = new QTcpSocket;
newListen();
connect(tcpServer,SIGNAL(newConnection()),SLOT(acceptConnection()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),SLOT(showError(QAbstractSocket::SocketError)));
}
void MainWindow::sendMessage() //发送数据
{
QString textEdit = ui->lineEdit->text();
QString strData =QString::fromLocal8Bit("Time: ") + QTime::currentTime().toString() + "\n" + textEdit.toLocal8Bit() + "\n";
QByteArray sendMessage = strData.toLocal8Bit();
mChat += ("Send " + sendMessage);
ui->textEdit->setText(mChat);
tcpSocket->write(sendMessage);
}
void MainWindow::onReciveData() //读取数据
{
QString data = tcpSocket->readAll();
qDebug()<<data;
mChat +=("Recv " + data);
ui->textEdit->setText(mChat);
}
void MainWindow::newListen()
{
if(!tcpServer->listen(QHostAddress::Any,6666))
{
qDebug()<<tcpServer->errorString();
tcpServer->close();
}
}
void MainWindow::acceptConnection()
{
tcpSocket = tcpServer->nextPendingConnection();
connect(tcpSocket,SIGNAL(readyRead()),SLOT(onReciveData()));
}
void MainWindow::showError(QAbstractSocket::SocketError)
{
qDebug()<<tcpSocket->errorString();
tcpSocket->close();
}
客户端:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtNetwork>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
#include <QAbstractSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public:
void init();
void newTcpConnect();
private slots:
void onReciveData();
void onSendMessage();
void onShowError(QAbstractSocket::SocketError);
private:
QTcpSocket *tcpSocket;
QByteArray mChat;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(QString::fromLocal8Bit("客户端"));
init();
connect(ui->sendBtn,SIGNAL(clicked(bool)),SLOT(onSendMessage()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init()
{
tcpSocket = new QTcpSocket;
newTcpConnect();
connect(tcpSocket,SIGNAL(readyRead()),SLOT(onReciveData()));
}
void MainWindow::newTcpConnect()
{
tcpSocket->abort();
tcpSocket->connectToHost("127.0.0.1",6666);
}
void MainWindow::onReciveData()
{
QString data = tcpSocket->readAll();
qDebug()<<data;
mChat += ("Recv " + data);
ui->textEdit->setText(mChat);
}
void MainWindow::onSendMessage()
{
QString textEdit = ui->lineEdit->text();
QString strData =QString::fromLocal8Bit("Time: ") + QTime::currentTime().toString() + "\n" + textEdit.toLocal8Bit() +"\n";
QByteArray sendMessage = strData.toLocal8Bit();
mChat+= ("Send " + sendMessage);
ui->textEdit->setText(mChat);
tcpSocket->write(sendMessage);
}
void MainWindow::onShowError(QAbstractSocket::SocketError)
{
qDebug()<<tcpSocket->errorString();
tcpSocket->close();
}
C++、Qt学习交流群: 302558294(欢迎你的加入)