PDA

View Full Version : QTcpSocket program doesn't work correctly



isan
7th April 2016, 17:08
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?:confused:
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(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(cntT oHost()));

}

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);
}

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) :
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(read Data()));

}
}

void sanrayModuleGUI::readData()
{
qDebug()<<"readData";
QByteArray data= _socket->readAll();
qDebug()<<"still read";
ReadReceiveData.push_back(data);
GetTCPData();
}

isan
9th April 2016, 07:43
I think my client has problem for connection when _socket check state() doesn't connected to server,any idea??:(

anda_skoa
9th April 2016, 10:56
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,
_

isan
9th April 2016, 17:11
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() :

#include "client.h"
#include <QApplication>
#include <QCoreApplication>

using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
client cln;
cln.doconnect();
cln.GetTCPData();
cln.SendTCPData();
return a.exec();
}

there is no something listening on IP/Port ,I using ssh

i added this to my SendTCPData() function

int client::SendTCPData()
{
connected = (_socket->state() == QTcpSocket::ConnectedState);
qDebug()<<_socket->state(); //print out QAbstractSocket::ConnectingState

if(connected){
Data=get();
_socket->write(Data, Data.size());
qDebug()<<"Data send"<<Data.toHex();
return _socket->waitForBytesWritten(3000);
}
else{

qDebug()<<"Not Connected";
_socket->connectToHost("192.168.23.138",2020); //print out QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.23.138"
// return false;
}
}
but still if(connected) doesn't work

anda_skoa
9th April 2016, 20:18
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.



I called client::SendTCPData() in my main, first I call doconnect() :

#include "client.h"
#include <QApplication>
#include <QCoreApplication>

using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
client cln;
cln.doconnect();
cln.GetTCPData();
cln.SendTCPData();
return a.exec();
}


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.



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,
_

isan
9th April 2016, 20:35
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??

anda_skoa
10th April 2016, 09:19
you mean I should first call SendTCPData() then call connectToHost() ??

No, that would even be in the wrong order.



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,
_

isan
10th April 2016, 09:33
tnx , According to your help i edit my client and in my main.cpp (client side)I just call doconnect() function....

#include "client.h"
#include <QHostAddress>
#include <unistd.h>
#include <QDebug>
client::client(QWidget *parent) :
QMainWindow(parent)
{

}


client::~client()
{
_socket->disconnectFromHost();
}

void client::doconnect()
{

_socket = new QTcpSocket(this);
QHostAddress address ("192.168.23.138");
qDebug()<<"connecting....";
_socket->connectToHost(address,8585);

connect(_socket,SIGNAL(connected()),this,SLOT(cntT oHost()));
connect(_socket, SIGNAL(disconnected()), this, SLOT(discond()));
connect(_socket, SIGNAL(readyRead()),this, SLOT(readData()));

qDebug()<<_socket->state();

}

void client::cntToHost()
{
qDebug()<<"connect to host";
SendTCPData();
}

void client::discond()
{
qDebug() << "disconnected...";
}

void client::readData()
{
qDebug() << "reading...";
GetTCPData();
}

int client::SendTCPData()
{
connectedcheck = (_socket->state() == QTcpSocket::ConnectedState);
qDebug()<<_socket->state();

if(connectedcheck){
Data=get();
_socket->write(Data, Data.size());
qDebug()<<"Data send"<<Data.toHex();
return _socket->waitForBytesWritten(3000);
}
else{

qDebug()<<"Not Connected";
_socket->connectToHost("192.168.23.138",8585);
// return false;
}
}

void client::GetTCPData()
{
unsigned int bytesAvailable = _socket->bytesAvailable();
char buf[bytesAvailable];
_socket->read(buf, bytesAvailable);
string pack(buf);
}

when I run the program print out:

connecting....
QAbstractSocket::ConnectingState
and do nothing , server side still listening........:(:(:(

anda_skoa
10th April 2016, 10:38
Strange, where's the output of client::cntToHost()?

Looks like SendTCPData() is called from somewhere else.

Cheers,
_