1

我无法发送大于420字节的数据块。例如,如果我尝试发送421 bytes,我无法使用ReadFile. 如果我检查 maislot 信息GetMailslotInfo( handleRead, 0, &msgSize, 0, 0 );thenmsgSize将永远是-1.

所以这对我永远不起作用:

err = WriteFile( handleWrite, wcharArrayToSend, 421, &numBytesWritten, NULL );

有谁知道这种行为的原因?这是正常行为吗?

4

2 回答 2

3

根据MSDN 关于邮槽的文档

邮槽消息中的数据可以是任何形式,但在计算机之间发送时不能大于 424 字节。

要在计算机之间发送大于 424 字节的消息,请改用命名管道Windows 套接字

于 2012-12-08T18:00:39.140 回答
0

我不确定你做错了什么,但邮槽绝对可以处理大于 421 字节的消息。这是一些测试代码(成功运行):

服务器:

#include <windows.h>
#include <stdio.h>

int main() {    
    HANDLE mailslot = CreateMailslot("\\\\.\\mailslot\\myslot", 
        0, 
        MAILSLOT_WAIT_FOREVER, 
        NULL);
    if (mailslot == INVALID_HANDLE_VALUE) {
        fprintf(stderr, "Unable to create mailslot.");
        return 1;
    }

    char buffer[2048];
    OVERLAPPED ovlp = {0};

    ovlp.hEvent = CreateEvent(NULL, false, false, NULL);   
    if (ovlp.hEvent == NULL) {
        fprintf(stderr, "Unable to create Event.");
        return 2;
    }

    DWORD read;

    do { 
        ReadFile(mailslot, buffer, sizeof(buffer), &read, &ovlp);
        buffer[read] = 0;
        WaitForSingleObject(ovlp.hEvent, INFINITE);
        printf("%s\n", buffer);
    } while (strcmp(buffer, "exit"));    
    return 0;
}

客户:

#include <windows.h>
#include <stdio.h>

int main() {    
    HANDLE mailslot = CreateFile("\\\\.\\mailslot\\myslot", 
        GENERIC_WRITE, 
        FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL, 
        OPEN_ALWAYS, 
        0, 
        NULL);

    if (INVALID_HANDLE_VALUE == mailslot) {
        fprintf(stderr, "Unable to open mailslot.\n");
        return 1;
    }
    char buffer[2048];
    DWORD written;

    for (int i=0; i<1024; i++)
        buffer[i] = 'A' + (i%26);
    buffer[1023] = '\0';
    size_t len = strlen(buffer);

    WriteFile(mailslot, buffer, len, &written, NULL);    
    WriteFile(mailslot, "exit", 4, &written, NULL);
    return 0;
}

结果:

E:\C\source>mailslot
ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZAB
CDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCD
EFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF
GHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH
IJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKL
MNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN
OPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOP
QRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR
STUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRST
UVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX
YZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI
exit
于 2012-12-08T17:52:43.627 回答