Как передать файл по socket?
Передается только малая часть картинки. Как можно полностью передать?
Отправка файла
int MessageSize;
recv(connections[i], (char*)&MessageSize, sizeof(int), 0);
char* message = (char*)malloc(MessageSize + 1);
message[MessageSize] = '\0';
recv(connections[i], message, MessageSize, 0);
FILE* ptrFile = fopen(message, "rb");
if (ptrFile == NULL)
{
fputs("Ошибка файла", stderr);
exit(1);
}
fseek(ptrFile, 0, SEEK_END);
long lSize = ftell(ptrFile);
rewind(ptrFile);
SendPacket(i, SendFileP);
std::string Sx = message;
int Ix = Sx.size();
Sx = "";
int Ii;
for (Ii = Ix; Ii != -1; Ii--)
{
if (message[Ii] == '\\')
{
break;
}
}
Ii++;
for (; Ii < Ix; Ii++)
{
Sx += message[Ii];
}
Ii = Sx.size();
free(message);
send(connections[i], (char*)&Ii, sizeof(int), 0);
send(connections[i], Sx.c_str(), Ii, 0);
send(connections[i], (char*)&lSize, sizeof(long), NULL);
получение
int size_message;
recv(conn, (char*)&size_message, sizeof(int), 0);
std::cout << "\nDone2\n";
char* name = new char[size_message + 1];
name[size_message] = '\0';
recv(conn, name, size_message, 0);
long lSize;
recv(conn, (char*)&lSize, sizeof(lSize), NULL); // передаем размер файлы
char* buffer = (char*)malloc(sizeof(char) * lSize);
if (buffer == NULL)
{
fputs("Ошибка памяти", stderr);
exit(2);
}
recv(conn, buffer, lSize, NULL); // получаем данные файла
GetModuleFileNameA(NULL, path, sizeof(path));
std::string FullFile = path;
FullFile += name;
delete[] name;
FILE* ptrFile = fopen(FullFile.c_str(), "wb");
fwrite(buffer, 1, lSize, ptrFile);
fclose(ptrFile);
free(buffer);