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*)&amp;MAXSIZE, sizeof(long), 0);
        while (f != 1) {
            std::cout &lt;&lt; "\nAccept\n";
            recv(conn[i], (char*)&amp;f, sizeof(int), 0);
        }
        send(conn[i], buffer, MAXSIZE, 0);
        std::cout &lt;&lt; "\nSend " &lt;&lt; i &lt;&lt; "/" &lt;&lt; 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*)&amp;MAXSIZE, sizeof(long), 0);
           while (f != 1) {
               std::cout &lt;&lt; "\nAccept\n";
               recv(conn[i], (char*)&amp;f, sizeof(int), 0);
           }
           send(conn[i], buffer, MAXSIZE, 0);
           std::cout &lt;&lt; "\nSend " &lt;&lt; i &lt;&lt; "/" &lt;&lt; lSize;
           break;
       }
       fseek(ptrFile, i, SEEK_SET);
       fread(buffer, 1, MAXSIZE, ptrFile);
       std::cout &lt;&lt; "\nMAXSIZE " &lt;&lt; MAXSIZE &lt;&lt; "\n";
       send(conn[i], (char*)&amp;MAXSIZE, sizeof(long), 0);
    
       while (f != 1) {
           std::cout &lt;&lt; "\nAccept\n";
           recv(conn[i], (char*)&amp;f, sizeof(int), 0);
       }
       send(conn[i], buffer, MAXSIZE, 0);
       std::cout &lt;&lt; "\nSend " &lt;&lt; i &lt;&lt; "/" &lt;&lt; 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*)&amp;i, sizeof(int), 0);
    
           recv(conn, buffer, mxs, 0);
           fwrite(buffer, 1, mxs, ptrFile);
       }
       else {
           std::cout &lt;&lt; mxs &lt;&lt; "\n";
           free(buffer);
           buffer = (char*)malloc(mxs + 1);
           buffer[mxs] = '\0';
    
           send(conn, (char*)&amp;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 cycle for (long i = 0; i < lSize; i += 1024) engulfs the variable argument of the function int SendFile(std::string pathToFile, std::string files, int i)

    as well as char* buffer = (char*)malloc(MAXSIZE + 1); stained (No access) Because of char* buffer = (char*)malloc(MAXSIZE + 1); within the cycle for (long i = 0; i < lSize; i += 1024)

    Give these variables different names. Warning the computer will be with the key. -Wshadow


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2