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:
#include "client.h"
#include <QHostAddress>
#include <unistd.h>
#include <QDebug>
{
}
client::~client()
{
}
void client::doconnect()
{
qDebug()<<"connecting....";
_socket->connectToHost(address,2020);
connect(_socket,SIGNAL(connected()),this,SLOT(cntToHost()));
}
int client::SendTCPData()
{
bool connected
= (_socket
->state
() == QTcpSocket::ConnectedState);
if(connected){
Data=get();
_socket->write(Data, Data.size());
qDebug()<<"Data send"<<Data.toHex();
return _socket->waitForBytesWritten(3000);
}
else{
qDebug()<<"Not Connected";
return false;
}
}
void client::cntToHost()
{
qDebug()<<"cnt";
if(!_socket->waitForConnected(3000))
{
qDebug() << "Error: " << _socket->errorString();
}
}
void client::GetTCPData()
{
unsigned int bytesAvailable = _socket->bytesAvailable();
char buf[bytesAvailable];
_socket->read(buf, bytesAvailable);
string pack(buf);
sendCmd(pack);
}
#include "client.h"
#include <QHostAddress>
#include <unistd.h>
#include <QDebug>
client::client(QWidget *parent) :
QMainWindow(parent)
{
_socket = new QTcpSocket(this);
}
client::~client()
{
}
void client::doconnect()
{
QHostAddress address ("192.168.23.138");
qDebug()<<"connecting....";
_socket->connectToHost(address,2020);
connect(_socket,SIGNAL(connected()),this,SLOT(cntToHost()));
}
int client::SendTCPData()
{
bool connected = (_socket->state() == QTcpSocket::ConnectedState);
if(connected){
Data=get();
_socket->write(Data, Data.size());
qDebug()<<"Data send"<<Data.toHex();
return _socket->waitForBytesWritten(3000);
}
else{
qDebug()<<"Not Connected";
return false;
}
}
void client::cntToHost()
{
qDebug()<<"cnt";
if(!_socket->waitForConnected(3000))
{
qDebug() << "Error: " << _socket->errorString();
}
}
void client::GetTCPData()
{
unsigned int bytesAvailable = _socket->bytesAvailable();
char buf[bytesAvailable];
_socket->read(buf, bytesAvailable);
string pack(buf);
sendCmd(pack);
}
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:
#include "sanraymodulegui.h"
#include "ui_sanraymodulegui.h"
#include <QProcess>
#include <QCoreApplication>
sanrayModuleGUI
::sanrayModuleGUI(QWidget *parent
) : ui(new Ui::sanrayModuleGUI)
{
ui->setupUi(this);
send=new SendCommand();
connect(_server, SIGNAL(newConnection()), this, SLOT(NewConnection()));
qDebug()<<"listening";
ui->label->setText("Listening...");
}
sanrayModuleGUI::~sanrayModuleGUI()
{
delete ui;
}
void sanrayModuleGUI::NewConnection()
{
while(_server->hasPendingConnections())
{
ui->label->setText("Connected");
_socket=_server->nextPendingConnection();
connect(_socket,SIGNAL(readyRead()),this,SLOT(readData()));
}
}
void sanrayModuleGUI::readData()
{
qDebug()<<"readData";
qDebug()<<"still read";
ReadReceiveData.push_back(data);
GetTCPData();
}
#include "sanraymodulegui.h"
#include "ui_sanraymodulegui.h"
#include <QProcess>
#include <QCoreApplication>
sanrayModuleGUI::sanrayModuleGUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::sanrayModuleGUI)
{
ui->setupUi(this);
send=new SendCommand();
_server=new QTcpServer(this);
_socket=new QTcpSocket(this);
connect(_server, SIGNAL(newConnection()), this, SLOT(NewConnection()));
if(_server->listen(QHostAddress::Any, 2020))
qDebug()<<"listening";
ui->label->setText("Listening...");
}
sanrayModuleGUI::~sanrayModuleGUI()
{
delete ui;
}
void sanrayModuleGUI::NewConnection()
{
while(_server->hasPendingConnections())
{
ui->label->setText("Connected");
_socket=_server->nextPendingConnection();
connect(_socket,SIGNAL(readyRead()),this,SLOT(readData()));
}
}
void sanrayModuleGUI::readData()
{
qDebug()<<"readData";
QByteArray data= _socket->readAll();
qDebug()<<"still read";
ReadReceiveData.push_back(data);
GetTCPData();
}
To copy to clipboard, switch view to plain text mode
Bookmarks