#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
server = new QTcpServer();
server->listen(QHostAddress::AnyIPv4, SERVER_PORT);
connect(server, &QTcpServer::newConnection, this, &Widget::newClientHandler);
}
Widget::~Widget()
{
delete ui;
}
void Widget::newClientHandler()
{
socket = server->nextPendingConnection();
ui->hostLineEdit->setText(socket->peerAddress().toString());
ui->portLineEdit->setText(QString::number(socket->peerPort()));
connect(socket, &QTcpSocket::readyRead, this, &Widget::clientInfoSlot);
}
void Widget::clientInfoSlot()
{
ui->chatLineEdit->setText(QString(socket->readAll()));
}
void Widget::on_closeButton_clicked()
{
socket->close();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
socket = new QTcpSocket;
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_connectButton_clicked()
{
socket->connectToHost(ui->hostLineEdit->text(), ui->portLineEdit->text().toInt());
connect(socket, &QTcpSocket::connected, [this](){
this->hide();
Chat *ct = new Chat(socket);
ct->show();
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
#include "chat.h"
#include "ui_chat.h"
#
Chat::Chat(QTcpSocket *socket, QWidget *parent) :
QWidget(parent),
ui(new Ui::Chat)
{
ui->setupUi(this);
this->socket = socket;
}
Chat::~Chat()
{
delete ui;
}
void Chat::on_sendButton_clicked()
{
QByteArray ba;
ba.append(ui->chatLineEdit->text());
socket->write(ba);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26