如题,服务器端的WriteFile和ReadFile,在GetLastError中返回错误代码997和234。请大家指点!
以下是代码,方便高手们查看,先放一份去掉错误处理的代码,后面是有错误处理的代码:
以下是代码,方便高手们查看,先放一份去掉错误处理的代码,后面是有错误处理的代码:
//服务器端 #include <windows.h> #include <iostream> #include <string> int _create(); int _write(); int _read(); DWORD myDWORD_GetLastError; HANDLE myHANDLE_CreateNamedPipe; OVERLAPPED myOVERLAPPED; int main(int argc, char *argv[]) { _create(); _write(); _read(); system("pause"); return 0; } int _create() { myHANDLE_CreateNamedPipe = CreateNamedPipe(L"\\.\pipe\zzs11", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED/* | WRITE_DAC | WRITE_OWNER | ACCESS_SYSTEM_SECURITY*/, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_ACCEPT_REMOTE_CLIENTS, PIPE_UNLIMITED_INSTANCES, 1024, 1024, 100, NULL); HANDLE myHANDLE_CreateEvent = CreateEvent(NULL, TRUE, FALSE, NULL); OVERLAPPED *myOVERLAPPED_memset = (OVERLAPPED *)memset(&myOVERLAPPED, 0, sizeof(OVERLAPPED)); myOVERLAPPED.hEvent = myHANDLE_CreateEvent; BOOL myBOOL_ConnectNamedPipe = ConnectNamedPipe(myHANDLE_CreateNamedPipe, &myOVERLAPPED); DWORD myDWORD_WaitForSingleObject = WaitForSingleObject(myHANDLE_CreateEvent, INFINITE); CloseHandle(myHANDLE_CreateEvent); return 0; } int _write() { char *aa = "server789"; DWORD dd; WriteFile(myHANDLE_CreateNamedPipe, aa, strlen(aa), &dd, NULL); std::cout << "已写入 " << aa << std::endl; return 0; } int _read() { char * aa; DWORD dd; aa = new char[100]; memset(aa, 0, 100); ReadFile(myHANDLE_CreateNamedPipe, aa, strlen(aa), &dd, NULL); std::cout << "读取到 " << aa << std::endl; return 0; }
//客户端 #include <windows.h> #include <iostream> int _open(); int _read(); int _write(); DWORD myDWORD_GetLastError; HANDLE myHANDLE_CreateFile; int main(int argc, char *argv[]) { _open(); Sleep(1000); _read(); _write(); system("pause"); return 0; } int _open() { BOOL myBOOL_WaitNamedPipe = WaitNamedPipe(L"\\.\pipe\zzs11", NMPWAIT_WAIT_FOREVER); myHANDLE_CreateFile = CreateFile(L"\\.\pipe\zzs11", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); return 0; } int _read() { char * aa; DWORD dd; aa = new char[100]; memset(aa, 0, 100); BOOL myBOOL_ReadFile = ReadFile(myHANDLE_CreateFile, aa, strlen(aa), &dd, NULL); std::cout << "读取到 " << aa << std::endl; return 0; } int _write() { char *aa = "client789"; DWORD dd; BOOL myBOOL_WriteFile = WriteFile(myHANDLE_CreateFile, aa, strlen(aa), &dd, NULL); std::cout << "已写入 " << aa << std::endl; return 0; }
//服务器端 #include <windows.h> #include <iostream> #include <string> int _create(); int _write(); int _read(); DWORD myDWORD_GetLastError; HANDLE myHANDLE_CreateNamedPipe; OVERLAPPED myOVERLAPPED; int main(int argc, char *argv[]) { _create(); _write(); _read(); system("pause"); return 0; } int _create() { myHANDLE_CreateNamedPipe = CreateNamedPipe(L"\\.\pipe\zzs11", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED/* | WRITE_DAC | WRITE_OWNER | ACCESS_SYSTEM_SECURITY*/, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_ACCEPT_REMOTE_CLIENTS, PIPE_UNLIMITED_INSTANCES, 1024, 1024, 100, NULL); myDWORD_GetLastError = GetLastError(); if (INVALID_HANDLE_VALUE == myHANDLE_CreateNamedPipe) { std::cout << "INVALID_HANDLE_VALUE == myHANDLE_CreateNamedPipe GetLastError:" << myDWORD_GetLastError << std::endl; return 1; } HANDLE myHANDLE_CreateEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (NULL == myHANDLE_CreateEvent) { std::cout << "NULL == myHANDLE_CreateEvent" << std::endl; return 1; } OVERLAPPED *myOVERLAPPED_memset = (OVERLAPPED *)memset(&myOVERLAPPED, 0, sizeof(OVERLAPPED)); if (&myOVERLAPPED != myOVERLAPPED_memset) { std::cout << "&myOVERLAPPED != myOVERLAPPED_memset" << std::endl; return 1; } myOVERLAPPED.hEvent = myHANDLE_CreateEvent; BOOL myBOOL_ConnectNamedPipe = ConnectNamedPipe(myHANDLE_CreateNamedPipe, &myOVERLAPPED); if (0 == myBOOL_ConnectNamedPipe) { myDWORD_GetLastError = 0; myDWORD_GetLastError = GetLastError(); if (ERROR_IO_PENDING != myDWORD_GetLastError && ERROR_PIPE_CONNECTED != myDWORD_GetLastError) { CloseHandle(myHANDLE_CreateNamedPipe); CloseHandle(myHANDLE_CreateEvent); std::cout << "0 == myBOOL_ConnectNamedPipe 并且 ERROR_IO_PENDING != myDWORD_GetLastError && ERROR_PIPE_CONNECTED != myDWORD_GetLastError" << std::endl; return 1; } } DWORD myDWORD_WaitForSingleObject = WaitForSingleObject(myHANDLE_CreateEvent, INFINITE); if (WAIT_FAILED == myDWORD_WaitForSingleObject) { CloseHandle(myHANDLE_CreateNamedPipe); CloseHandle(myHANDLE_CreateEvent); std::cout << "WAIT_FAILED == myDWORD_WaitForSingleObject" << std::endl; return 1; } CloseHandle(myHANDLE_CreateEvent); return 0; } int _write() { char *aa = "server789"; DWORD dd; WriteFile(myHANDLE_CreateNamedPipe, "server789", strlen("server789"), &dd, NULL); myDWORD_GetLastError = 0; myDWORD_GetLastError = GetLastError(); std::cout << myDWORD_GetLastError << std::endl; std::cout << "已写入 " << aa << std::endl; return 0; } int _read() { char * aa; DWORD dd; aa = new char[100]; memset(aa, 0, 100); ReadFile(myHANDLE_CreateNamedPipe, aa, strlen(aa), &dd, NULL); myDWORD_GetLastError = 0; myDWORD_GetLastError = GetLastError(); std::cout << myDWORD_GetLastError << std::endl; std::cout << "读取到 " << aa << std::endl; return 0; }
//客户端 #include <windows.h> #include <iostream> int _open(); int _read(); int _write(); DWORD myDWORD_GetLastError; HANDLE myHANDLE_CreateFile; int main(int argc, char *argv[]) { _open(); Sleep(1000); _read(); _write(); system("pause"); return 0; } int _open() { BOOL myBOOL_WaitNamedPipe = WaitNamedPipe(L"\\.\pipe\zzs11", NMPWAIT_WAIT_FOREVER); myDWORD_GetLastError = 0; myDWORD_GetLastError = GetLastError(); if (0 == myBOOL_WaitNamedPipe) { std::cout << "0 == myBOOL_WaitNamedPipe GetLastError:" << myDWORD_GetLastError << std::endl; return 1; } myHANDLE_CreateFile = CreateFile(L"\\.\pipe\zzs11", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); myDWORD_GetLastError = 0; myDWORD_GetLastError = GetLastError(); if (INVALID_HANDLE_VALUE == myHANDLE_CreateFile) { std::cout << "INVALID_HANDLE_VALUE == myHANDLE_CreateFile GetLastError:" << myDWORD_GetLastError << std::endl; return 1; } return 0; } int _read() { char * aa; DWORD dd; aa = new char[100]; memset(aa, 0, 100); BOOL myBOOL_ReadFile = ReadFile(myHANDLE_CreateFile, aa, strlen(aa), &dd, NULL); myDWORD_GetLastError = 0; myDWORD_GetLastError = GetLastError(); if (TRUE != myBOOL_ReadFile) { std::cout << "TRUE != myBOOL_ReadFile GetLastError:" << myDWORD_GetLastError << std::endl; } std::cout << "读取到 " << aa << std::endl; return 0; } int _write() { char *aa = "client789"; DWORD dd; BOOL myBOOL_WriteFile = WriteFile(myHANDLE_CreateFile, aa, strlen(aa), &dd, NULL); myDWORD_GetLastError = 0; myDWORD_GetLastError = GetLastError(); if (TRUE != myBOOL_WriteFile) { std::cout << "TRUE != myBOOL_WriteFile GetLastError:" << myDWORD_GetLastError << std::endl; } std::cout << "已写入 " << aa << std::endl; return 0; }
解决方案
20
234 More data is available. ERROR_MORE_DATA
应该是定义的数组太小了
997 Overlapped I/O operation is in progress. ERROR_IO_PENDING
重叠I / O操作
应该是定义的数组太小了
997 Overlapped I/O operation is in progress. ERROR_IO_PENDING
重叠I / O操作
20
赞同,题主单步调试下