Results 1 to 4 of 4

Thread: Client/server application not woirking properly

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Client/server application not woirking 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.
    Qt Code:
    1. #ifndef MYSOCKET_H
    2. #define MYSOCKET_H
    3.  
    4. #include <QTcpSocket>
    5. #include <QThread>
    6.  
    7. class MySocket : public QTcpSocket
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit MySocket(int descriptor);
    12. void connectThread(QThread &thread);
    13. signals:
    14. void sendData(QByteArray);
    15. public slots:
    16. void setupSocket();
    17. void readSocket();
    18. void writeSocket(QByteArray);
    19. void disconnected();
    20. void connecte_d();
    21. private:
    22. int socketDescriptor;
    23. };
    24.  
    25. #endif // MYSOCKET_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mysocket.h"
    2.  
    3. MySocket::MySocket(int descriptor) :socketDescriptor(descriptor)
    4. {
    5. this->setSocketDescriptor(descriptor);
    6. }
    7. void MySocket::connectThread(QThread &thread)
    8. {
    9. connect(&thread, SIGNAL(started()), this, SLOT(setupSocket()));
    10. }
    11. void MySocket::setupSocket()
    12. {
    13. connect(this, SIGNAL(readyRead()), this, SLOT(readSocket()));
    14. connect(this, SIGNAL(connected()), this, SLOT(connecte_d()));
    15. connect(this, SIGNAL(disconnected()), this, SLOT(disconnected()));
    16. }
    17. void MySocket::readSocket()
    18. {
    19. QByteArray buffer;
    20. buffer.clear();
    21. buffer.append(this->readAll());
    22. qDebug() << buffer;
    23. emit sendData(buffer);
    24. }
    25. void MySocket::writeSocket(QByteArray data)
    26. {
    27. this->write(data);
    28. }
    29. void MySocket::connecte_d()
    30. {
    31. qDebug() << socketDescriptor << " connected";
    32. }
    33. void MySocket::disconnected()
    34. {
    35. qDebug() << socketDescriptor << " Disconnected";
    36. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MSEVER_H
    2. #define MSEVER_H
    3.  
    4. #include <QTcpServer>
    5. #include <QDebug>
    6. #include <QThreadPool>
    7. #include <QList>
    8. #include <QString>
    9. #include <QThread>
    10. #include <QTcpSocket>
    11. #include <QHash>
    12. #include "mysocket.h"
    13.  
    14. class MServer : public QTcpServer
    15. {
    16. Q_OBJECT
    17. public:
    18. explicit MServer(QObject *parent = 0);
    19. ~MServer();
    20.  
    21. signals:
    22. void sendD(QByteArray);
    23. public slots:
    24. void dataRec(QByteArray);
    25. protected:
    26. void incomingConnection(int handle);
    27. private:
    28. MySocket* mSocket;
    29. QList<QThread*>* myThreads;
    30. QThreadPool* threadPool;
    31. static int counter;
    32. };
    33.  
    34. #endif // MSEVER_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "msever.h"
    2. int MServer::counter = 0;
    3. MServer::MServer(QObject *parent) :
    4. QTcpServer(parent)
    5. {
    6. if(this->listen(QHostAddress::Any, 1234))
    7. qDebug() << "Listening...";
    8. else
    9. qDebug() << "Server failed to start";
    10. myThreads = new QList<QThread*>();
    11. for(int i = 0; i <= 5; i++)
    12. {
    13. QThread* thread = new QThread();
    14. myThreads->append(thread);
    15. }
    16. }
    17. MServer::~MServer()
    18. {
    19. for(int i = 0; i <= 5; i++)
    20. myThreads->at(i)->deleteLater();
    21. }
    22. void MServer::incomingConnection(int handle)
    23. {
    24. qDebug() << handle << " Connected";
    25. mSocket = new MySocket(handle);
    26. mSocket->writeSocket("Welcome to Ayanda's Server");
    27.  
    28. connect(mSocket, SIGNAL(sendData(QByteArray)), this, SLOT(dataRec(QByteArray)));
    29. connect(this, SIGNAL(sendD(QByteArray)), mSocket, SLOT(writeSocket(QByteArray)));
    30.  
    31. mSocket->connectThread(*(myThreads->at(counter)));
    32. mSocket->moveToThread(myThreads->at(counter));
    33. myThreads->at(counter)->start();
    34. counter++;
    35. }
    36. void MServer::dataRec(QByteArray data)
    37. {
    38. emit sendD(data);
    39. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "msever.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QCoreApplication a(argc, argv);
    6. MServer Server;
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    And the following code is for my client program.
    Qt Code:
    1. #ifndef ASIXOXE_UI_H
    2. #define ASIXOXE_UI_H
    3.  
    4. #include <QMainWindow>
    5. #include <QUdpSocket>
    6. #include <QTcpSocket>
    7. #include <QThread>
    8. #include <QInputDialog>
    9. #include <QString>
    10.  
    11. namespace Ui {
    12. class AsiXoxe_ui;
    13. }
    14.  
    15. class AsiXoxe_ui : public QMainWindow
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit AsiXoxe_ui(QWidget *parent = 0);
    21. ~AsiXoxe_ui();
    22. signals:
    23. void sendMsg(QString);
    24. public slots:
    25. void displayMsg(QByteArray);
    26. void readMsg();
    27. void sendMsg();
    28. void connectedMsg(QString);
    29. void connectedMsg_2();
    30. private:
    31. Ui::AsiXoxe_ui *ui;
    32. QTcpSocket* socket;
    33. QThread* thread;
    34. QString nickName;
    35.  
    36. private slots:
    37. void on_pushButton_2_clicked();
    38. void on_lineEdit_returnPressed();
    39. void on_pushButton_clicked();
    40. };
    41.  
    42. #endif // ASIXOXE_UI_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "asixoxe_ui.h"
    2. #include "ui_asixoxe_ui.h"
    3.  
    4. AsiXoxe_ui::AsiXoxe_ui(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::AsiXoxe_ui)
    7. {
    8. ui->setupUi(this);
    9. socket = new QTcpSocket(this);
    10. QHostAddress hAddr;
    11. hAddr.setAddress("192.168.43.78");
    12. socket->connectToHost(hAddr, 1234);
    13. socket->waitForConnected(30000);
    14.  
    15. connect(socket, SIGNAL(readyRead()), this, SLOT(readMsg()));
    16. connect(socket, SIGNAL(connected(QString)),this, SLOT(connectedMsg(QString)));
    17. }
    18.  
    19. AsiXoxe_ui::~AsiXoxe_ui()
    20. {
    21. delete ui;
    22. thread->deleteLater();
    23. }
    24.  
    25. void AsiXoxe_ui::displayMsg(QByteArray sentMsg)
    26. {
    27. QString msg = QString(sentMsg.data());
    28. ui->textBrowser->append(msg);
    29. }
    30.  
    31. void AsiXoxe_ui::on_pushButton_clicked()
    32. {
    33. sendMsg();
    34.  
    35. }
    36.  
    37. void AsiXoxe_ui::sendMsg()
    38. {
    39. QByteArray buffer;
    40. buffer.append(nickName);
    41. buffer.append(": ");
    42. buffer.append(ui->lineEdit->text());
    43. socket->write(buffer);
    44. ui->lineEdit->clear();
    45. }
    46.  
    47. void AsiXoxe_ui::readMsg()
    48. {
    49. thread = new QThread();
    50. ui->textBrowser->append(socket->readAll());
    51. socket->flush();
    52. socket->waitForBytesWritten(3000);
    53.  
    54. socket->moveToThread(thread);
    55. thread->start();
    56. }
    57.  
    58. void AsiXoxe_ui::on_lineEdit_returnPressed()
    59. {
    60. sendMsg();
    61. }
    62.  
    63. void AsiXoxe_ui::connectedMsg(QString c)
    64. {
    65. }
    66.  
    67. void AsiXoxe_ui::connectedMsg_2()
    68. {
    69. }
    70.  
    71. void AsiXoxe_ui::on_pushButton_2_clicked()
    72. {
    73. nickName = QInputDialog::getText(this, "Name input," "Name or Nickname", 0);
    74. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "asixoxe_ui.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. AsiXoxe_ui w;
    8.  
    9. w.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Client/server application not woirking properly

    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?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Client/server application not woirking properly

    Quote Originally Posted by ChrisW67 View Post
    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2013
    Location
    Sofia, Bulgaria
    Posts
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Client/server application not woirking properly

    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?

  5. The following user says thank you to stoyanps for this useful post:


Similar Threads

  1. Replies: 4
    Last Post: 19th November 2012, 14:35
  2. Client/server application
    By sali555 in forum Qt Programming
    Replies: 5
    Last Post: 22nd April 2011, 09:55
  3. problem with client/server application
    By marco.stanzani in forum Newbie
    Replies: 1
    Last Post: 9th April 2011, 07:36
  4. TCP Client-Server Application
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2008, 13:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.