0%

TCP

client

learned

  • .pro中+network
  • QTcpsocket类::connetToHost
  • 跳转方式,创建新的指针(指针在堆上,实例在栈上)
  • ba.append(ui->lineEdit->text().toUtf8()); QByteArray加QString字符串的方法

code

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
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include"chat.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
socket=new QTcpSocket;
}

Widget::~Widget()
{
delete ui;
}

void Widget::on_cancelButton_clicked()
{
this->close();
}


void Widget::on_connectButton_clicked()
{
//ip port
QString IP=ui->ipLine->text();
QString port=ui->portLine->text();
//connect
socket->connectToHost(QHostAddress(IP),port.toShort());

//whether success
connect(socket,&QTcpSocket::connected,[this](){
QMessageBox::information(this,"连接提示","连接服务器成功");
this->hide();
Chat *C=new Chat(socket);
C->show();
});

connect(socket,&QTcpSocket::disconnected,[this](){
QMessageBox::warning(this,"连接提示","连接异常,网络断开");
});

}


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
#include "chat.h"
#include "ui_chat.h"

Chat::Chat(QTcpSocket *s,QWidget *parent)
: QWidget(parent)
, ui(new Ui::Chat)
{
ui->setupUi(this);
socket=s;
}

Chat::~Chat()
{
delete ui;
}

void Chat::on_clearButton_clicked()
{
ui->lineEdit->clear();
}


void Chat::on_sendButton_clicked()
{
QByteArray ba;
ba.append(ui->lineEdit->text().toUtf8());
ui->lineEdit->text();
socket->write(ba);
}

server

  • 线程的创建 继承QThread ->start启动
  • 信号量 emit+signals声明 发送信号

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "mythread.h"

myThread::myThread(QTcpSocket *s)
{
socket=s;
}

void myThread::run()
{
connect(socket,&QTcpSocket::readyRead,this,&myThread::clientInfoSlot);
}

void myThread::clientInfoSlot()
{
// socket->readAll();
QByteArray ba=socket->readAll();
emit sendToWidget(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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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,PORT);

connect(server,&QTcpServer::newConnection,this,&Widget::newClientHandler);
}

Widget::~Widget()
{
delete ui;
}

void Widget::newClientHandler()
{
QTcpSocket *socket=server->nextPendingConnection();
// socket->peerAddress();
// socket->peerPort();

ui->ipLineEdit->setText(socket->peerAddress().toString());
ui->portLineEdit->setText(QString::number(socket->peerPort()));
// connect(socket,&QTcpSocket::readyRead,this,&Widget::clientInfoSlot);

//启动线程
myThread *t=new myThread(socket);
t->start();

connect(t,&myThread::sendToWidget,this,&Widget::threadSlot);

}

// void Widget::clientInfoSlot(){
// QTcpSocket *s = (QTcpSocket*)sender();//获取信号发出者
// ui->mainLineEdit->setText(QString(s->readAll()));

// }

void Widget::threadSlot(QByteArray b)
{
ui->mainLineEdit->setText(QString(b));
}