<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Only a part of File gets send with QtcpSocket]]></title><description><![CDATA[<p dir="auto">Hello Everyone,</p>
<p dir="auto">I'm having a very curious case trying to send Data with QTcpSocket from a Client to a Server.<br />
I try to send the file Name and the Path where it should be saved on the Server Side. My Code works only for Data under 10 Ko. As soon as the File size is bigger than 10Ko only a part gets sent (14 or 12 Ko).</p>
<p dir="auto">Here's a part of the sender/Client Code:</p>
<pre><code>                       QFileInfo fileInfo(File);								
			out &lt;&lt; Name;										
			out &lt;&lt; QString::number(fileInfo.size());			
			QString FilePath = fileInfo.absolutePath();
			printf(FilePath.toStdString().c_str());
			out &lt;&lt; FilePath;
			int size = 0;
			while (!File.atEnd())
			{
				QByteArray rawFile;
				rawFile = File.read(1000);
				QFileInfo rawFileInfo(rawFile);
				size += rawFileInfo.size();
				out &lt;&lt; rawFile;
			}
</code></pre>
<p dir="auto">On the Server Side</p>
<pre><code>QDataStream in(m_socket);
	
	QByteArray z;
	QString fileName;
	QString fileSize;
	QString FilePath;
	in &gt;&gt; fileName;					
	qDebug() &lt;&lt; fileName;
	in &gt;&gt; fileSize;					
	qDebug() &lt;&lt; fileSize;
	in &gt;&gt; FilePath;				
	

	QDir Projectpath;
	Projectpath.mkpath(FilePath);

	QFile Target(FilePath + "/" + fileName);
	
		if (Target.open(QIODevice::Append))
		{
			while (m_socket-&gt;bytesAvailable())
			{

				in &gt;&gt; z;
				Target.write(z);
			}
			
			Target.close();
		}
	
</code></pre>
<p dir="auto">The Server creates the directory and saves  the File to the right path. I can't understand why it sends only a part of the File and why it works only for files under 10 ko?</p>
<p dir="auto">PS: qDebug()&lt;&lt; fileSize prints the right size of the File on the Server side. I just can't seem to write all of it to the File.</p>
<p dir="auto">I'm really desperate and any hint is highly appreciated. Thank you all !</p>
]]></description><link>https://forum.qt.io/topic/116751/only-a-part-of-file-gets-send-with-qtcpsocket</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 05:30:10 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/116751.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 07 Jul 2020 15:30:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Only a part of File gets send with QtcpSocket on Tue, 07 Jul 2020 15:47:31 GMT]]></title><description><![CDATA[<p dir="auto">Basically it's your responsibility to check how much data you processed</p>
<p dir="auto">from <a href="https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application" target="_blank" rel="noopener noreferrer nofollow ugc">https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application</a></p>
<p dir="auto">The <code>onReadyRead</code> slot is really the most interesting one. The first thing to remember is that when we receive the <code>readyRead</code> we can't make any assumption on how much data is available, the signal only tells us some data is there hence there are 4 cases we need to handle:</p>
<ol>
<li>The socket did not receive enough data to be a complete message, we only received a partial</li>
<li>There is exactly enough data in the socket buffer to read a message</li>
<li>There is more than enough data in the socket buffer to read a message but not enough to read 2 messages<br />
4. There is enough data in the socket buffer to read multiple messages</li>
</ol>
<p dir="auto">Cases 1 and 2 are entirely handled by <code>socketStream.commitTransaction()</code>. If there was not enough data to complete the reading when we called <code>socketStream &gt;&gt; jsonData;</code> this method will return false and we'll just exit the function and wait for more data to come in otherwise it will proceed and parse the data as a complete JSON message. Cases 3 and 4 are handled by the infinite loop. After we read the first message we try to read another one in the same way as before and break the loop only when the data in the buffer is no longer enough to be a complete JSON message.</p>
<p dir="auto">To facilitate the handling of differences in the expected size of the data received and the actual size, since Qt 5.7, <code>QIODevice</code> and <code>QDataStream</code> introduced transactions. Transactions work very similarly to SQL transactions: start the transaction, try and do something with the data, if everything went as expected you commit the transaction, otherwise you can go back as if you did nothing at all. <code>QDataStream::commitTransaction</code> will actually rollback if it detects there was an error and return false. We use this to check if <code>socketStream &gt;&gt; jsonData;</code> retrieved a full JSON document or not.". Internally <code>socketStream &gt;&gt; jsonData;</code> will read a 32bit unsigned integer that will be <code>jsonData.size()</code> after the read. Then it will read raw bytes of data to fill that length. If it detects any error it will set <code>socketStream.status()</code> to something different form <code>QDataStream::Ok</code>. <code>socketStream.commitTransaction()</code> then checks that status. If it is <code>QDataStream::Ok</code> it will shorten the buffer in the socket by the amount of data we successfully read and return true, otherwise it will return false maintaining the socket buffer identical to what it was before.</p>
]]></description><link>https://forum.qt.io/post/605297</link><guid isPermaLink="true">https://forum.qt.io/post/605297</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Tue, 07 Jul 2020 15:47:31 GMT</pubDate></item></channel></rss>