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?
fileSize? Also, you have no error checking onsendorread. Maybe they're not returning what you expect.int32and sending an array of bytes. And what type isfileSize? If it's larger than 4 bytes, you might just be reading the most or least significant bytes.