간단한 메일 슬롯 프로그램이 작동하지 않습니까?
-
19-09-2019 - |
문제
여기에있는 클라이언트 및 서버 예제 사용 : http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2vancedmailslot14.html VS2008로 컴파일하고 서버를 실행 한 다음 "Client MySlot"은 계속받습니다. 누구든지 아이디어가 있습니까? 다른 MailSlot 예제에 대한 링크도 환영합니다. 감사합니다.
섬기는 사람:
// Server sample
#include <windows.h>
#include <stdio.h>
void main(void)
{
HANDLE Mailslot;
char buffer[256];
DWORD NumberOfBytesRead;
// Create the mailslot
if ((Mailslot = CreateMailslot("\\\\.\\Mailslot\\Myslot", 0, MAILSLOT_WAIT_FOREVER, NULL)) == INVALID_HANDLE_VALUE)
{
printf("Failed to create a mailslot %d\n", GetLastError());
return;
}
// Read data from the mailslot forever!
while(ReadFile(Mailslot, buffer, 256, &NumberOfBytesRead, NULL) != 0)
{
printf("%.*s\n", NumberOfBytesRead, buffer);
}
}
고객:
// Client sample
#include <windows.h>
#include <stdio.h>
void main(int argc, char *argv[])
{
HANDLE Mailslot;
DWORD BytesWritten;
CHAR ServerName[256];
// Accept a command line argument for the server to send a message to
if (argc < 2)
{
printf("Usage: client <server name>\n");
return;
}
sprintf(ServerName, "\\\\%s\\Mailslot\\Myslot", argv[1]);
if ((Mailslot = CreateFile(ServerName, GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed with error %d\n", GetLastError());
return;
}
if (WriteFile(Mailslot, "This is a test", 14, &BytesWritten, NULL) == 0)
{
printf("WriteFile failed with error %d\n", GetLastError());
return;
}
printf("Wrote %d bytes\n", BytesWritten);
CloseHandle(Mailslot);
}
해결책
오류 53은 Error_Bad_netPath이며 "네트워크 경로는 찾을 수 없었습니다"입니다. 분명히 메일 슬롯에 잘못된 서버 이름을 사용하고 있습니다. 사용 \\.\mailslot\blah
서버가 클라이언트와 동일한 컴퓨터에서 실행되는 경우 그리고 문자열로 백 슬래시를 피하는 것을 잊지 마십시오. "\\\\.\\mailslot\\blah"
.
다른 팁
두 파일에 게시 된대로 코드를 정확하게 복사하여 VS2008로 컴파일했으며 완벽하게 달렸습니다. 클라이언트 프로그램이 Client.Exe로 컴파일 된 경우 다음 명령을 입력하십시오.
client .
또는
client <computername>
여기서 컴퓨터 이름은 도메인이없는 PC 이름입니다. API에 전화 할 수 있습니다 GetComputerName 이름을 검색합니다.
제휴하지 않습니다 StackOverflow