如何在 windows 中随时(同时)发送数据?
how to send data at any moment ( simultaneous ) in windows?
我想在windowsOS中写一个namedpipe客户端,即使客户端正在接收数据,也可以随时发送数据。MSDN的Example只显示发送数据后receive something.And 串行操作不是我想要的。因为我在客户端和服务器之间传输的数据不是很大,也就是说IO操作应该不是一个耗时的过程,所以我没有在客户端使用OVERLAP
。
我在客户端的MSDN示例中修改的代码如下:
主线程不断读取数据,子线程不断向服务器发送数据。但是调试的时候服务器读取数据时卡住了
std::thread t([&] {
cbToWrite = (lstrlen(lpvMessage) + 1) * sizeof(TCHAR);
_tprintf(TEXT("Sending %d byte message: \"%s\"\n"), cbToWrite, lpvMessage);
fSuccess = WriteFile(
hPipe, // pipe handle
lpvMessage, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL); // not overlapped
if (!fSuccess)
{
_tprintf(TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError());
return -1;
}
printf("\nMessage sent to server, receiving reply as follows:\n");
});
while (1) // main thread always reading
{
do
{
// Read from the pipe.
fSuccess = ReadFile(
hPipe, // pipe handle
chBuf, // buffer to receive reply
BUFSIZE * sizeof(TCHAR), // size of buffer
&cbRead, // number of bytes read
NULL); // not overlapped
if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
break;
_tprintf(TEXT("\"%s\"\n"), chBuf);
} while (!fSuccess); // repeat loop if ERROR_MORE_DATA
if (!fSuccess)
{
_tprintf(TEXT("ReadFile from pipe failed. GLE=%d\n"), GetLastError());
return -1;
}
}
t.join();
我希望有人可以更正该代码以使其正常工作,或者您能说出标准做法或建议吗?
非常感谢!
来自 CreateFile
关于 FILE_FLAG_OVERLAPPED
的文档
If this flag is specified, the file can be used for simultaneous
read and write operations.
If this flag is not specified, then I/O operations are serialized
I/O 操作被序列化意味着新的 I/O 请求将等到之前的请求未完成。所以即使在这里使用多线程也无济于事,如果你不使用 FILE_FLAG_OVERLAPPED
。例如,您可以从一个线程开始读取操作并等待数据不存在。如果您从另一个线程调用此文件的写入 - 写入将在 I/O 子系统代码中等待,直到您的读取未完成。即使您说查询文件名(通过 GetFileInformationByHandleEx
和 FileNameInfo
),此请求也会被序列化并等待您的读取未完成。
因此,当您创建文件时,同时 I/O 操作(不仅是读写,而且所有)的唯一选项使用 FILE_FLAG_OVERLAPPED
。
我想在windowsOS中写一个namedpipe客户端,即使客户端正在接收数据,也可以随时发送数据。MSDN的Example只显示发送数据后receive something.And 串行操作不是我想要的。因为我在客户端和服务器之间传输的数据不是很大,也就是说IO操作应该不是一个耗时的过程,所以我没有在客户端使用OVERLAP
。
我在客户端的MSDN示例中修改的代码如下: 主线程不断读取数据,子线程不断向服务器发送数据。但是调试的时候服务器读取数据时卡住了
std::thread t([&] {
cbToWrite = (lstrlen(lpvMessage) + 1) * sizeof(TCHAR);
_tprintf(TEXT("Sending %d byte message: \"%s\"\n"), cbToWrite, lpvMessage);
fSuccess = WriteFile(
hPipe, // pipe handle
lpvMessage, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL); // not overlapped
if (!fSuccess)
{
_tprintf(TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError());
return -1;
}
printf("\nMessage sent to server, receiving reply as follows:\n");
});
while (1) // main thread always reading
{
do
{
// Read from the pipe.
fSuccess = ReadFile(
hPipe, // pipe handle
chBuf, // buffer to receive reply
BUFSIZE * sizeof(TCHAR), // size of buffer
&cbRead, // number of bytes read
NULL); // not overlapped
if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
break;
_tprintf(TEXT("\"%s\"\n"), chBuf);
} while (!fSuccess); // repeat loop if ERROR_MORE_DATA
if (!fSuccess)
{
_tprintf(TEXT("ReadFile from pipe failed. GLE=%d\n"), GetLastError());
return -1;
}
}
t.join();
我希望有人可以更正该代码以使其正常工作,或者您能说出标准做法或建议吗? 非常感谢!
来自 CreateFile
关于 FILE_FLAG_OVERLAPPED
If this flag is specified, the file can be used for simultaneous read and write operations.
If this flag is not specified, then I/O operations are serialized
I/O 操作被序列化意味着新的 I/O 请求将等到之前的请求未完成。所以即使在这里使用多线程也无济于事,如果你不使用 FILE_FLAG_OVERLAPPED
。例如,您可以从一个线程开始读取操作并等待数据不存在。如果您从另一个线程调用此文件的写入 - 写入将在 I/O 子系统代码中等待,直到您的读取未完成。即使您说查询文件名(通过 GetFileInformationByHandleEx
和 FileNameInfo
),此请求也会被序列化并等待您的读取未完成。
因此,当您创建文件时,同时 I/O 操作(不仅是读写,而且所有)的唯一选项使用 FILE_FLAG_OVERLAPPED
。