#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
tcpSocket = new QTcpSocket;
connect(tcpSocket,SIGNAL(connected()),this,SLOT(connect_success()));
tcpSocket->connectToHost("172.20.10.3",1234);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_send_clicked()
{
std::string msg = ui->msg->text().toStdString();
int ret = tcpSocket->write(msg.c_str(),msg.size()+1);
qDebug("--send:%d--",ret);
}
void Widget::connect_success()
{
ui->send->setEnabled(true);
}
Qt中封装了TCP类客户端只需要设置槽函数,并且连接到服务器。如果连接成功会发送connected()信号ÿ