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