CLIENT/SERVER APPLICATION NOT WORKING PROPERLY

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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";
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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);
}




[code]#include <QtCore/QCoreApplication>
#include "msever.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MServer Server;
return a.exec();
}
[
Last edited on
"192.168.43.78" ← This is address belonging to local network, not internet.
http://en.wikipedia.org/wiki/IP_address#IPv4_private_addresses
Last edited on
I thought that was the problem, but now how do I fix this, because I got the above IP address from the "ipconfig" command. So which IP address identifies my server outside the local network and how do I find it?
http://www.whatismyip.com/
You will probably need to enable port forwarding on your router for your program to work.
but I'm using my cell as a modem to the internet. so how would port forwarding be handled in that case?
Last edited on
Server should have white (static) IP to work. If you got "proxy detected" on whatismyip, you certainly have gray (dynamic) IP.
Some cellphone providers can provide you with static IP for extra cost.
Last edited on
It has more to do with being behind a NAT than having a dynamic IP.
Topic archived. No new replies allowed.