无法通过邮件发送wchar_t数组

时间:2012-12-08 17:30:49

标签: c winapi client-server mailslot

我无法发送大于420字节的数据块。例如,如果我尝试发送421 bytes,则无法使用ReadFile阅读。如果我使用GetMailslotInfo( handleRead, 0, &msgSize, 0, 0 );检查maislot信息,则msgSize将始终为-1

所以这对我来说永远不会有用:

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

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

2 个答案:

答案 0 :(得分:3)

MSDN's documentation on mailslots

  

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

     

要在计算机之间发送大于424字节的邮件,请改用named pipesWindows Sockets

答案 1 :(得分:0)

我不确定你做错了什么,但是mailslots绝对可以处理大于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