HI wagmare.. I have tried enough times same sample code, what was shared by you. I had created full new project of TCPServer/Client. Still inmy LAN its not workin or in my same PC also. Any problem in the code. please review once, I need to understand where my code is going wrong.
TCP CLIENT main.cpp
#include <QtGui/QApplication>
#include "tcpclient.h"
int main(int argc, char *argv[])
{
TCPClient w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "tcpclient.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TCPClient w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
TCP Client.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QTcpSocket>
namespace Ui {
class TCPClient;
}
Q_OBJECT
public:
~TCPClient();
protected:
private:
Ui::TCPClient *ui;
private slots:
void connecttoserver();
void on_connected();
void write();
};
#endif // TCPCLIENT_H
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QTcpSocket>
class QTcpSocket;
namespace Ui {
class TCPClient;
}
class TCPClient : public QWidget {
Q_OBJECT
public:
TCPClient(QWidget *parent = 0);
~TCPClient();
protected:
void changeEvent(QEvent *e);
private:
Ui::TCPClient *ui;
QLineEdit *le;
QPushButton *pb_connect;
QPushButton *pb_send;
QTcpSocket *socket;
private slots:
void connecttoserver();
void on_connected();
void write();
};
#endif // TCPCLIENT_H
To copy to clipboard, switch view to plain text mode
TCPClient.cpp
#include "tcpclient.h"
#include "ui_tcpclient.h"
#include <QtGui>
#include <QTcpSocket>
#include <QHostAddress>
#include <QDebug>
TCPClient
::TCPClient(QWidget *parent
) : ui(new Ui::TCPClient)
{
ui->setupUi(this);
le=ui->lineEdit;
pb_connect=ui->pushButton;
pb_connect->setText("CONNECT");
pb_connect->setEnabled(true);
pb_send=ui->pushButton_2;
pb_send->setText("Send");
pb_send->setEnabled(false);
connect(pb_connect,SIGNAL(clicked()),this,SLOT(connecttoserver()));
connect(pb_send,SIGNAL(clicked()),this,SLOT(write()));
connect(socket,SIGNAL(connected()),this,SLOT(on_connected()));
}
TCPClient::~TCPClient()
{
delete ui;
}
void TCPClient
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
void TCPClient::connecttoserver()
{
//QHostAddress hAddr;
// hAddr.setAddress("192.168.64.55");
}
void TCPClient::on_connected()
{
pb_connect->setEnabled(false);
pb_send->setEnabled(true);
qDebug("Connection established");
}
void TCPClient::write()
{
socket->write(data.toUtf8().constData());
//const char * data = writeData .toUtf8().constData();
socket->flush();
}
#include "tcpclient.h"
#include "ui_tcpclient.h"
#include <QtGui>
#include <QTcpSocket>
#include <QHostAddress>
#include <QDebug>
TCPClient::TCPClient(QWidget *parent) :
QWidget(parent),
ui(new Ui::TCPClient)
{
ui->setupUi(this);
le= new QLineEdit;
le=ui->lineEdit;
pb_connect=new QPushButton;
pb_connect=ui->pushButton;
pb_connect->setText("CONNECT");
pb_connect->setEnabled(true);
pb_send=new QPushButton;
pb_send=ui->pushButton_2;
pb_send->setText("Send");
pb_send->setEnabled(false);
socket=new QTcpSocket(this);
connect(pb_connect,SIGNAL(clicked()),this,SLOT(connecttoserver()));
connect(pb_send,SIGNAL(clicked()),this,SLOT(write()));
connect(socket,SIGNAL(connected()),this,SLOT(on_connected()));
}
TCPClient::~TCPClient()
{
delete ui;
}
void TCPClient::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void TCPClient::connecttoserver()
{
//QHostAddress hAddr;
// hAddr.setAddress("192.168.64.55");
socket->connectToHost(QHostAddress::Any,1234);
}
void TCPClient::on_connected()
{
pb_connect->setEnabled(false);
pb_send->setEnabled(true);
qDebug("Connection established");
}
void TCPClient::write()
{
QString data=this->le->text();
socket->write(data.toUtf8().constData());
//const char * data = writeData .toUtf8().constData();
socket->flush();
}
To copy to clipboard, switch view to plain text mode
TCP Client.pro
#-------------------------------------------------
#
# Project created by QtCreator 2013-09-27T10:57:57
#
#-------------------------------------------------
QT += network
TARGET = TCPClient
TEMPLATE = app
SOURCES += main.cpp\
tcpclient.cpp
HEADERS += tcpclient.h
FORMS += tcpclient.ui
#-------------------------------------------------
#
# Project created by QtCreator 2013-09-27T10:57:57
#
#-------------------------------------------------
QT += network
TARGET = TCPClient
TEMPLATE = app
SOURCES += main.cpp\
tcpclient.cpp
HEADERS += tcpclient.h
FORMS += tcpclient.ui
To copy to clipboard, switch view to plain text mode
================================================== ===================================
TCP Server.main.cpp
#include <QtGui/QApplication>
#include "tcpserver.h"
int main(int argc, char *argv[])
{
Tcpserver w;
w.listen();
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "tcpserver.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Tcpserver w;
w.listen();
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
TCP SERVER.h
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QTcpServer>
#include <QTcpSocket>
namespace Ui {
class Tcpserver;
}
Q_OBJECT
public:
~Tcpserver();
void listen();
protected:
private:
Ui::Tcpserver *ui;
private slots:
void on_newconn();
void read_socket();
void socket_disconnected();
};
#endif // TCPSERVER_H
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QTcpServer>
#include <QTcpSocket>
class QTcpServer;
class QTcpSocket;
namespace Ui {
class Tcpserver;
}
class Tcpserver : public QWidget {
Q_OBJECT
public:
Tcpserver(QWidget *parent = 0);
~Tcpserver();
void listen();
protected:
void changeEvent(QEvent *e);
private:
Ui::Tcpserver *ui;
QLineEdit *le;
QPushButton *Pb_newconn;
QPushButton *Pb_send;
QTcpServer *server;
QTcpSocket *socket;
private slots:
void on_newconn();
void read_socket();
void socket_disconnected();
};
#endif // TCPSERVER_H
To copy to clipboard, switch view to plain text mode
TCP SERVER.cpp
#include "tcpserver.h"
#include "ui_tcpserver.h"
Tcpserver
::Tcpserver(QWidget *parent
) : ui(new Ui::Tcpserver)
{
ui->setupUi(this);
le=ui->lineEdit;
Pb_newconn=ui->pushButton;
Pb_newconn->setText("Listen");
Pb_newconn->setEnabled(true);
Pb_send=ui->pushButton_2;
Pb_send->setText("SEND");
Pb_send->setEnabled(false);
//connect(Pb_newconn,SIGNAL(clicked()),this,SLOT(listen()));
connect(server,SIGNAL(newConnection()),this,SLOT(on_newconn()));
connect(socket,SIGNAL(readyRead()),this,SLOT(read_socket()));
connect(socket,SIGNAL(disconnected()),this,SLOT(socket_disconnected()));
}
Tcpserver::~Tcpserver()
{
delete ui;
}
void Tcpserver
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
void Tcpserver::listen()
{
//QHostAddress hAddr;
//hAddr.setAddress("192.168.64.52");
qDebug("Listening to new connection");
}
void Tcpserver::on_newconn()
{
socket=server->nextPendingConnection();
{
qDebug("new connection established");
}
Pb_newconn->setEnabled(false);
Pb_send->setEnabled(true);
}
void Tcpserver::read_socket()
{
while(socket->canReadLine())
{
if(strcmp(buffer.constData(), "!exit\n") == 0)
{
qDebug("No data");
}
else
le->setText(buffer);
}
}
void Tcpserver::socket_disconnected()
{
qDebug("Socket Disconnected");
socket->deleteLater();
}
#include "tcpserver.h"
#include "ui_tcpserver.h"
Tcpserver::Tcpserver(QWidget *parent) :
QWidget(parent),
ui(new Ui::Tcpserver)
{
ui->setupUi(this);
le=new QLineEdit;
le=ui->lineEdit;
Pb_newconn= new QPushButton;
Pb_newconn=ui->pushButton;
Pb_newconn->setText("Listen");
Pb_newconn->setEnabled(true);
Pb_send=new QPushButton;
Pb_send=ui->pushButton_2;
Pb_send->setText("SEND");
Pb_send->setEnabled(false);
server=new QTcpServer(this);
socket= new QTcpSocket(this);
//connect(Pb_newconn,SIGNAL(clicked()),this,SLOT(listen()));
connect(server,SIGNAL(newConnection()),this,SLOT(on_newconn()));
connect(socket,SIGNAL(readyRead()),this,SLOT(read_socket()));
connect(socket,SIGNAL(disconnected()),this,SLOT(socket_disconnected()));
}
Tcpserver::~Tcpserver()
{
delete ui;
}
void Tcpserver::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void Tcpserver::listen()
{
//QHostAddress hAddr;
//hAddr.setAddress("192.168.64.52");
server->listen(QHostAddress::Any,1234);
qDebug("Listening to new connection");
}
void Tcpserver::on_newconn()
{
socket=server->nextPendingConnection();
if(socket->state()==QTcpSocket::ConnectedState)
{
qDebug("new connection established");
}
Pb_newconn->setEnabled(false);
Pb_send->setEnabled(true);
}
void Tcpserver::read_socket()
{
while(socket->canReadLine())
{
QByteArray buffer= socket->readLine();
if(strcmp(buffer.constData(), "!exit\n") == 0)
{
qDebug("No data");
}
else
le->setText(buffer);
}
}
void Tcpserver::socket_disconnected()
{
qDebug("Socket Disconnected");
socket->deleteLater();
}
To copy to clipboard, switch view to plain text mode
TCP SERVER.pro
#-------------------------------------------------
#
# Project created by QtCreator 2013-09-27T13:50:57
#
#-------------------------------------------------
QT += network
TARGET = TCPServer
TEMPLATE = app
SOURCES += main.cpp\
tcpserver.cpp
HEADERS += tcpserver.h
FORMS += tcpserver.ui
#-------------------------------------------------
#
# Project created by QtCreator 2013-09-27T13:50:57
#
#-------------------------------------------------
QT += network
TARGET = TCPServer
TEMPLATE = app
SOURCES += main.cpp\
tcpserver.cpp
HEADERS += tcpserver.h
FORMS += tcpserver.ui
To copy to clipboard, switch view to plain text mode
Bookmarks