Results 1 to 19 of 19

Thread: Qt Network Chat

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt Network Chat

    Dear All,

    I'm new to Qt. I have installed Qt version 4.6.1 in my Windows PC. I need to develop a application, where I need to chat as well as I need to update some info in some Lineedits in my network connected PC. Initially I need to develop a simple chat window where I can send msgs to my peer PC. How this can be done.please support me..

    Thanks in advance

  2. #2
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Network Chat


  3. #3
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Network Chat

    thanks for fast reply.. I have gone thru the example.. Please can u suggest me code with simple comments or article. Where I can understand what is Socket programming in Qt. what are the procedures in code to be written in Prog to develop my own applications. It will be helpfull for me, If im getting any doc with how socket programming can be implemented in Qt.

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Network Chat

    . Please can u suggest me code with simple comments or article. Where I can understand what is Socket programming in Qt
    use these simple client/server program in qt
    http://harmattan-dev.nokia.com/docs/...uneserver.html
    http://harmattan-dev.nokia.com/docs/...uneclient.html
    more simple than others for beginner ..
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Network Chat

    Hi wagmare...

    The site/link which u have sent is down. Its not accessible. Kindly suggest alternate way to get the same page or code.

  6. #6
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Network Chat

    yeah me too .. yesterday it was working! .. some problem with the doc.trolltech server i guess ..
    go to ur qt-assistant and search for
    Fortune Server Example and Fortune Client Example
    "Behind every great fortune lies a crime" - Balzac

  7. #7
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Network Chat

    Hi Wagmare..
    Thanks for your reply. I have tried out a simple code where it sends simple qDebug msgs to compile output.I'm attaching code here.please go thru this and let me know.Im running a server code and client code at a time on same PC, my PC is connected to LAN(192.168.64.55). The moment I run client code. New connection is getting added and its printing qDebug msgs. please go thru the code. I feel problem may be at two points .one is at declaration socket QHostAddress and next is at main.cpp file where instead of default w.show(), listen()/connect to server().

    TCPSERVER main.cpp
    #include <QtGui/QApplication>
    #include "server.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    server w;
    //w.show();
    w.listen();

    return a.exec();
    }


    TCPSERVER server.cpp
    #include "server.h"
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <cstdio>

    server::server(QWidget *parent)
    : QMainWindow(parent)
    {
    server1 = new QTcpServer(this);
    connect(server1, SIGNAL(newConnection()),
    this, SLOT(on_newConnection()));
    qDebug("new socket created");

    }

    server::~server()
    {

    }
    void server::listen()
    {
    server1->listen(QHostAddress::LocalHost, 1234);
    qDebug("Listening for new connection");

    }
    void server:n_newConnection()
    {
    socket = server1->nextPendingConnection();
    qDebug("new connection established");
    if(socket->state() == QTcpSocket::ConnectedState)
    {
    printf("New connection established.\n");
    }
    connect(socket, SIGNAL(disconnected()),
    this, SLOT(on_disconnected()));
    connect(socket, SIGNAL(readyRead()),
    this, SLOT(on_readyRead()));
    }
    void server:n_readyRead()
    {
    while(socket->canReadLine())
    {
    QByteArray ba = socket->readLine();
    if(strcmp(ba.constData(), "!exit\n") == 0)
    {
    socket->disconnectFromHost();
    break;
    }
    printf(">> %s", ba.constData());
    }
    }
    void server:n_disconnected()
    {
    printf("Connection disconnected.\n");
    disconnect(socket, SIGNAL(disconnected()));
    disconnect(socket, SIGNAL(readyRead()));
    socket->deleteLater();
    }


    TCPSERVER server.h
    #ifndef SERVER_H
    #define SERVER_H

    #include <QtGui/QMainWindow>
    #include <QObject>

    class QTcpServer;
    class QTcpSocket;

    class server : public QMainWindow
    {
    Q_OBJECT

    public:
    server(QWidget *parent = 0);
    void listen();
    ~server();
    public slots:
    void on_newConnection();
    void on_readyRead();
    void on_disconnected();

    private:
    QTcpServer *server1;
    QTcpSocket *socket;
    };

    #endif // SERVER_H

    Hi Wagmare..
    Thanks for your reply. I have tried out a simple code where it sends simple qDebug msgs to compile output.I'm attaching code here.please go thru this and let me know.Im running a server code and client code at a time on same PC, my PC is connected to LAN(192.168.64.55). The moment I run client code. New connection is getting added and its printing qDebug msgs. please go thru the code. I feel problem may be at two points .one is at declaration socket QHostAddress and next is at main.cpp file where instead of default w.show(), listen()/connect to server().
    TCPCLIENT main.cpp
    #include <QtGui/QApplication>
    #include "client.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    client w;
    //w.show();
    w.connectToserver();
    return a.exec();
    }


    TCPCLIENT client.cpp

    #include "client.h"
    #include <QTcpSocket>
    #include <QHostAddress>
    #include <cstdio>


    client::client(QWidget *parent)
    : QMainWindow(parent)
    {
    socket = new QTcpSocket(this);
    connect(socket, SIGNAL(connected()),
    this, SLOT(on_connected()));
    }

    client::~client()
    {

    }
    void client::connectToserver()
    {
    socket->connectToHost(QHostAddress::LocalHost, 1234);
    qDebug("START PACKET SENDING");
    }

    void client:n_connected()
    {
    printf("Connection established.\n");
    char buffer[1024];
    forever
    {
    printf(">> ");
    gets(buffer);
    int len = strlen(buffer);
    buffer[len] = '\n';
    buffer[len+1] = '\0';
    socket->write(buffer);
    socket->flush();
    }
    }


    TCPCLIENT client.h
    #ifndef CLIENT_H
    #define CLIENT_H

    #include <QtGui/QMainWindow>
    #include <QObject>

    class QTcpSocket;

    class client : public QMainWindow
    {
    Q_OBJECT

    public:
    client(QWidget *parent = 0);
    void connectToserver();
    ~client();

    private:
    QTcpSocket *socket;
    public slots:
    void on_connected();

    };

    #endif // CLIENT_H

  8. #8
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Network Chat

    sow ur .pro file .. did u added QT += network in it ..?
    "Behind every great fortune lies a crime" - Balzac

  9. #9
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Network Chat

    Yes Wagmare. I have added initially only when creating the project. I'm posting the code of the Both files . I feel, problem may be the code or bcoz of I'm running on the LAN. I'm not sure of this. IP address QHostaddress also.
    #-------------------------------------------------
    #
    # Project created by QtCreator 2013-09-25T13:28:55
    #
    #-------------------------------------------------

    QT += network

    TARGET = TcpServer
    TEMPLATE = app


    SOURCES += main.cpp\
    server.cpp

    HEADERS += server.h


    #-------------------------------------------------
    #
    # Project created by QtCreator 2013-09-25T17:12:05
    #
    #-------------------------------------------------

    QT += network

    TARGET = TcpClient
    TEMPLATE = app


    SOURCES += main.cpp\
    client.cpp

    HEADERS += client.h
    Last edited by Vivek1982; 26th September 2013 at 12:29. Reason: removed sensitive data

  10. #10
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Network Chat

    for u

    in .h

    Qt Code:
    1. #include <QWidget>
    2. #include <QLineEdit>
    3. #include <QPushButton>
    4.  
    5. class QTcpSocket;
    6.  
    7. class TCPClient : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. TCPClient(QWidget *parent =0);
    13.  
    14.  
    15. private slots:
    16. void on_connected();
    17. void connectToServer();
    18. void writeMessage();
    19.  
    20.  
    21. private:
    22. QLineEdit *m_lineEdit;
    23. QPushButton *m_button;
    24. QPushButton *m_sendButton;
    25. QTcpSocket *m_socket;
    26. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2. #include <QTcpSocket>
    3. #include <QHostAddress>
    4. #include <QDebug>
    5.  
    6. #include "TCPClient.h"
    7.  
    8. TCPClient::TCPClient(QWidget *parent)
    9. : QWidget(parent)
    10. {
    11. m_lineEdit = new QLineEdit();
    12. m_button = new QPushButton("Connect");
    13. m_sendButton = new QPushButton("Send");
    14. m_sendButton->setEnabled(false);
    15. connect(m_button,SIGNAL(clicked()), this,SLOT(connectToServer()));
    16. connect(m_sendButton, SIGNAL(clicked()), this, SLOT(writeMessage()));
    17. m_socket = new QTcpSocket;
    18. connect(m_socket, SIGNAL(connected()), this, SLOT(on_connected()));
    19.  
    20. QHBoxLayout *hLayout = new QHBoxLayout;
    21. hLayout->addWidget(m_button);
    22. hLayout->addWidget(m_sendButton);
    23.  
    24. QVBoxLayout *mLayout = new QVBoxLayout;
    25. mLayout->addWidget(m_lineEdit);
    26. mLayout->addLayout(hLayout);
    27.  
    28. this->setLayout(mLayout);
    29. }
    30.  
    31. void
    32. TCPClient::on_connected()
    33. {
    34. qDebug()<<"Debug Connection established:";
    35. m_sendButton->setEnabled(true);
    36.  
    37.  
    38. }
    39.  
    40. void
    41. TCPClient::writeMessage()
    42. {
    43. QString writeData = this->m_lineEdit->text();
    44.  
    45. m_socket->write(writeData.toStdString().c_str());
    46. m_socket->flush();
    47. }
    48.  
    49.  
    50. void
    51. TCPClient::connectToServer()
    52. {
    53. m_socket->connectToHost(QHostAddress::LocalHost, 1234);
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 

    and main
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "TCPClient.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. TCPClient c;
    9.  
    10. c.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    use this client ..
    "Behind every great fortune lies a crime" - Balzac

  11. #11
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Network Chat

    Hi. wagmare.. Thanks for the code.. I will try in my PC and let you know the result. Any changes to made in TCP server.h/cpp/main.cpp or let that remain same as mentioned above discussions.

    Thanks alot wagmare

Similar Threads

  1. voice chat via network
    By Con Nít in forum Qt Programming
    Replies: 3
    Last Post: 1st February 2011, 19:55
  2. Specialised Network Chat Application
    By TropicalPenguin in forum Jobs
    Replies: 0
    Last Post: 28th September 2010, 22:40
  3. Network chat system with pop-up window
    By kernel.roy in forum Qt Tools
    Replies: 1
    Last Post: 9th September 2010, 12:36
  4. Network chat system with pop-up window
    By kernel.roy in forum Newbie
    Replies: 1
    Last Post: 8th September 2010, 13:41
  5. Network chat system with pop-up window
    By kernel.roy in forum Qt Programming
    Replies: 0
    Last Post: 8th September 2010, 13:38

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.