0

I'm currently trying to implement basic client-server file transfer program using TCP sockets. Client is being written in C++/Qt and the server in C. I encountered great difficulty when trying to send the file size from server to client (integer value). Below are the code samples.

C server:

if(fileExists == '1')
{
   fp = fopen(filename, "r");
   fseek(fp, 0L, SEEK_END);
   fileSize = ftell(fp);
   printf("%d\n", fileSize);
   send(client_socket, &fileSize, sizeof(fileSize), 0);
}

Qt Client:

void Client::receiveFile(QString filename)
{
    qint32 fileSize;
    clientSocket->waitForReadyRead(1000);
    clientSocket->read(fileSize);
    qDebug() << fileSize;
}

The problem is that C calculates the file size properly as 40435408 and sends it over to the client which says the size is 32767. It's obvious that the problem lies on the client side. I tried to figure out the problem for almost whole day and failed. I realize that this is some simple and stupid mistake I made and I apologize for asking such dumb question. I'm a complete begginner. Can anyone help?

2
  • 1
    Can you explain the protocol you're trying to use to send the size? Is it a fixed number of bytes? If so, how many? If not, how does the receiver know when it has all of them? Is it in little-endian byte order? When you say the server is sending the correct bytes, what bytes exactly is it sending? And how do you know that they are correct? What type is fileSize? Also, you have no error checking on send or read. Maybe they're not returning what you expect. Commented Nov 30, 2018 at 16:36
  • 1
    Might be an endinanness problem if you are reading a int32 and sending an array of bytes. And what type is fileSize? If it's larger than 4 bytes, you might just be reading the most or least significant bytes. Commented Nov 30, 2018 at 17:05

1 Answer 1

1

When you call clientSocket->read(fileSize);, you are in fact calling QByteArray QIODevice::read(qint64 maxSize). Which means you are reading up to an undefined (as fileSize is not initialized) amount of bytes from the TCP socket and discard it immediatly, as you don't use the return value.

I think you are trying to use qint64 QIODevice::read(char *data, qint64 maxSize), so your code should look like this:

qint32 fileSize = 0;
clientSocket->waitForReadyRead(1000);
if (clientSocket->bytesAvailable() >= sizeof(fileSize)) {
    clientSocket->read(&fileSize, sizeof(fileSize));
} else {
    qWarning() < "Only received" << clientSocket->bytesAvailable() << "bytes: " << clientSocket->readAll().toHex(' ');
}
qDebug() << fileSize;

Note that I would not use this code in any software that is more than a proof of concept.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.