Exempt an exception to attempting to send the size of the mass
-
I'm trying to get a 1024 file, but I'm throwing an exception.
Violation of reading rights at 0x0069E2FC
for (long i = 0; i < lSize; i += 1024) { if (lSize - i < 1024) { MAXSIZE = lSize - i; char* buffer = (char*)malloc(MAXSIZE + 1); buffer[MAXSIZE] = '\0'; fseek(ptrFile, i, SEEK_SET); fread(buffer, 1, MAXSIZE, ptrFile);
send(conn[i], (char*)&MAXSIZE, sizeof(long), 0); while (f != 1) { std::cout << "\nAccept\n"; recv(conn[i], (char*)&f, sizeof(int), 0); } send(conn[i], buffer, MAXSIZE, 0); std::cout << "\nSend " << i << "/" << lSize; break; }
server
int SendFile(std::string pathToFile, std::string files, int i) {
SendPacket(sendF, i);
FILE* ptrFile = fopen(pathToFile.c_str(), "rb");
if (ptrFile == NULL)
{
fputs("1", stderr);
exit(1);
}
// определяем размер файла
fseek(ptrFile, 0, SEEK_END);
long lSize = ftell(ptrFile);
rewind(ptrFile);send(conn[i], (char*)&lSize, sizeof(long), 0);
int msgsize = files.size();
send(conn[i], (char*)&msgsize, sizeof(int), 0);
send(conn[i], files.c_str(), msgsize, 0);msgsize = pathToFile.size();
send(conn[i], (char*)&msgsize, sizeof(int), 0);
send(conn[i], pathToFile.c_str(), msgsize, 0);long MAXSIZE = 1024;
char* buffer = (char*)malloc(MAXSIZE + 1); // выделить память для хранения содержимого файла
if (buffer == NULL)
{
fputs("2", stderr);
exit(2);
}
int f = 0;
buffer[MAXSIZE] = '\0';
for (long i = 0; i < lSize; i += 1024) {
if (lSize - i < 1024) {
MAXSIZE = lSize - i;
char* buffer = (char*)malloc(MAXSIZE + 1);
buffer[MAXSIZE] = '\0';
fseek(ptrFile, i, SEEK_SET);
fread(buffer, 1, MAXSIZE, ptrFile);send(conn[i], (char*)&MAXSIZE, sizeof(long), 0); while (f != 1) { std::cout << "\nAccept\n"; recv(conn[i], (char*)&f, sizeof(int), 0); } send(conn[i], buffer, MAXSIZE, 0); std::cout << "\nSend " << i << "/" << lSize; break; } fseek(ptrFile, i, SEEK_SET); fread(buffer, 1, MAXSIZE, ptrFile); std::cout << "\nMAXSIZE " << MAXSIZE << "\n"; send(conn[i], (char*)&MAXSIZE, sizeof(long), 0); while (f != 1) { std::cout << "\nAccept\n"; recv(conn[i], (char*)&f, sizeof(int), 0); } send(conn[i], buffer, MAXSIZE, 0); std::cout << "\nSend " << i << "/" << lSize;
}
fclose(ptrFile);
recv(conn[i], (char*)&f, sizeof(int), 0);
return 1;
}
client
void SaveFile()
{
std::cout << "START\n";
register long sizeF;
recv(conn, (char*)&sizeF, sizeof(long), 0);int msgsize;
recv(conn, (char*)&msgsize, sizeof(int), 0);
char* buffer = (char*)malloc(msgsize + 1);
buffer[msgsize] = '\0';recv(conn, buffer, msgsize, 0);
std::string gile = buffer;
free(buffer);recv(conn, (char*)&msgsize, sizeof(int), 0);
buffer = (char*)malloc(msgsize + 1);
buffer[msgsize] = '\0';recv(conn, buffer, msgsize, 0);
gile += GetNameFileMy(buffer);
free(buffer);//прием
FILE* ptrFile = fopen(gile.c_str(), "wb");
long mxs = 1024;
int i = 1;
buffer = (char*)malloc(mxs + 1);
buffer[mxs] = '\0';
do {
recv(conn, (char*)&mxs, sizeof(long), 0);
if (mxs == 1024)
{
std::cout << mxs << "\n";send(conn, (char*)&i, sizeof(int), 0); recv(conn, buffer, mxs, 0); fwrite(buffer, 1, mxs, ptrFile); } else { std::cout << mxs << "\n"; free(buffer); buffer = (char*)malloc(mxs + 1); buffer[mxs] = '\0'; send(conn, (char*)&i, sizeof(int), 0); recv(conn, buffer, mxs, 0); fwrite(buffer, 1, mxs, ptrFile); free(buffer); break; }
} while (true);
fclose(ptrFile);
send(conn, (char*)&i, sizeof(int), 0);
}
-
You have a variable.
i
cycle cycle cycle cyclefor (long i = 0; i < lSize; i += 1024)
engulfs the variable argument of the functionint SendFile(std::string pathToFile, std::string files, int i)
♪as well as
char* buffer = (char*)malloc(MAXSIZE + 1);
stained (No access) Because ofchar* buffer = (char*)malloc(MAXSIZE + 1);
within the cyclefor (long i = 0; i < lSize; i += 1024)
Give these variables different names. Warning the computer will be with the key.
-Wshadow
♪