Reputation: 33
I'm writing a C++ project in Qt Creator, and I have a QTcpSocket
. It is connecting ok, but bytesAvailable()
is always zero
, even when I send data to it from another project I wrote in C#. I tested that the C# project is actually sending data by using another C# project, and it is sending the data correctly. Here is my code:
#include <QTcpSocket>
#include <QDebug>
void main()
{
QTcpSocket *tcpSocket=new QTcpSocket();
tcpSocket->connectToHost("127.0.0.1",8080);
while(true)
{
if(tcpSocket->bytesAvailable()!=0)
{
qDebug()<<tcpSocket<-bytesAvailable();
}
}
tcpSocket->disconnectFromHost();
}
Thanks. :D
Upvotes: 0
Views: 874
Reputation: 4350
First of all, all Qt programs need an application object to set up a lot of the internals of the framework. Insert the following as your first statement:
QCoreApplication app(argc, argv);
Secondly, the ordinary way of network programming in Qt is to use the event processing system. Since you don't seem to need that, you should use the blocking functions instead so that the operating system gets a chance to tell your application that data has arrived.
You must use waitForReadyRead()
before trying to read from the socket.
Read the documentation for more detailed information on the blocking and non-blocking API functions.
Upvotes: 1
Reputation: 710
You should replace line
if(tcpSocket->bytesAvailable!=0)
to
if(tcpSocket->bytesAvailable()!=0)
Upvotes: 0