gpt4 book ai didi

java - C++ 和 Java 应用程序之间的 NamedPipe 交互进程

转载 作者:行者123 更新时间:2023-11-28 02:34:11 24 4
gpt4 key购买 nike

我需要从 C++ 端启动一个命名管道服务器,并让一个 Java 应用程序从管道中读取。

对于创建管道的 C++ 端,我遵循了 MSDN 中的示例: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365603(v=vs.85).aspx

从 Java 端,我做了一段 hacky 代码来测试:

Thread readerThread = new Thread( new Runnable()
{
@Override
public void run()
{
String line = null;
while( true )
{
try
{
final RandomAccessFile pipe =
new RandomAccessFile( "\\\\\\\\.\\\\pipe\\\\smarts_interprocess", "r" );
while( null != ( line = pipe.readLine() ) )
{
s_logger.warning( line );
}
}
catch( IOException ex )
{
s_logger.severe( ex.getMessage() );
try
{
Thread.sleep( 1000 );
}
catch( InterruptedException e )
{
s_logger.info( "nothing available, I will sleep for a second" );
}
}
}
}
} );
readerThread.start();

C++ 端代码:

void Pipe::startMessageLoop()
{
DWORD dwWait{};
BOOL fSuccess;

fPendingIO = ConnectToNewClient(m_inputHandle, &oOverlap_);

while (true)
{
dwWait = ::WaitForSingleObject(oOverlap_.hEvent, INFINITE);

if (dwWait == WAIT_FAILED)
{
::CloseHandle(oOverlap_.hEvent);
std::cout << "failed to WaitForSingleObject.." << std::endl;
}

if (fPendingIO)
{
DWORD cbRet;
fSuccess = GetOverlappedResult(
m_inputHandle, // handle to pipe
&oOverlap_, // OVERLAPPED structure
&cbRet, // bytes transferred
FALSE); // do not wait

switch (dwState)
{
// Pending connect operation
case CONNECTING_STATE:
if (!fSuccess)
{
printf("Error %d.\n", GetLastError());
return;
}
dwState = WRITING_STATE;
break;

// Pending write operation
case WRITING_STATE:
if (!fSuccess)
{
DisconnectAndReconnect();
continue;
}
break;

default:
{
printf("Invalid pipe state.\n");
return;
}
}
}


std::string message = "naive message from server";
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring data = converter.from_bytes(message.c_str());
DWORD numBytesWritten = 0;

switch (dwState)
{
case WRITING_STATE:

fSuccess = WriteFile(
m_inputHandle, // handle to our outbound pipe
data.c_str(), // data to send
wcslen(data.c_str()) * sizeof(wchar_t), // length of data to send (bytes)
&numBytesWritten, // will store actual amount of data sent
NULL // not using overlapped IO
);

// FlushFileBuffers(m_inputHandle);
// The write operation completed successfully.
if (fSuccess)
{
fPendingIO = FALSE;
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
dwState = WRITING_STATE;
continue;
}

// The write operation is still pending.
if (!fSuccess && (GetLastError() == ERROR_IO_PENDING))
{
fPendingIO = TRUE;
continue;
}
// An error occurred; disconnect from the client.
DisconnectAndReconnect();
break;

default:
{
printf("Invalid pipe state.\n");
return;
}
}
}
}

它可以毫无问题地连接到管道,但唯一的问题是在 C++ 进程完成之前,java 端无法获取写在 c++ 端的数据(我的意思是从 VS 终止调试器)。

只要我的 C++ 进程还在运行,它就会卡在 readLine 的点上: line = pipe.readLine()

有人对此有任何想法吗?

最佳答案

据我所知 - 如果您在服务器端使用了完全相同的 MSDN 代码 - 服务器在将任何内容写入管道之前正在等待输入。如果您的客户端从不发送任何内容,则 pipe.readLine 将阻塞,直到连接被切断。

Java 不是我的菜,但我猜服务器的响应还必须包含一个换行符,以便 readLine 返回。

关于java - C++ 和 Java 应用程序之间的 NamedPipe 交互进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28104183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com