#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("Client");
this->client = new QTcpSocket(this);
connect(this->ui->requestPushButton, SIGNAL(clicked()), this, SLOT(clickRequestButton()));
connect(this->client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(this->client, SIGNAL(readyRead()), this, SLOT(readFormServer()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::clickRequestButton()
{
sendToServer();
}
void Widget::sendToServer()
{
this->client->abort();
QString ip = "localhost";
int port = 9876;
this->client->connectToHost(ip, port);
QString str = this->ui->toTextEdit->toPlainText();
if(str != "")
{
string str2 = string(str.toAscii().data());
const char *str3 = str2.c_str();
this->client->write(str3);
}
}
void Widget::readFormServer()
{
this->ui->fromTextEdit->clear();
QString str5;
str5 = this->client->readAll();
this->ui->fromTextEdit->setText(str5);
}
void Widget::displayError(QAbstractSocket::SocketError)
{
this->ui->errorLabel->setText(this->client->errorString());
}
- 1
- 2
- 3
- 4
前往页