Solved Server side does not emit ReadyRead signal
-
I have a client server architecture, the client can connect to server side. I have already checked that and it also writes data to the socket, but on the server side the ReadyRead signal is not being emitted, can someone help me out? thank you
#include "tcpsocket.h" #define PORT 7755 #define LOWER_BIT 0 TCPSocket::TCPSocket() { // socket = new QTcpSocket(this); connect(&server,SIGNAL(newConnection()),this,SLOT(newConnectionS())); } void TCPSocket::ListenS(){ if(server.listen(QHostAddress::LocalHost,5555)){ qDebug()<<"Server is listening"; } else{ qDebug()<<"Server cannot be started"<<server.errorString(); } } void TCPSocket::newConnectionS(){ qDebug()<<"Connected"; socket = server.nextPendingConnection(); socket->open(QIODevice::ReadWrite); if(socket->isOpen()){ qDebug()<<"Socket is open"; }else {qDebug()<<"Socket cannot be opened"<<socket->errorString();} connect(socket,SIGNAL(readyRead()),this,SLOT(newImageReceived())); } void TCPSocket::newImageReceived() { qDebug()<<"Img rece"; QByteArray ba; if(socket->bytesAvailable() <=0){ qDebug()<<"Cannot read from socket"<<socket->errorString(); }else{ ba = socket->readAll(); } QImage img = QImage::fromData(ba); emit sendImage(img); }
-
As already written in your other thread, it is a good idea to check out one of the examples (e.g. the fortune server/client example). There you can the different steps for handling a server client connection. Here is the example for the server part Checkout where nextPendingConnection is used.
You need to remove the open statement and it should work:
void TCPSocket::newConnectionS(){ qDebug()<<"Connected"; socket = server.nextPendingConnection(); // socket->open(QIODevice::ReadWrite); <== this is not required if(socket->isOpen()){ qDebug()<<"Socket is open"; }else {qDebug()<<"Socket cannot be opened"<<socket->errorString();} connect(socket,SIGNAL(readyRead()),this,SLOT(newImageReceived())); }
-
@koahnig It did not help(
I know that I asked this question already but now the it does not work, it does not even send a simple message, because the ReadyRead signal is not being emitted. On the client side the data is being written to the socket because it returns the size of the data being written into the socket. Maybe smth wrong with the client side:#include "tcpsocket.h" #define PORT 7755 TCPSocket::TCPSocket() { socket = new QTcpSocket(this); connect(socket,SIGNAL(bytesWritten(qint64)),this,SLOT(bytesWrittenC(qint64))); } void TCPSocket::bytesWrittenC(qint64 b){ qDebug()<<"Data is ready:"<<b; } void TCPSocket::ReadyReadC(){ qDebug()<<"Ready read"; qDebug()<<"Info: "<<socket->readAll(); } void TCPSocket::SendData(QImage &frame){ QByteArray ba; QBuffer buffer(&ba); buffer.open(QIODevice::ReadWrite); frame.save(&buffer, "jpg"); //ba.append("We did it"); //socket.setOpenMode(QIODevice::ReadWrite); if(connectedToHostC == false){ socket->connectToHost(QHostAddress::LocalHost,PORT,QIODevice::ReadWrite); connect(socket,SIGNAL(readyRead()),this,SLOT(ReadyReadC())); connectedToHostC = true; } if(socket->isValid()){ qDebug()<<"Socket is valid"; } socket->open(QIODevice::ReadWrite); qDebug()<<"Data written"<<socket->write(buffer.data()); qDebug()<<"Bytes available"<<socket->bytesAvailable(); }
-
Same thing here, comment out socket->open(QIODevice::ReadWrite);
void TCPSocket::SendData(QImage &frame){ QByteArray ba; QBuffer buffer(&ba); buffer.open(QIODevice::ReadWrite); frame.save(&buffer, "jpg"); //ba.append("We did it"); //socket.setOpenMode(QIODevice::ReadWrite); if(connectedToHostC == false){ socket->connectToHost(QHostAddress::LocalHost,PORT,QIODevice::ReadWrite); connect(socket,SIGNAL(readyRead()),this,SLOT(ReadyReadC())); connectedToHostC = true; } if(socket->isValid()){ qDebug()<<"Socket is valid"; } // socket->open(QIODevice::ReadWrite); <=== comment out qDebug()<<"Data written"<<socket->write(buffer.data()); qDebug()<<"Bytes available"<<socket->bytesAvailable(); }
You open a device which is already open. See connectToHost
It says in second paragraph:
The socket is opened in the given openMode and first enters HostLookupState, then performs a host ... -
@koahnig I have commented that, but it stills does not work, the client connects to the serverm but the server does not emit the ReadyRead signal.Also tried to connect to the server part with telnet, it does connect and sends chars to the server and the server part emits the ReadyRead signal. Can the problem be that the client part writes the data way too fast to the socket and the server side does not have the time to read from it?
Console output: (Client side):QObject: Cannot create children for a parent that is in a different thread. (Parent is QTcpSocket(0x20ea3780), parent's thread is QThread(0x20546478), current thread is _Camera(0x20e42d30) QObject: Cannot create children for a parent that is in a different thread. (Parent is QTcpSocket(0x20ea3780), parent's thread is QThread(0x20546478), current thread is _Camera(0x20e42d30) Socket is valid Data written 15063 Bytes available 0 Socket is valid Data written 15274 Bytes available 0
-
@mandruk1331 said in Server side does not emit ReadyRead signal:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x20ea3780), parent's thread is QThread(0x20546478), current thread is _Camera(0x20e42d30)Don't you consider this to be worthy of fixing?
-
@kshegunov I have fixed the issue with the thread, but still the signal is not being emitted on the server side, I just don't understand what is wrong. For getting the video from the webcam I use OpenCV.
-
@mandruk1331 Are there any other warnings when running the client or server?
Also: before writing to socket you should either call http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected or connect slot to http://doc.qt.io/qt-5/qabstractsocket.html#connected signal and write in this slot to make sure you're actually connected at the time you're trying to write. And you should always connect slots to error signals. -
Start by making sure you're sending data. Test your client by sending a few bytes to a server application that's known to work, e.g. the fortune server Qt example. Then test your server, for example open
netcat
and send a few bytes manually to your server, if you receive them, then the problem is most certainly with your client. Also make sure you're not using threading on the server, asQTcpServer::nextPendingConnection
is not the correct way to do it. -
if(connectedToHostC == false){ socket->connectToHost(QHostAddress::LocalHost,PORT,QIODevice::ReadWrite); connect(socket,SIGNAL(readyRead()),this,SLOT(ReadyReadC())); connectedToHostC = true; }
Can you verify that the socket actually connects?
qDebug() << socket->state();
-
@jsulm , @kshegunov @BjornW At the moment the photo is being transmitted to the server part. And when the frame is showed on the server part the lower part of the image is grey don't really know why and now when I want to scale it so it would fit the label size it gives these errors in the console:
QImage::scaled: Image is a null image Img rece Corrupt JPEG data: premature end of data segment
-
@mandruk1331 As the error message says your image is corrupted (most probably incomplete). You should check your networking code.
How do you read the data on the server? Keep in mind that most probably the data does not arrive at once but in several chunks. -
@jsulm On the server side I use the readAll function like this:
QString str(socket->readAll());
And how I can make the client side to wait for a msg from the server side. The question how I can make TCP/IP synchronous?
If you want I can give a link to GitHub account. -
- Why do you convert the data to QString?! You expect to get an JPEG image, right?
- Do you call readAll() only once? As I said the data arrives in several chunks and you need to read all of them. You connect readyRead signal to a slot and in this slot you call readAll() and store the data in a buffer until you have all the data.
"And how I can make the client side to wait for a msg from the server side" - what do you mean? you send a picture from client to server, right?
"The question how I can make TCP/IP synchronous?" - you don't. Why would you? -
@jsulm From the server side I expect a QString "1" img passed the validation "0" !passed. I call it only once. How I can determine that the buffer has all the data that can be used?
Client->JPEG->Server (checks if img is ok)->send msg that img is ok->client rcv that img is ok. -
@mandruk1331 "From the server side I expect a QString "1" img passed the validation "0" !passed" - actually I wanted to know how you read on server.
You need to define a protocol. For example the client could send first an int containing the number of bytes it is going to send next. Then the server knows when all data was received. -
@jsulm At the moment I can send a videostream over the socket to the server side using TCP/IP protocol and I was a little bit surprised because it works really really good, the frame rate on the server side is the same as on the client side. Now I will try to send the validation string from the server side to the client. A big thank you for everybody who gave me the advice on how to fix my problem and a special thank you to JSULM.
-
@mandruk1331 Glad I could help :-)
-
@jsulm One more question,. At the moment the frame are being transmitted between client and server, but on the server side I get a corrupted image (lower part of it is grey), but if the image size is less then 9k bytes than everything is ok, how I can make the image to be transmitted fully?
Thank you. -
@mandruk1331 I explained it above: you have to make sure you read the whole picture on the server side. Data is transmitted in packages over the network - you don't know how many packages you will get, so you need to read until you got everything.
One question: how big are the broken image on the server side? Do they have exact same size as on client side?