Results 1 to 3 of 3

Thread: Some issues with chat program

  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Some issues with chat program

    I created a simple chat program based in client/server model using TCP of course. I have some issues starting from server ending to client, here is the list:
    The server can't bind in specified address or port, I used lineEdit widgets to that, i tried to convert to int, but it doesn't work.
    The server can send messages but it can't receive messages, even peer information like address.
    If I clicked in connectButton widget, the client crash(I didn't test other functionality because the client crash before I did).
    chat.tar.bz2

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Some issues with chat program

    Hi SIFE, here you go.

    You have two main problems in you code (both in server and client), but don't worry you have done most of it, all the problems are small, may be you just need figure things out

    1. Both in server and client project, the "GuiClient", and "GuiServer" classes, are multiple inherited from QMainWindow, and Ui:GuiServer/Ui::GuiClient. You also have a member variable as Ui:GuiServer*/Ui::GuiClient* ui. This is not good, you already have ui instance from the inherited call Ui:GuiServer/Ui::GuiClient

    Fix: You either have Ui:GuiServer/Ui::GuiClien as a base class (or) have ui as a member variable, but not both. This was the main problem. You tried to do a new Ui:GuiServer/Ui::GuiClien in the constructor, and then use the setupUi(), which is actually calling the base class method (you should be calling ui->setupUi(this)).

    So just have only one the things, having both is not good.

    2. In server code, you are trying to override (re-implement) incomingConnection() (which is a virtual member of QTcpServer), this will not work as GuiServer is not a sub-class of QTcpServer.

    Fix: The better way would be to make incomingConnection(), as a slot in GuiServer, and connect server.newConnection() signal to this.incomingConnection(), as shown below, also modify the GuiServer::incomingConnection() accordingly.

    Qt Code:
    1. void GuiServer::startServer()
    2. {
    3. //bool stat = server->listen(QHostAddress::Any, quint16(portEdit->text().toUShort()));
    4. bool stat = server->listen(QHostAddress::Any, 5000);
    5. qDebug("%d", stat);
    6. /* if(!stat)
    7.   QMessageBox::critical(this, "Error to bind port", "Fail to listen");
    8.   else
    9.   {
    10.   startButton->setEnabled(false);
    11.   stopButton->setEnabled(false);
    12.   }
    13. */
    14. connect(server, SIGNAL(newConnection()), this, SLOT(incomingConnection())); //Added
    15. }
    16.  
    17. void GuiServer::incomingConnection(void)
    18. {
    19. QTcpSocket *client = server->nextPendingConnection(); //modified
    20.  
    21. qDebug() << "New client from:" << client->peerAddress().toString();
    22.  
    23. connect(client, SIGNAL(readyRead()), this, SLOT(readMsg()));
    24. connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMsg()));
    25. connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
    26. }
    To copy to clipboard, switch view to plain text mode 

    With these changes, I was able to send text form client to server. I guess you can carry on from here

    Good Luck.

  3. #3
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Some issues with chat program

    Now my problem solved.

Similar Threads

  1. Simple chat
    By kernel.roy in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2010, 11:41
  2. Simple chat
    By kernel.roy in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2010, 05:37
  3. Simple Chat example
    By Misenko in forum Qt Programming
    Replies: 3
    Last Post: 5th July 2008, 19:19
  4. TCP Chat Problem
    By cooler123 in forum Qt Programming
    Replies: 2
    Last Post: 27th September 2007, 06:47
  5. Chat client!!!!
    By galleeg in forum Qt Tools
    Replies: 1
    Last Post: 16th May 2006, 09:05

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.