WriteFile does not work with ERROR_UNEXP_NET_ERR when writing to one file from multiple processes

I have multiple computers connected to the same network.

When I try to write a file with the WriteFile function over the network from multiple processes running on different computers, I get ERROR_UNEXP_NET_ERR.

More details: On several PCs they have calculations and obtaining a part of the overall result, which should be written to one file. One randomly selected process calculates the total file size and determines the write offsets for each process. This process creates a file and resizes it to the calculated size.

After that, I start writing the file in all processes at the same time.

I open a file for each process like this:

handle_ = ::CreateFile("////SERVERNAME//folder//filename.ext", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

      

And trying to write in a loop like this:

const size_t BUFFER_SIZE = 30 * 1024;
std::vector<char> buffer(BUFFER_SIZE);
const size_t storage_size = storage_.size();
size_t internal_offset = 0;
while (size_t bytes = storage_.read(buffer.data(), buffer.size(), internal_offset))
{
    DWORD done = 0;

    LARGE_INTEGER li;
    li.QuadPart = offset + internal_offset;

    OVERLAPPED overlapped;
    overlapped.OffsetHigh = li.HighPart;
    overlapped.Offset = li.LowPart;
    overlapped.hEvent = 0;
    overlapped.Pointer = 0;
    overlapped.Internal = 0;
    overlapped.InternalHigh = 0;

    BOOL success = ::WriteFile(handle_, buffer, (DWORD)bytes, &done, &overlapped);
    if (!success || done != bytes)
            ThrowError(FFL, "Cannot write file: {1} ({2})", filepath_, GetLastError()); // Returns error code 59 (ERROR_UNEXP_NET_ERR)
    internal_offset += bytes;
}

      

This approach works with a file size of around 7MB, but fails when trying to write to 186.5GB.

I tried to do this in a couple of different ways and tried to write the local part of the file on each PC. Local recording works fine, but all network attempts fail.

I want to know why I am getting errors when I try to write over the network. I will be grateful for your suggestions on how to do it better than better.

+3


source to share





All Articles