PDA

View Full Version : Client/server application not woirking properly



ayanda83
16th May 2013, 18:03
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>

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

#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";
}

#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

#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);
}





#include <QtCore/QCoreApplication>
#include "msever.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MServer Server;
return a.exec();
}
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;
}

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

#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);
}

#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();
}

ChrisW67
17th May 2013, 00:22
:rolleyes: Another one using threads to drive networking.

So, it works when connected locally but doesn't work when separated by the Internet. Doesn't that tell you the problem is the network?

What have you done to diagnose the problem?
Have you eliminated firewalls and NAT?
Are you listening on public IP addresses?
Are you connecting to public IP addresses?

wysota
17th May 2013, 07:31
:rolleyes: Another one using threads to drive networking.
Yeah... and five of them :)

I wonder what happens if a sixth client connects to this program (once OP manages to get the "cannot connect" thing solved).

stoyanps
17th May 2013, 14:13
I noticed that you wrote IP address of the server as constant in the client code. So if you are starting server on other computer with different IP address, how do you expect to connect?