Solved Server side does not emit ReadyRead signal
-
- 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? -
@jsulm So I can write to the socket the size of the image therefore the server will know the size of the picture, and after that send the image?
-
@mandruk1331 yes
-
@jsulm Ok. Got it.
-
@jsulm so this if what I got so far
The client side send the size but the server does not response that he received the data, the string is "" but it has to be "2".
Could you tell me what am I doing wrong?
Server side:void TCPSocket::newImageReceived() { qDebug()<<"Img rece"; QByteArray ba; if(socket->bytesAvailable() <=0){ qDebug()<<"Cannot read from socket"<<socket->errorString(); }else{ if(socket->bytesAvailable()>0){ QString size(socket->readAll()); size_data = size.toInt(); socket->write("2"); socket->waitForBytesWritten(1000); } } while(size_data >0){ qDebug()<<"Server:"<<socket->bytesAvailable(); ba += socket->readAll(); socket->waitForReadyRead(1000); size_data-=socket->bytesAvailable(); } QImage img = QImage::fromData(ba); //socket->waitForReadyRead(1000); if(!img.isNull()){ if(checkFrame(img)){ //socket->write("1"); qDebug()<<"Frame ok"; }else{ qDebug()<<"Frame not ok"; //socket->write("0"); } emit sendImage(img); // to the GUI side not over the network } }
Client side:
void _Camera::SendData(QImage &frame){ QByteArray ba; QBuffer buffer(&ba); buffer.open(QIODevice::ReadWrite); frame.save(&buffer, "JPEG"); //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(QString::number(buffer.size()).toStdString().c_str()); qDebug()<<"Bytes written"<<socket.waitForBytesWritten(1000); while(socket.bytesAvailable() <0){ qDebug()<<"no data to be read"; } QString response (socket.readAll()); qDebug()<<"response is"<<response; if(response == "2"){ qDebug()<<"Data written"<<socket.write(buffer.data()); qDebug()<<"Bytes written"<<socket.waitForBytesWritten(1000); qDebug()<<"Bytes available"<<socket.bytesAvailable(); }else{ qDebug()<<"smth is wrong"; } }
-
@mandruk1331 No need to send anything to the client after reading size. Also, you're reading size wrongly: size should have a fixed size (4 bytes should be enough), so you need to read exactly 4 bytes not a string which can have arbitrary size.
Then, remove this while loop where you read the picture data - this should be done in the usual way using readyRead signal. You really should take a look at the networking Qt examples. -
@jsulm Ok, I will take a look at the examples and the docs.