I'm working with socket programming and am currently attempting to transfer a 1MB file through 16KB at a time. The data does initially transfer 16KB at a time; however, my ifstream reaches EOF prematurely which makes it so the file transfer is incomplete.
int main() {
int SIZE = 16000;
char file_buffer[SIZE];
int i = 0;
ifstream my_file("1MB", ios::in | ios::binary);
if (!my_file) {
cout << "No such file";
} else {
io_service io_service;
// socket creation
ip::tcp::socket client_socket(io_service);
client_socket
.connect(
tcp::endpoint(
address::from_string("127.0.0.1"),
9999));
while(!my_file.eof()) {
char ch;
my_file >> ch;
if(my_file.eof())
{
cout << "File Buffer: " << file_buffer << endl;
cout << "ERROR: EOF DETECTED" << endl;
break;
}
else if (i == SIZE)
{
sendData(client_socket, file_buffer);
memset(file_buffer, 0, sizeof file_buffer);
i = 0;
} else
{
file_buffer[i] = ch;
i++;
}
}
}
my_file.close();
return 0;
}