Results 1 to 19 of 19

Thread: Qt Network Chat

  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

  12. #12
    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

    Any changes to made in TCP server.h/cpp/main.cpp or let that remain same as mentioned above discussions.
    no changes required in server or main
    but in client.cpp TCPClient::writeMessage() function im not converting QString to const char * properly ..
    use
    const char * data = writeData .toUtf8().constData();
    to convert it properly .
    "Behind every great fortune lies a crime" - Balzac

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

    Default Re: Qt Network Chat

    okay I will check it. Actually my few queries for you is. Please if possible answer.
    1. In a network(Office LAN), can these two client and server program can run on same machine.
    2. QHostaddress::Localhost this statement will call for IP address 127.0.0.1. how to use this.
    3. In my PC IP address will taken automatically bcoz Im connected in office LAN.Any changes to be made...
    4.Finally last query is on server side.. New connection found(), accept connection(), readyread(),write() delete()
    clientside.. connecttoserver() writemsg(). these two type is enough..................

    Any other general info required to Socket programming please let me know, where I can implement?


    Added after 1 45 minutes:


    Hi wagmare.. I have tried that example. As usual we have kept a qDebug msg in client program that is not executed. I didnt make any changes in the server directly. I started Server program next client program. GUI came no problem in that.but application window gave this msgs.
    Client Application output
    Starting C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe...
    QMetaObject::connectSlotsByName: No matching signal for on_connected()
    QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "127.0.0.1"
    QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "127.0.0.1"
    C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe exited with code 1
    Starting C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe...
    Cannot retrieve debugging output!
    C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe exited with code 0
    Starting C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe...
    QMetaObject::connectSlotsByName: No matching signal for on_connected()
    C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe exited with code 1
    Starting C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe...
    Cannot retrieve debugging output!
    C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe exited with code 0
    Starting C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe...
    Cannot retrieve debugging output!
    C:\Users\398292\Desktop\TCPClient\debug\TCPClient. exe exited with code 0
    Last edited by Vivek1982; 27th September 2013 at 07:26.

  14. #14
    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.. I have tried enough times same sample code, what was shared by you. I had created full new project of TCPServer/Client. Still inmy LAN its not workin or in my same PC also. Any problem in the code. please review once, I need to understand where my code is going wrong.
    TCP CLIENT main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "tcpclient.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. TCPClient w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    TCP Client.h
    Qt Code:
    1. #ifndef TCPCLIENT_H
    2. #define TCPCLIENT_H
    3.  
    4. #include <QWidget>
    5. #include <QPushButton>
    6. #include <QLineEdit>
    7. #include <QTcpSocket>
    8.  
    9. class QTcpSocket;
    10.  
    11. namespace Ui {
    12. class TCPClient;
    13. }
    14.  
    15. class TCPClient : public QWidget {
    16. Q_OBJECT
    17. public:
    18. TCPClient(QWidget *parent = 0);
    19. ~TCPClient();
    20.  
    21. protected:
    22. void changeEvent(QEvent *e);
    23.  
    24. private:
    25. Ui::TCPClient *ui;
    26. QLineEdit *le;
    27. QPushButton *pb_connect;
    28. QPushButton *pb_send;
    29. QTcpSocket *socket;
    30.  
    31. private slots:
    32. void connecttoserver();
    33. void on_connected();
    34. void write();
    35.  
    36. };
    37.  
    38. #endif // TCPCLIENT_H
    To copy to clipboard, switch view to plain text mode 

    TCPClient.cpp

    Qt Code:
    1. #include "tcpclient.h"
    2. #include "ui_tcpclient.h"
    3. #include <QtGui>
    4. #include <QTcpSocket>
    5. #include <QHostAddress>
    6. #include <QDebug>
    7.  
    8.  
    9. TCPClient::TCPClient(QWidget *parent) :
    10. QWidget(parent),
    11. ui(new Ui::TCPClient)
    12. {
    13. ui->setupUi(this);
    14.  
    15. le= new QLineEdit;
    16. le=ui->lineEdit;
    17.  
    18. pb_connect=new QPushButton;
    19. pb_connect=ui->pushButton;
    20. pb_connect->setText("CONNECT");
    21. pb_connect->setEnabled(true);
    22.  
    23. pb_send=new QPushButton;
    24. pb_send=ui->pushButton_2;
    25. pb_send->setText("Send");
    26. pb_send->setEnabled(false);
    27.  
    28. socket=new QTcpSocket(this);
    29.  
    30. connect(pb_connect,SIGNAL(clicked()),this,SLOT(connecttoserver()));
    31. connect(pb_send,SIGNAL(clicked()),this,SLOT(write()));
    32. connect(socket,SIGNAL(connected()),this,SLOT(on_connected()));
    33.  
    34.  
    35. }
    36.  
    37. TCPClient::~TCPClient()
    38. {
    39. delete ui;
    40. }
    41.  
    42. void TCPClient::changeEvent(QEvent *e)
    43. {
    44. QWidget::changeEvent(e);
    45. switch (e->type()) {
    46. case QEvent::LanguageChange:
    47. ui->retranslateUi(this);
    48. break;
    49. default:
    50. break;
    51. }
    52. }
    53.  
    54. void TCPClient::connecttoserver()
    55. {
    56. //QHostAddress hAddr;
    57. // hAddr.setAddress("192.168.64.55");
    58.  
    59. socket->connectToHost(QHostAddress::Any,1234);
    60.  
    61.  
    62. }
    63.  
    64. void TCPClient::on_connected()
    65. {
    66. pb_connect->setEnabled(false);
    67. pb_send->setEnabled(true);
    68. qDebug("Connection established");
    69. }
    70.  
    71. void TCPClient::write()
    72. {
    73. QString data=this->le->text();
    74. socket->write(data.toUtf8().constData());
    75. //const char * data = writeData .toUtf8().constData();
    76.  
    77. socket->flush();
    78. }
    To copy to clipboard, switch view to plain text mode 


    TCP Client.pro
    qmake Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2013-09-27T10:57:57
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += network
    8.  
    9. TARGET = TCPClient
    10. TEMPLATE = app
    11.  
    12.  
    13. SOURCES += main.cpp\
    14. tcpclient.cpp
    15.  
    16. HEADERS += tcpclient.h
    17.  
    18. FORMS += tcpclient.ui
    To copy to clipboard, switch view to plain text mode 

    ================================================== ===================================

    TCP Server.main.cpp

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


    TCP SERVER.h

    Qt Code:
    1. #ifndef TCPSERVER_H
    2. #define TCPSERVER_H
    3.  
    4. #include <QWidget>
    5. #include <QPushButton>
    6. #include <QLineEdit>
    7. #include <QTcpServer>
    8. #include <QTcpSocket>
    9.  
    10. class QTcpServer;
    11. class QTcpSocket;
    12.  
    13. namespace Ui {
    14. class Tcpserver;
    15. }
    16.  
    17. class Tcpserver : public QWidget {
    18. Q_OBJECT
    19. public:
    20. Tcpserver(QWidget *parent = 0);
    21. ~Tcpserver();
    22. void listen();
    23.  
    24. protected:
    25. void changeEvent(QEvent *e);
    26.  
    27. private:
    28. Ui::Tcpserver *ui;
    29. QLineEdit *le;
    30. QPushButton *Pb_newconn;
    31. QPushButton *Pb_send;
    32. QTcpServer *server;
    33. QTcpSocket *socket;
    34.  
    35. private slots:
    36.  
    37. void on_newconn();
    38. void read_socket();
    39. void socket_disconnected();
    40.  
    41. };
    42.  
    43.  
    44. #endif // TCPSERVER_H
    To copy to clipboard, switch view to plain text mode 

    TCP SERVER.cpp

    Qt Code:
    1. #include "tcpserver.h"
    2. #include "ui_tcpserver.h"
    3.  
    4. Tcpserver::Tcpserver(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Tcpserver)
    7. {
    8. ui->setupUi(this);
    9.  
    10. le=new QLineEdit;
    11. le=ui->lineEdit;
    12.  
    13. Pb_newconn= new QPushButton;
    14. Pb_newconn=ui->pushButton;
    15. Pb_newconn->setText("Listen");
    16. Pb_newconn->setEnabled(true);
    17.  
    18. Pb_send=new QPushButton;
    19. Pb_send=ui->pushButton_2;
    20. Pb_send->setText("SEND");
    21. Pb_send->setEnabled(false);
    22.  
    23. server=new QTcpServer(this);
    24.  
    25. socket= new QTcpSocket(this);
    26.  
    27. //connect(Pb_newconn,SIGNAL(clicked()),this,SLOT(listen()));
    28. connect(server,SIGNAL(newConnection()),this,SLOT(on_newconn()));
    29. connect(socket,SIGNAL(readyRead()),this,SLOT(read_socket()));
    30. connect(socket,SIGNAL(disconnected()),this,SLOT(socket_disconnected()));
    31. }
    32.  
    33. Tcpserver::~Tcpserver()
    34. {
    35. delete ui;
    36. }
    37.  
    38. void Tcpserver::changeEvent(QEvent *e)
    39. {
    40. QWidget::changeEvent(e);
    41. switch (e->type()) {
    42. case QEvent::LanguageChange:
    43. ui->retranslateUi(this);
    44. break;
    45. default:
    46. break;
    47. }
    48. }
    49.  
    50. void Tcpserver::listen()
    51. {
    52. //QHostAddress hAddr;
    53. //hAddr.setAddress("192.168.64.52");
    54. server->listen(QHostAddress::Any,1234);
    55. qDebug("Listening to new connection");
    56. }
    57. void Tcpserver::on_newconn()
    58. {
    59. socket=server->nextPendingConnection();
    60. if(socket->state()==QTcpSocket::ConnectedState)
    61. {
    62. qDebug("new connection established");
    63. }
    64. Pb_newconn->setEnabled(false);
    65. Pb_send->setEnabled(true);
    66. }
    67. void Tcpserver::read_socket()
    68. {
    69. while(socket->canReadLine())
    70. {
    71. QByteArray buffer= socket->readLine();
    72. if(strcmp(buffer.constData(), "!exit\n") == 0)
    73. {
    74. qDebug("No data");
    75. }
    76. else
    77. le->setText(buffer);
    78.  
    79. }
    80.  
    81. }
    82. void Tcpserver::socket_disconnected()
    83. {
    84. qDebug("Socket Disconnected");
    85. socket->deleteLater();
    86. }
    To copy to clipboard, switch view to plain text mode 


    TCP SERVER.pro
    qmake Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2013-09-27T13:50:57
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += network
    8.  
    9. TARGET = TCPServer
    10. TEMPLATE = app
    11.  
    12.  
    13. SOURCES += main.cpp\
    14. tcpserver.cpp
    15.  
    16. HEADERS += tcpserver.h
    17.  
    18. FORMS += tcpserver.ui
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 29th September 2013 at 23:06. Reason: missing [code] tags

  15. #15
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt Network Chat

    @Vivek1982 use code tags please for better reading and viewing.

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

    Thumbs up Re: Qt Network Chat

    Hi.. Toufic.dbouk.. Thanks for reply and giving idea of using Quotes. I was not knowing how to use it.. .
    I have gone thru the page of BB code. I start posting comments/discussions/code in the right way.... thanks


    Added after 14 minutes:


    Hi Wagmare. thanks for all your suggestions and comments. I have created now chatting window on my PC. The problem earlier was data conversion and calling the functions at proper procedure. I had kept lot Qdebug msgs then, i got to know wat was the error. now I have created simple chat window in server/client GUI. In my PC if I'm running both server/client applications I can chat. If I run server in one and client in two different PC. I'm not getting it. Any issues in the QHostAddress::... How to use class in LAN connected PCs..

    Thanks in Advance
    Last edited by Vivek1982; 30th September 2013 at 05:05.

  17. #17
    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

    be sure with these things

    address is ur server address and port ur valid server port no
    Qt Code:
    1. QHostAddress addr(address);
    2. bool rval = client.reset();
    3. qDebug() << "Reset before Connect to Host = " << rval;
    4. socket.connectToHost(address, port);
    To copy to clipboard, switch view to plain text mode 
    Last edited by wagmare; 30th September 2013 at 08:36.
    "Behind every great fortune lies a crime" - Balzac

  18. #18
    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.. I was able to communicate between two PCs connected in LAN. In client I had mentioned
    Qt Code:
    1. void TCPClient::connecttoserver()
    2. {
    3. QHostAddress hAddr;
    4. hAddr.setAddress("192.168.64.55");
    5. socket->connectToHost(hAddr,1234);
    6. // socket->connectToHost("192.168.64.52",1234);
    7. }
    To copy to clipboard, switch view to plain text mode 

    In server I had mentioned these lines.
    Qt Code:
    1. void Tcpserver::listen()
    2. {
    3. server->listen(QHostAddress::Any,1234);
    4. qDebug("Listening to new connection");
    5. }
    To copy to clipboard, switch view to plain text mode 

    After compiling by these changes I was able to do chatting in LAN between two PCs server/client applications.
    Now, I need to have chat application and sending commands{encrypted codes} in the same socket. I was trying to create new socket and implement it in the same code but it was not possible. I wrote two sockets in both client/server application. program server application was hanging on PC, suggested to close bcoz of listening/connecting two times to the server.

    How to address this issue.Whether two or more sockets can be connected to same server to send different datas Or any tagging should be done before writing into socket such while decoding at remote end it will classify this info for whom.. means lineEdit1(chatting msg) or for LineEdit2(commands)..


    Thanks in advance.

  19. #19
    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.. I need to create a program for chatting and another lineedit to send specific commands, how it can be either by creating socket or tagging prior sending in same socket. please suggest in this, which is better. how it can addressed....

    Thanks in advance


    Added after 52 minutes:


    Hi.. wagmare any suggestion from you to how to address this...
    Last edited by Vivek1982; 1st October 2013 at 06:03.

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.