NamedPipe c ++和Java应用程序之间的进程间

时间:2015-01-23 06:25:27

标签: java c++ windows interprocess

我需要从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;
            }
        }
    }
}

它可以连接到管道而没有任何问题,但唯一的问题是java端无法获取在C ++进程完成之前写入的数据(从我意味着终止调试器)。

只要我的C ++进程仍在运行,它就会挂起readLine:     line = pipe.readLine()

有人对此有任何想法吗?

2 个答案:

答案 0 :(得分:1)

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

Java不是我的事,但我猜测来自服务器的响应还必须包含一个新行,以便readLine返回。

答案 1 :(得分:1)

如果您在readLine()中被阻止,则对等方不会发送线路。它在对等体退出时解锁的原因是读者遇到流的结束,并返回到目前为止收到的数据。

您需要找出对等端发送的内容并使用合适的读取方法。显然它不是行,所以readLine()是错误的工具。