Results 1 to 9 of 9

Thread: QTcpSocket program doesn't work correctly

  1. #1
    Join Date
    Feb 2016
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Question QTcpSocket program doesn't work correctly

    I'm new to Qt and I wrote this program, but it seems that the socket doesn't connect to server. I don't know why, did I write connect and listen properly?
    my client run in raspbian on raspberry pi and my server run on Ubuntu install on WMware WorkStation,for connect I get the ip of ubuntu,can they connect to each other easily??
    client.cpp:
    Qt Code:
    1. #include "client.h"
    2. #include <QHostAddress>
    3. #include <unistd.h>
    4. #include <QDebug>
    5. client::client(QWidget *parent) :
    6. QMainWindow(parent)
    7. {
    8. _socket = new QTcpSocket(this);
    9. }
    10.  
    11.  
    12. client::~client()
    13. {
    14.  
    15. }
    16.  
    17. void client::doconnect()
    18. {
    19.  
    20.  
    21. QHostAddress address ("192.168.23.138");
    22. qDebug()<<"connecting....";
    23. _socket->connectToHost(address,2020);
    24.  
    25. connect(_socket,SIGNAL(connected()),this,SLOT(cntToHost()));
    26.  
    27. }
    28.  
    29. int client::SendTCPData()
    30. {
    31.  
    32. bool connected = (_socket->state() == QTcpSocket::ConnectedState);
    33. if(connected){
    34. Data=get();
    35. _socket->write(Data, Data.size());
    36. qDebug()<<"Data send"<<Data.toHex();
    37. return _socket->waitForBytesWritten(3000);
    38. }
    39. else{
    40. qDebug()<<"Not Connected";
    41. return false;
    42. }
    43. }
    44.  
    45. void client::cntToHost()
    46. {
    47. qDebug()<<"cnt";
    48. if(!_socket->waitForConnected(3000))
    49. {
    50. qDebug() << "Error: " << _socket->errorString();
    51. }
    52. }
    53.  
    54. void client::GetTCPData()
    55. {
    56. unsigned int bytesAvailable = _socket->bytesAvailable();
    57. char buf[bytesAvailable];
    58. _socket->read(buf, bytesAvailable);
    59. string pack(buf);
    60. sendCmd(pack);
    61. }
    To copy to clipboard, switch view to plain text mode 

    in SendTCPData() function I check socket connect or not always qDebug() ,Not Connected why??

    Server.cpp:
    Qt Code:
    1. #include "sanraymodulegui.h"
    2. #include "ui_sanraymodulegui.h"
    3. #include <QProcess>
    4. #include <QCoreApplication>
    5.  
    6. sanrayModuleGUI::sanrayModuleGUI(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::sanrayModuleGUI)
    9. {
    10. ui->setupUi(this);
    11. send=new SendCommand();
    12. _server=new QTcpServer(this);
    13. _socket=new QTcpSocket(this);
    14. connect(_server, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    15. if(_server->listen(QHostAddress::Any, 2020))
    16. qDebug()<<"listening";
    17. ui->label->setText("Listening...");
    18.  
    19. }
    20.  
    21. sanrayModuleGUI::~sanrayModuleGUI()
    22. {
    23. delete ui;
    24. }
    25. void sanrayModuleGUI::NewConnection()
    26. {
    27.  
    28. while(_server->hasPendingConnections())
    29. {
    30. ui->label->setText("Connected");
    31. _socket=_server->nextPendingConnection();
    32. connect(_socket,SIGNAL(readyRead()),this,SLOT(readData()));
    33.  
    34. }
    35. }
    36.  
    37. void sanrayModuleGUI::readData()
    38. {
    39. qDebug()<<"readData";
    40. QByteArray data= _socket->readAll();
    41. qDebug()<<"still read";
    42. ReadReceiveData.push_back(data);
    43. GetTCPData();
    44. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2016
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTcpSocket program doesn't work correctly

    I think my client has problem for connection when _socket check state() doesn't connected to server,any idea??

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,336
    Thanks
    36
    Thanked 1,528 Times in 1,479 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket program doesn't work correctly

    Aside from it not making any sense to call waitForConnected() inside the slot that gets invoked on connect, is that slot called?

    It also makes no sense to create a QTcpSocket in the server's constructor, on the server side sockets come from the server's nextPendingConnection() call.

    There is no indication in your code where you call client::SendTCPData() maybe you do that before the connection has been established.

    Have you verified that there is something listening on the IP/Port combination you are using? E.g. using telnet?

    Cheers,
    _

  4. #4
    Join Date
    Feb 2016
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTcpSocket program doesn't work correctly

    I call if(!_socket->waitForConnected(3000)) for check state,you mean It is not necessary??delete it??

    I edit create QTcpSocket ,
    QTcpSocket *_socket;
    _socket=_server->nextPendingConnection();
    I called client::SendTCPData() in my main, first I call doconnect() :
    Qt Code:
    1. #include "client.h"
    2. #include <QApplication>
    3. #include <QCoreApplication>
    4.  
    5. using namespace std;
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. client cln;
    10. cln.doconnect();
    11. cln.GetTCPData();
    12. cln.SendTCPData();
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    there is no something listening on IP/Port ,I using ssh

    i added this to my SendTCPData() function
    Qt Code:
    1. int client::SendTCPData()
    2. {
    3. connected = (_socket->state() == QTcpSocket::ConnectedState);
    4. qDebug()<<_socket->state(); //print out QAbstractSocket::ConnectingState
    5.  
    6. if(connected){
    7. Data=get();
    8. _socket->write(Data, Data.size());
    9. qDebug()<<"Data send"<<Data.toHex();
    10. return _socket->waitForBytesWritten(3000);
    11. }
    12. else{
    13.  
    14. qDebug()<<"Not Connected";
    15. _socket->connectToHost("192.168.23.138",2020); //print out QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.23.138"
    16. // return false;
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    but still if(connected) doesn't work
    Last edited by isan; 9th April 2016 at 17:33.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,336
    Thanks
    36
    Thanked 1,528 Times in 1,479 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket program doesn't work correctly

    Quote Originally Posted by isan View Post
    I call if(!_socket->waitForConnected(3000)) for check state,you mean It is not necessary??delete it??
    This is inside the slot that is connected to the socket's connected() signal.
    So it is called when the socket has connected. There is not point in waiting for connected(), it was the cause of the invocation.

    Quote Originally Posted by isan View Post
    I called client::SendTCPData() in my main, first I call doconnect() :
    Qt Code:
    1. #include "client.h"
    2. #include <QApplication>
    3. #include <QCoreApplication>
    4.  
    5. using namespace std;
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. client cln;
    10. cln.doconnect();
    11. cln.GetTCPData();
    12. cln.SendTCPData();
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Then it is clear why SendTCPData() finds the socket unconnected, it never had any chance to connect
    You are calling SendTCPData() after connectToHost() without the event loop running yet.

    Quote Originally Posted by isan View Post
    but still if(connected) doesn't work
    You need to give the socket a chance to connect.
    Don't call any IO method before the socket has connected.

    Cheers,
    _

  6. #6
    Join Date
    Feb 2016
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTcpSocket program doesn't work correctly

    you mean I should first call SendTCPData() then call connectToHost() ?? can you show me with code how can I give the socket a chance to connect??

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,336
    Thanks
    36
    Thanked 1,528 Times in 1,479 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket program doesn't work correctly

    Quote Originally Posted by isan View Post
    you mean I should first call SendTCPData() then call connectToHost() ??
    No, that would even be in the wrong order.

    Quote Originally Posted by isan View Post
    can you show me with code how can I give the socket a chance to connect??
    You can either call waitForConnected() so that you block until you are connected or you let the application's event loop run and do the IO once you've received the connected() signal.

    Cheers,
    _

  8. #8
    Join Date
    Feb 2016
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTcpSocket program doesn't work correctly

    tnx , According to your help i edit my client and in my main.cpp (client side)I just call doconnect() function....
    Qt Code:
    1. #include "client.h"
    2. #include <QHostAddress>
    3. #include <unistd.h>
    4. #include <QDebug>
    5. client::client(QWidget *parent) :
    6. QMainWindow(parent)
    7. {
    8.  
    9. }
    10.  
    11.  
    12. client::~client()
    13. {
    14. _socket->disconnectFromHost();
    15. }
    16.  
    17. void client::doconnect()
    18. {
    19.  
    20. _socket = new QTcpSocket(this);
    21. QHostAddress address ("192.168.23.138");
    22. qDebug()<<"connecting....";
    23. _socket->connectToHost(address,8585);
    24.  
    25. connect(_socket,SIGNAL(connected()),this,SLOT(cntToHost()));
    26. connect(_socket, SIGNAL(disconnected()), this, SLOT(discond()));
    27. connect(_socket, SIGNAL(readyRead()),this, SLOT(readData()));
    28.  
    29. qDebug()<<_socket->state();
    30.  
    31. }
    32.  
    33. void client::cntToHost()
    34. {
    35. qDebug()<<"connect to host";
    36. SendTCPData();
    37. }
    38.  
    39. void client::discond()
    40. {
    41. qDebug() << "disconnected...";
    42. }
    43.  
    44. void client::readData()
    45. {
    46. qDebug() << "reading...";
    47. GetTCPData();
    48. }
    49.  
    50. int client::SendTCPData()
    51. {
    52. connectedcheck = (_socket->state() == QTcpSocket::ConnectedState);
    53. qDebug()<<_socket->state();
    54.  
    55. if(connectedcheck){
    56. Data=get();
    57. _socket->write(Data, Data.size());
    58. qDebug()<<"Data send"<<Data.toHex();
    59. return _socket->waitForBytesWritten(3000);
    60. }
    61. else{
    62.  
    63. qDebug()<<"Not Connected";
    64. _socket->connectToHost("192.168.23.138",8585);
    65. // return false;
    66. }
    67. }
    68.  
    69. void client::GetTCPData()
    70. {
    71. unsigned int bytesAvailable = _socket->bytesAvailable();
    72. char buf[bytesAvailable];
    73. _socket->read(buf, bytesAvailable);
    74. string pack(buf);
    75. }
    To copy to clipboard, switch view to plain text mode 

    when I run the program print out:
    connecting....
    QAbstractSocket::ConnectingState
    and do nothing , server side still listening........

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,336
    Thanks
    36
    Thanked 1,528 Times in 1,479 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket program doesn't work correctly

    Strange, where's the output of client::cntToHost()?

    Looks like SendTCPData() is called from somewhere else.

    Cheers,
    _

Similar Threads

  1. Replies: 6
    Last Post: 3rd April 2015, 21:01
  2. if statement doesn't behave correctly
    By esotery in forum Qt Programming
    Replies: 8
    Last Post: 1st August 2012, 03:46
  3. Program result writed by qDebug() to file doesn't work.
    By Vincenzo in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2009, 19:49
  4. Replies: 1
    Last Post: 19th March 2009, 13:40
  5. QHttp simple program doesn't work
    By johncharlesb in forum Qt Programming
    Replies: 0
    Last Post: 4th April 2008, 04:18

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.