Results 1 to 5 of 5

Thread: Problem with simple TCP chat client-server

  1. #1
    Join Date
    Jan 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Problem with simple TCP chat client-server

    Hi, im new in QT. I have QT 5.5 and i create a very simple chat client-server based on TCP. But when i clicked button on server window to send message, application crashed. Client aplication doesn't get any errors, so i think it's working. I dont know how to fix it :/

    Thanks for any reply!!

    This is my code that i made:
    server.pro
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2016-01-17T12:45:39
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8. QT += network
    9. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    10.  
    11. TARGET = server
    12. TEMPLATE = app
    13.  
    14.  
    15. SOURCES += main.cpp\
    16. server.cpp
    17.  
    18. HEADERS += server.h
    19.  
    20. FORMS += server.ui
    To copy to clipboard, switch view to plain text mode 

    server.h
    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTcpServer>
    6. #include <QTcpSocket>
    7. #include <QHostAddress>
    8. #include <QList>
    9.  
    10. namespace Ui {
    11. class server;
    12. }
    13.  
    14. class server : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit server(QWidget *parent = 0);
    20. ~server();
    21.  
    22. public slots:
    23. void connect_new();
    24. void leer_socketclient();
    25.  
    26. private slots:
    27. void on_pushButton_clicked();
    28.  
    29. private:
    30. Ui::server *ui;
    31. QTcpServer *tcpserver;
    32. QTcpSocket *tcpclient;
    33. };
    34.  
    35. #endif // SERVER_H
    To copy to clipboard, switch view to plain text mode 

    server.cpp
    Qt Code:
    1. #include "server.h"
    2. #include "ui_server.h"
    3.  
    4. server::server(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::server)
    7. {
    8. ui->setupUi(this);
    9.  
    10. tcpserver = new QTcpServer (this);
    11. tcpserver->newConnection();
    12.  
    13. tcpserver->listen(QHostAddress::LocalHost, 1234);
    14. connect(tcpserver, SIGNAL(newConnection()), this, SLOT(connect_new()));
    15. }
    16.  
    17. server::~server()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void server::connect_new()
    23. {
    24. tcpclient = tcpserver->nextPendingConnection();
    25. connect(tcpclient, SIGNAL(readyRead()), this, SLOT(leer_socketclient()));
    26. }
    27.  
    28. void server::leer_socketclient()
    29. {
    30. QByteArray buffer;
    31. buffer.resize( tcpclient->bytesAvailable() );
    32. tcpclient->read( buffer.data(), buffer.size() );
    33. ui->plainTextEdit->setReadOnly( true );
    34. ui->plainTextEdit->appendPlainText( QString (buffer));
    35. }
    36.  
    37. void server::on_pushButton_clicked()
    38. {
    39. tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
    40. ui->lineEdit->clear();
    41. }
    To copy to clipboard, switch view to plain text mode 

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

    =========================================
    and client if you check

    client.pro
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2016-01-17T12:22:05
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8. QT += network
    9. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    10.  
    11. TARGET = client
    12. TEMPLATE = app
    13.  
    14.  
    15. SOURCES += main.cpp\
    16. client.cpp
    17.  
    18. HEADERS += client.h
    19.  
    20. FORMS += client.ui
    To copy to clipboard, switch view to plain text mode 

    client.h
    Qt Code:
    1. #ifndef CLIENT_H
    2. #define CLIENT_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTcpSocket>
    6. #include <QHostAddress>
    7. #include <QList>
    8.  
    9. namespace Ui {
    10. class client;
    11. }
    12.  
    13. class client : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit client(QWidget *parent = 0);
    19. ~client();
    20.  
    21.  
    22. private slots:
    23. void on_pushButton_clicked();
    24.  
    25. public slots:
    26. void leer_socketserver();
    27.  
    28. private:
    29. Ui::client *ui;
    30.  
    31. QTcpSocket *tcpclient;
    32. };
    33.  
    34. #endif // CLIENT_H
    To copy to clipboard, switch view to plain text mode 

    client.cpp
    Qt Code:
    1. #include "client.h"
    2. #include "ui_client.h"
    3.  
    4. client::client(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::client)
    7. {
    8. ui->setupUi(this);
    9.  
    10. tcpclient = new QTcpSocket (this);
    11.  
    12. tcpclient->connectToHost( QHostAddress::LocalHost , 1234);
    13.  
    14. connect (tcpclient, SIGNAL (readyRead()), this, SLOT (leer_socketserver()));
    15. }
    16.  
    17. client::~client()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void client::on_pushButton_clicked()
    23. {
    24. tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
    25. ui->lineEdit->clear();
    26. }
    27.  
    28. void client::leer_socketserver()
    29. {
    30. QByteArray buffer;
    31. buffer.resize( tcpclient->bytesAvailable() );
    32. tcpclient->read( buffer.data(), buffer.size() );
    33. ui->plainTextEdit->setReadOnly( true );
    34. ui->plainTextEdit->appendPlainText( QString (buffer));
    35. }
    To copy to clipboard, switch view to plain text mode 

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

    =========================
    server.ui
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>server</class>
    4. <widget class="QMainWindow" name="server">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>376</width>
    10. <height>335</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>server</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget">
    17. <widget class="QLineEdit" name="lineEdit">
    18. <property name="geometry">
    19. <rect>
    20. <x>100</x>
    21. <y>200</y>
    22. <width>261</width>
    23. <height>20</height>
    24. </rect>
    25. </property>
    26. </widget>
    27. <widget class="QLabel" name="label">
    28. <property name="geometry">
    29. <rect>
    30. <x>10</x>
    31. <y>200</y>
    32. <width>91</width>
    33. <height>16</height>
    34. </rect>
    35. </property>
    36. <property name="text">
    37. <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:11pt;&quot;&gt;Wiadomosc:&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
    38. </property>
    39. </widget>
    40. <widget class="QLabel" name="label_2">
    41. <property name="geometry">
    42. <rect>
    43. <x>20</x>
    44. <y>10</y>
    45. <width>91</width>
    46. <height>16</height>
    47. </rect>
    48. </property>
    49. <property name="text">
    50. <string>Chat</string>
    51. </property>
    52. </widget>
    53. <widget class="QPushButton" name="pushButton">
    54. <property name="geometry">
    55. <rect>
    56. <x>250</x>
    57. <y>230</y>
    58. <width>111</width>
    59. <height>23</height>
    60. </rect>
    61. </property>
    62. <property name="text">
    63. <string>Wyslij na client</string>
    64. </property>
    65. </widget>
    66. <widget class="QPlainTextEdit" name="plainTextEdit">
    67. <property name="geometry">
    68. <rect>
    69. <x>20</x>
    70. <y>30</y>
    71. <width>341</width>
    72. <height>151</height>
    73. </rect>
    74. </property>
    75. </widget>
    76. </widget>
    77. <widget class="QMenuBar" name="menuBar">
    78. <property name="geometry">
    79. <rect>
    80. <x>0</x>
    81. <y>0</y>
    82. <width>376</width>
    83. <height>21</height>
    84. </rect>
    85. </property>
    86. </widget>
    87. <widget class="QToolBar" name="mainToolBar">
    88. <attribute name="toolBarArea">
    89. <enum>TopToolBarArea</enum>
    90. </attribute>
    91. <attribute name="toolBarBreak">
    92. <bool>false</bool>
    93. </attribute>
    94. </widget>
    95. <widget class="QStatusBar" name="statusBar"/>
    96. </widget>
    97. <layoutdefault spacing="6" margin="11"/>
    98. <resources/>
    99. <connections/>
    100. </ui>
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with simple TCP chat client-server

    Are you sure server::tcpclient is a valid pointer when you call functions on it in the slot connected to the button?

    Better initialize to 0 and check for != 0

    Also no need to call signal "newConnection" in line 11 of the server constructor.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with simple TCP chat client-server

    After 6 hours i figured what i made wrong.
    In server.cpp

    Qt Code:
    1. tcpserver->newConnection();
    To copy to clipboard, switch view to plain text mode 
    That line is wrong.


    It's should be that
    Qt Code:
    1. tcpserver->setMaxPendingConnections(1);
    2. //and add this line
    3. tcpclient = new QTcpSocket (this);
    To copy to clipboard, switch view to plain text mode 


    @Edit
    But now, a message doesn't send to anyone( client->server | server -> client)

    mhm...

    @Edit 2
    Now that i mentioned, if i delete this line
    Qt Code:
    1. tcpserver->setMaxPendingConnections(1);
    To copy to clipboard, switch view to plain text mode 
    message is sending.

    But, only with 1 pc :/

    If i tried on 2 pc connected to one router, server side get me this:
    Qt Code:
    1. QIODevice::write (QTcpSocket): device not open
    To copy to clipboard, switch view to plain text mode 
    in Application Output

    Any ideas?
    Last edited by ziele; 17th January 2016 at 16:32.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with simple TCP chat client-server

    Quote Originally Posted by ziele View Post
    Qt Code:
    1. tcpserver->newConnection();
    To copy to clipboard, switch view to plain text mode 
    That line is wrong.
    Yes, as I said

    Quote Originally Posted by ziele View Post
    It's should be that
    Qt Code:
    1. //and add this line
    2. tcpclient = new QTcpSocket (this);
    To copy to clipboard, switch view to plain text mode 
    No, because you don't want to connect to clients but accept client connections

    Quote Originally Posted by ziele View Post
    But, only with 1 pc :/
    Yes, because your handler for newConnection() overwrites a single QTcpSocket variable.

    Quote Originally Posted by ziele View Post
    Any ideas?
    Yes, store the accepted connections in a list or map, not in a single pointer variable.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with simple TCP chat client-server

    I figured a solution.
    I made a server to accept connect to any client not only localhost, and in client instead of localhost i connect to ip 192.168....
    And that work on 2 different PC on same LAN. THANKS FOR HELP

Similar Threads

  1. Examples for chat program, server-client?
    By Gokulnathvc in forum Newbie
    Replies: 1
    Last Post: 8th September 2011, 10:53
  2. problem in client and server model of tcp in qt
    By shakthi in forum Qt Programming
    Replies: 1
    Last Post: 26th July 2011, 15:18
  3. Replies: 1
    Last Post: 17th December 2010, 08:53
  4. server-client problem
    By sksingh73 in forum Newbie
    Replies: 2
    Last Post: 3rd July 2010, 07:18
  5. TCP server-client app problem
    By pogostick in forum Newbie
    Replies: 6
    Last Post: 25th January 2010, 08:13

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.