for u
in .h
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
{
Q_OBJECT
public:
private slots:
void on_connected();
void connectToServer();
void writeMessage();
private:
};
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
class QTcpSocket;
class TCPClient : public QWidget
{
Q_OBJECT
public:
TCPClient(QWidget *parent =0);
private slots:
void on_connected();
void connectToServer();
void writeMessage();
private:
QLineEdit *m_lineEdit;
QPushButton *m_button;
QPushButton *m_sendButton;
QTcpSocket *m_socket;
};
To copy to clipboard, switch view to plain text mode
#include <QtGui>
#include <QTcpSocket>
#include <QHostAddress>
#include <QDebug>
#include "TCPClient.h"
TCPClient
::TCPClient(QWidget *parent
){
m_sendButton->setEnabled(false);
connect(m_button,SIGNAL(clicked()), this,SLOT(connectToServer()));
connect(m_sendButton, SIGNAL(clicked()), this, SLOT(writeMessage()));
connect(m_socket, SIGNAL(connected()), this, SLOT(on_connected()));
hLayout->addWidget(m_button);
hLayout->addWidget(m_sendButton);
mLayout->addWidget(m_lineEdit);
mLayout->addLayout(hLayout);
this->setLayout(mLayout);
}
void
TCPClient::on_connected()
{
qDebug()<<"Debug Connection established:";
m_sendButton->setEnabled(true);
}
void
TCPClient::writeMessage()
{
QString writeData
= this
->m_lineEdit
->text
();
m_socket->write(writeData.toStdString().c_str());
m_socket->flush();
}
void
TCPClient::connectToServer()
{
}
#include <QtGui>
#include <QTcpSocket>
#include <QHostAddress>
#include <QDebug>
#include "TCPClient.h"
TCPClient::TCPClient(QWidget *parent)
: QWidget(parent)
{
m_lineEdit = new QLineEdit();
m_button = new QPushButton("Connect");
m_sendButton = new QPushButton("Send");
m_sendButton->setEnabled(false);
connect(m_button,SIGNAL(clicked()), this,SLOT(connectToServer()));
connect(m_sendButton, SIGNAL(clicked()), this, SLOT(writeMessage()));
m_socket = new QTcpSocket;
connect(m_socket, SIGNAL(connected()), this, SLOT(on_connected()));
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(m_button);
hLayout->addWidget(m_sendButton);
QVBoxLayout *mLayout = new QVBoxLayout;
mLayout->addWidget(m_lineEdit);
mLayout->addLayout(hLayout);
this->setLayout(mLayout);
}
void
TCPClient::on_connected()
{
qDebug()<<"Debug Connection established:";
m_sendButton->setEnabled(true);
}
void
TCPClient::writeMessage()
{
QString writeData = this->m_lineEdit->text();
m_socket->write(writeData.toStdString().c_str());
m_socket->flush();
}
void
TCPClient::connectToServer()
{
m_socket->connectToHost(QHostAddress::LocalHost, 1234);
}
To copy to clipboard, switch view to plain text mode
and main
#include <QApplication>
#include "TCPClient.h"
int main(int argc, char *argv[])
{
TCPClient c;
c.show();
return a.exec();
}
#include <QApplication>
#include "TCPClient.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TCPClient c;
c.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
use this client ..
Bookmarks