Hi there, I've created this client/server app. It's basically a chat app. The client and server work perfectly fine on a localHost (i.e if I open up my server and two clients on the same computer, I can send messages from one client to the server and the server relays the message to the other client). Okay, here is my problem, When I install my client program on another computer that is connected to the internet,and then run the server program on another computer, my client program does not connect to the server at all. Both the server computer and the client computer are connected to the internet. here is my code below.
#ifndef MYSOCKET_H
#define MYSOCKET_H
#include <QTcpSocket>
#include <QThread>
{
Q_OBJECT
public:
explicit MySocket(int descriptor);
void connectThread
(QThread &thread
);
signals:
public slots:
void setupSocket();
void readSocket();
void disconnected();
void connecte_d();
private:
int socketDescriptor;
};
#endif // MYSOCKET_H
#ifndef MYSOCKET_H
#define MYSOCKET_H
#include <QTcpSocket>
#include <QThread>
class MySocket : public QTcpSocket
{
Q_OBJECT
public:
explicit MySocket(int descriptor);
void connectThread(QThread &thread);
signals:
void sendData(QByteArray);
public slots:
void setupSocket();
void readSocket();
void writeSocket(QByteArray);
void disconnected();
void connecte_d();
private:
int socketDescriptor;
};
#endif // MYSOCKET_H
To copy to clipboard, switch view to plain text mode
#include "mysocket.h"
MySocket::MySocket(int descriptor) :socketDescriptor(descriptor)
{
this->setSocketDescriptor(descriptor);
}
void MySocket
::connectThread(QThread &thread
) {
connect(&thread, SIGNAL(started()), this, SLOT(setupSocket()));
}
void MySocket::setupSocket()
{
connect(this, SIGNAL(readyRead()), this, SLOT(readSocket()));
connect(this, SIGNAL(connected()), this, SLOT(connecte_d()));
connect(this, SIGNAL(disconnected()), this, SLOT(disconnected()));
}
void MySocket::readSocket()
{
buffer.clear();
buffer.append(this->readAll());
qDebug() << buffer;
emit sendData(buffer);
}
{
this->write(data);
}
void MySocket::connecte_d()
{
qDebug() << socketDescriptor << " connected";
}
void MySocket::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
}
#include "mysocket.h"
MySocket::MySocket(int descriptor) :socketDescriptor(descriptor)
{
this->setSocketDescriptor(descriptor);
}
void MySocket::connectThread(QThread &thread)
{
connect(&thread, SIGNAL(started()), this, SLOT(setupSocket()));
}
void MySocket::setupSocket()
{
connect(this, SIGNAL(readyRead()), this, SLOT(readSocket()));
connect(this, SIGNAL(connected()), this, SLOT(connecte_d()));
connect(this, SIGNAL(disconnected()), this, SLOT(disconnected()));
}
void MySocket::readSocket()
{
QByteArray buffer;
buffer.clear();
buffer.append(this->readAll());
qDebug() << buffer;
emit sendData(buffer);
}
void MySocket::writeSocket(QByteArray data)
{
this->write(data);
}
void MySocket::connecte_d()
{
qDebug() << socketDescriptor << " connected";
}
void MySocket::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
}
To copy to clipboard, switch view to plain text mode
#ifndef MSEVER_H
#define MSEVER_H
#include <QTcpServer>
#include <QDebug>
#include <QThreadPool>
#include <QList>
#include <QString>
#include <QThread>
#include <QTcpSocket>
#include <QHash>
#include "mysocket.h"
{
Q_OBJECT
public:
explicit MServer
(QObject *parent
= 0);
~MServer();
signals:
public slots:
protected:
void incomingConnection(int handle);
private:
MySocket* mSocket;
QList<QThread*>* myThreads;
QThreadPool* threadPool;
static int counter;
};
#endif // MSEVER_H
#ifndef MSEVER_H
#define MSEVER_H
#include <QTcpServer>
#include <QDebug>
#include <QThreadPool>
#include <QList>
#include <QString>
#include <QThread>
#include <QTcpSocket>
#include <QHash>
#include "mysocket.h"
class MServer : public QTcpServer
{
Q_OBJECT
public:
explicit MServer(QObject *parent = 0);
~MServer();
signals:
void sendD(QByteArray);
public slots:
void dataRec(QByteArray);
protected:
void incomingConnection(int handle);
private:
MySocket* mSocket;
QList<QThread*>* myThreads;
QThreadPool* threadPool;
static int counter;
};
#endif // MSEVER_H
To copy to clipboard, switch view to plain text mode
#include "msever.h"
int MServer::counter = 0;
MServer
::MServer(QObject *parent
) :{
qDebug() << "Listening...";
else
qDebug() << "Server failed to start";
myThreads = new QList<QThread*>();
for(int i = 0; i <= 5; i++)
{
myThreads->append(thread);
}
}
MServer::~MServer()
{
for(int i = 0; i <= 5; i++)
myThreads->at(i)->deleteLater();
}
void MServer::incomingConnection(int handle)
{
qDebug() << handle << " Connected";
mSocket = new MySocket(handle);
mSocket->writeSocket("Welcome to Ayanda's Server");
mSocket->connectThread(*(myThreads->at(counter)));
mSocket->moveToThread(myThreads->at(counter));
myThreads->at(counter)->start();
counter++;
}
{
emit sendD(data);
}
#include "msever.h"
int MServer::counter = 0;
MServer::MServer(QObject *parent) :
QTcpServer(parent)
{
if(this->listen(QHostAddress::Any, 1234))
qDebug() << "Listening...";
else
qDebug() << "Server failed to start";
myThreads = new QList<QThread*>();
for(int i = 0; i <= 5; i++)
{
QThread* thread = new QThread();
myThreads->append(thread);
}
}
MServer::~MServer()
{
for(int i = 0; i <= 5; i++)
myThreads->at(i)->deleteLater();
}
void MServer::incomingConnection(int handle)
{
qDebug() << handle << " Connected";
mSocket = new MySocket(handle);
mSocket->writeSocket("Welcome to Ayanda's Server");
connect(mSocket, SIGNAL(sendData(QByteArray)), this, SLOT(dataRec(QByteArray)));
connect(this, SIGNAL(sendD(QByteArray)), mSocket, SLOT(writeSocket(QByteArray)));
mSocket->connectThread(*(myThreads->at(counter)));
mSocket->moveToThread(myThreads->at(counter));
myThreads->at(counter)->start();
counter++;
}
void MServer::dataRec(QByteArray data)
{
emit sendD(data);
}
To copy to clipboard, switch view to plain text mode
#include <QtCore/QCoreApplication>
#include "msever.h"
int main(int argc, char *argv[])
{
MServer Server;
return a.exec();
}
#include <QtCore/QCoreApplication>
#include "msever.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MServer Server;
return a.exec();
}
To copy to clipboard, switch view to plain text mode
And the following code is for my client program.
#ifndef ASIXOXE_UI_H
#define ASIXOXE_UI_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QTcpSocket>
#include <QThread>
#include <QInputDialog>
#include <QString>
namespace Ui {
class AsiXoxe_ui;
}
{
Q_OBJECT
public:
explicit AsiXoxe_ui
(QWidget *parent
= 0);
~AsiXoxe_ui();
signals:
public slots:
void readMsg();
void sendMsg();
void connectedMsg_2();
private:
Ui::AsiXoxe_ui *ui;
private slots:
void on_pushButton_2_clicked();
void on_lineEdit_returnPressed();
void on_pushButton_clicked();
};
#endif // ASIXOXE_UI_H
#ifndef ASIXOXE_UI_H
#define ASIXOXE_UI_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QTcpSocket>
#include <QThread>
#include <QInputDialog>
#include <QString>
namespace Ui {
class AsiXoxe_ui;
}
class AsiXoxe_ui : public QMainWindow
{
Q_OBJECT
public:
explicit AsiXoxe_ui(QWidget *parent = 0);
~AsiXoxe_ui();
signals:
void sendMsg(QString);
public slots:
void displayMsg(QByteArray);
void readMsg();
void sendMsg();
void connectedMsg(QString);
void connectedMsg_2();
private:
Ui::AsiXoxe_ui *ui;
QTcpSocket* socket;
QThread* thread;
QString nickName;
private slots:
void on_pushButton_2_clicked();
void on_lineEdit_returnPressed();
void on_pushButton_clicked();
};
#endif // ASIXOXE_UI_H
To copy to clipboard, switch view to plain text mode
#include "asixoxe_ui.h"
#include "ui_asixoxe_ui.h"
AsiXoxe_ui
::AsiXoxe_ui(QWidget *parent
) : ui(new Ui::AsiXoxe_ui)
{
ui->setupUi(this);
hAddr.setAddress("192.168.43.78");
socket->connectToHost(hAddr, 1234);
socket->waitForConnected(30000);
connect(socket, SIGNAL(readyRead()), this, SLOT(readMsg()));
connect(socket,
SIGNAL(connected
(QString)),
this,
SLOT(connectedMsg
(QString)));
}
AsiXoxe_ui::~AsiXoxe_ui()
{
delete ui;
thread->deleteLater();
}
{
ui->textBrowser->append(msg);
}
void AsiXoxe_ui::on_pushButton_clicked()
{
sendMsg();
}
void AsiXoxe_ui::sendMsg()
{
buffer.append(nickName);
buffer.append(": ");
buffer.append(ui->lineEdit->text());
socket->write(buffer);
ui->lineEdit->clear();
}
void AsiXoxe_ui::readMsg()
{
ui->textBrowser->append(socket->readAll());
socket->flush();
socket->waitForBytesWritten(3000);
socket->moveToThread(thread);
thread->start();
}
void AsiXoxe_ui::on_lineEdit_returnPressed()
{
sendMsg();
}
void AsiXoxe_ui
::connectedMsg(QString c
) {
}
void AsiXoxe_ui::connectedMsg_2()
{
}
void AsiXoxe_ui::on_pushButton_2_clicked()
{
nickName
= QInputDialog::getText(this,
"Name input," "Name or Nickname",
0);
}
#include "asixoxe_ui.h"
#include "ui_asixoxe_ui.h"
AsiXoxe_ui::AsiXoxe_ui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AsiXoxe_ui)
{
ui->setupUi(this);
socket = new QTcpSocket(this);
QHostAddress hAddr;
hAddr.setAddress("192.168.43.78");
socket->connectToHost(hAddr, 1234);
socket->waitForConnected(30000);
connect(socket, SIGNAL(readyRead()), this, SLOT(readMsg()));
connect(socket, SIGNAL(connected(QString)),this, SLOT(connectedMsg(QString)));
}
AsiXoxe_ui::~AsiXoxe_ui()
{
delete ui;
thread->deleteLater();
}
void AsiXoxe_ui::displayMsg(QByteArray sentMsg)
{
QString msg = QString(sentMsg.data());
ui->textBrowser->append(msg);
}
void AsiXoxe_ui::on_pushButton_clicked()
{
sendMsg();
}
void AsiXoxe_ui::sendMsg()
{
QByteArray buffer;
buffer.append(nickName);
buffer.append(": ");
buffer.append(ui->lineEdit->text());
socket->write(buffer);
ui->lineEdit->clear();
}
void AsiXoxe_ui::readMsg()
{
thread = new QThread();
ui->textBrowser->append(socket->readAll());
socket->flush();
socket->waitForBytesWritten(3000);
socket->moveToThread(thread);
thread->start();
}
void AsiXoxe_ui::on_lineEdit_returnPressed()
{
sendMsg();
}
void AsiXoxe_ui::connectedMsg(QString c)
{
}
void AsiXoxe_ui::connectedMsg_2()
{
}
void AsiXoxe_ui::on_pushButton_2_clicked()
{
nickName = QInputDialog::getText(this, "Name input," "Name or Nickname", 0);
}
To copy to clipboard, switch view to plain text mode
#include <QtGui/QApplication>
#include "asixoxe_ui.h"
int main(int argc, char *argv[])
{
AsiXoxe_ui w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "asixoxe_ui.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AsiXoxe_ui w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks