Created
February 1, 2012 16:00
-
-
Save lgliducik/1717703 to your computer and use it in GitHub Desktop.
client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtGui> | |
#include <QtNetwork> | |
#include <QFileInfo> | |
#include "client.h" | |
#include <iostream> | |
using namespace std; | |
Client::Client(QWidget *parent) | |
: QDialog(parent) | |
{ | |
hostLabel = new QLabel(tr("&Server name:")); | |
portLabel = new QLabel(tr("S&erver port for address:")); | |
portLabelData = new QLabel(tr("S&erver port for data:")); | |
addrLabel = new QLabel(tr("Start address for data:")); | |
hostLineEdit = new QLineEdit("10.73.17.122"); | |
portLineEdit = new QLineEdit("7"); | |
portLineEdit->setValidator(new QIntValidator(1, 65535, this)); | |
portLineEditData = new QLineEdit("2010"); | |
portLineEditData->setValidator(new QIntValidator(1, 65535, this)); | |
addrLineEdit = new QLineEdit("0x10800000"); | |
addrLabel->setBuddy(addrLineEdit); | |
hostLabel->setBuddy(hostLineEdit); | |
portLabel->setBuddy(portLineEdit); | |
portLabelData->setBuddy(portLineEditData); | |
textLineEdit = new QTextEdit(); | |
openLineEdit = new QLineEdit(); | |
sendButton = new QPushButton(tr("Send data")); | |
sendButton->setDefault(false); | |
sendButton->setEnabled(true); | |
openButton = new QPushButton(tr("...")); | |
openButton->setDefault(true); | |
openButton->setEnabled(true); | |
quitButton = new QPushButton(tr("Quit")); | |
buttonBox = new QDialogButtonBox; | |
buttonBox->addButton(sendButton, QDialogButtonBox::ActionRole); | |
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); | |
tcpSocket = new QTcpSocket(this); | |
connect(sendButton, SIGNAL(clicked()),this, SLOT(sentToServer())); | |
connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); | |
connect(openButton, SIGNAL(clicked()), this, SLOT(open())); | |
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), | |
this, SLOT(displayError(QAbstractSocket::SocketError))); | |
QGridLayout *mainLayout = new QGridLayout; | |
//mainLayout->addWidget(openLineEdit,0,0,1,2); | |
//mainLayout->addWidget(openButton, 0, 2,1,1); | |
mainLayout->addWidget(hostLabel, 0, 0); | |
mainLayout->addWidget(hostLineEdit, 0, 1); | |
mainLayout->addWidget(portLabel, 1, 0); | |
mainLayout->addWidget(portLineEdit, 1, 1); | |
mainLayout->addWidget(portLabelData, 2, 0); | |
mainLayout->addWidget(portLineEditData, 2, 1); | |
mainLayout->addWidget(addrLabel, 3, 0); | |
mainLayout->addWidget(addrLineEdit, 3, 1); | |
//mainLayout->addWidget(buttonBox, 5, 0, 1, 1); | |
//mainLayout->addWidget(textLineEdit, 6, 0, 1, 1); | |
QVBoxLayout *LayoutTEB = new QVBoxLayout; | |
LayoutTEB->addLayout(mainLayout); | |
LayoutTEB->addWidget(buttonBox); | |
QHBoxLayout *LayoutTEBL = new QHBoxLayout; | |
LayoutTEBL->addLayout(LayoutTEB); | |
LayoutTEBL->addWidget(textLineEdit); | |
QHBoxLayout *LayoutF = new QHBoxLayout; | |
LayoutF->addWidget(openLineEdit); | |
LayoutF->addWidget(openButton); | |
//LayoutF->addWidget(buttonBox); | |
QVBoxLayout *Layout = new QVBoxLayout; | |
Layout->addLayout(LayoutF); | |
Layout->addLayout(LayoutTEBL); | |
setLayout(Layout); | |
setWindowTitle(tr("Client")); | |
} | |
void Client::sentToServer() | |
{ | |
sendButton->setEnabled(false); | |
tcpSocket->abort(); | |
tcpSocket->connectToHost(hostLineEdit->text(),portLineEdit->text().toInt()); | |
textLineEdit->append("connect to device with ip "+ hostLineEdit->text()); | |
//textLineEdit->append(hostLineEdit->text()); | |
textLineEdit->append("port number is " + portLineEdit->text()); | |
//передача адреса в порт 7 | |
QString addr_s = addrLineEdit->text(); | |
tcpSocket->write(addr_s.toAscii()); | |
if(tcpSocket->waitForReadyRead(1000)==false) //тут ждем ответа для чтения адреса | |
{ | |
textLineEdit->append("disconnect..."); | |
//TODO: начать заново (например else и выход из функции send..) | |
} | |
QByteArray reply_addr = tcpSocket->read(tcpSocket->bytesAvailable()); | |
//сервер посылает 1 если принял адресс | |
if(reply_addr.size()>0) | |
{ | |
QString s; | |
s.append("address = "); | |
s.append(addr_s ); | |
textLineEdit->append("addres is set"); | |
textLineEdit->append(s); | |
//передача размера файла(в байтах) в порт 7 | |
int countByte = file.size(); | |
if(countByte!=0) | |
{ | |
QString size_s = QString::number(countByte); | |
tcpSocket->write(size_s.toAscii()); | |
if(tcpSocket->waitForReadyRead(1000)==false) //тут ждем ответа для чтения размера файла | |
{ | |
textLineEdit->append("disconnect..."); | |
////TODO: начать заново (например else и выход из функции send..) | |
} | |
QByteArray reply_size = tcpSocket->read(tcpSocket->bytesAvailable()); | |
//сервер посылает 1, если принял размер файла | |
//if(tcpSocket->bytesAvailable()>0) | |
if(reply_size.size()>0) | |
{ | |
textLineEdit->append("size is set"); | |
QString s; | |
s.append("size of file = "); | |
s.append(size_s ); | |
s.append(" bytes"); | |
textLineEdit->append(s); | |
tcpSocket->abort(); | |
//соединяемся с портом 2010(по умолчанию) | |
tcpSocket->connectToHost(hostLineEdit->text(),portLineEditData->text().toInt()); | |
textLineEdit->append("port number is " + portLineEditData->text()); | |
//textLineEdit->append(portLineEditData->text()); | |
//передача файла в порт 2010 (по умолч.) | |
QByteArray arr = file.readAll(); | |
tcpSocket->write(arr); | |
if(tcpSocket->waitForReadyRead(1000)==false) //тут ждем ответа о чтении данных | |
{ | |
textLineEdit->append("disconnect1..."); | |
//TODO: начать заново (например else и выход из функции send..) | |
} | |
//сервер посылает 1, если принял данные из файла | |
QByteArray reply_data = tcpSocket->read(tcpSocket->bytesAvailable()); | |
if(reply_data.size()>0) | |
{ | |
textLineEdit->append("data from file is transfered"); | |
} | |
} | |
} | |
else | |
{ | |
textLineEdit->append("file is empty"); | |
} | |
} | |
file.close(); | |
} | |
void Client::displayError(QAbstractSocket::SocketError socketError) | |
{ | |
switch (socketError) { | |
case QAbstractSocket::RemoteHostClosedError: | |
break; | |
case QAbstractSocket::HostNotFoundError: | |
QMessageBox::information(this, tr("Client"), | |
tr("The host was not found. Please check the " | |
"host name and port settings.")); | |
break; | |
case QAbstractSocket::ConnectionRefusedError: | |
QMessageBox::information(this, tr("Client"), | |
tr("The connection was refused by the peer. " | |
"Make sure the server is running, " | |
"and check that the host name and port " | |
"settings are correct.")); | |
break; | |
default: | |
QMessageBox::information(this, tr("Client"), | |
tr("The following error occurred: %1.") | |
.arg(tcpSocket->errorString())); | |
} | |
sendButton->setEnabled(true); | |
} | |
int Client::readFile(const QString &fileName) | |
{ | |
strSend.clear(); | |
file.setFileName(fileName); | |
if (file.open(QIODevice::ReadOnly)==false) | |
{ | |
setMessage("Cannot open file for reading!"); | |
return 0; | |
} | |
else | |
{ | |
textLineEdit->append("file is opened"); | |
return 1; | |
} | |
} | |
void Client::open() | |
{ | |
fileName= QFileDialog::getOpenFileName(this, | |
tr("Open configure file"), "."); | |
QFileInfo FileIn(fileName); | |
if (!fileName.isEmpty()) | |
{ | |
sendButton->setEnabled(true); | |
readFile(fileName); | |
openLineEdit->insert(fileName); | |
sendButton->setEnabled(true); | |
} | |
} | |
void Client::setMessage(QString str) | |
{ | |
QMessageBox msgBox; | |
msgBox.setText(str); | |
msgBox.setWindowTitle("Error"); | |
msgBox.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment