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?: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:
Code:
#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);
}
in SendTCPData() function I check socket connect or not always qDebug() ,Not Connected why??:(
Server.cpp:
Code:
#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();
}
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??:(
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,
_
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 ,
Quote:
QTcpSocket *_socket;
_socket=_server->nextPendingConnection();
I called client::SendTCPData() in my main, first I call doconnect() :
Code:
#include "client.h"
#include <QApplication>
#include <QCoreApplication>
using namespace std;
int main(int argc, char *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
Code:
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
Re: QTcpSocket program doesn't work correctly
Quote:
Originally Posted by
isan
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
I called client::SendTCPData() in my main, first I call doconnect() :
Code:
#include "client.h"
#include <QApplication>
#include <QCoreApplication>
using namespace std;
int main(int argc, char *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.
Quote:
Originally Posted by
isan
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,
_
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??
Re: QTcpSocket program doesn't work correctly
Quote:
Originally Posted by
isan
you mean I should first call SendTCPData() then call connectToHost() ??
No, that would even be in the wrong order.
Quote:
Originally Posted by
isan
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,
_
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....
Code:
#include "client.h"
#include <QHostAddress>
#include <unistd.h>
#include <QDebug>
{
}
client::~client()
{
_socket->disconnectFromHost();
}
void client::doconnect()
{
qDebug()<<"connecting....";
_socket->connectToHost(address,8585);
connect(_socket,SIGNAL(connected()),this,SLOT(cntToHost()));
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:
Quote:
connecting....
QAbstractSocket::ConnectingState
and do nothing , server side still listening........:(:(:(
Re: QTcpSocket program doesn't work correctly
Strange, where's the output of client::cntToHost()?
Looks like SendTCPData() is called from somewhere else.
Cheers,
_