java - 从 C++ 到 Java 行的命名管道连接,每个字符之间有空格

标签 java c++ string pipe named-pipes

您好,

我正在使用命名管道将数据从 C++ 程序传输到 Java。

我的问题是,我收到的每一行都带有每个字符之间的一个空格 (""),示例:

Sent : 25343,398843292,55871844,492974923,-0.042,-0.022,2.474
Receive : 2 5 3 4 3 , 3 9 8 8 4 3 2 9 2 , 5 5 8 7 1 8 4 4 , 4 9 2 9 7 4 9 2 3 , - 0 . 0 4 2 , - 0 . 0 2 2 , 2 . 4 7 4

我的问题是为什么会发生这种情况以及如何避免这种情况。

C++ 管道代码:

#include "stdafx.h"
///// SERVER PROGRAM /////

#include <iostream>
#include <windows.h>
using namespace std;

int main(int argc, const char **argv)
{
    wcout << "Creating an instance of a named pipe..." << endl;

    // Create a pipe to send data
    HANDLE pipe = CreateNamedPipe(
        L"\\\\.\\pipe\\my_pipe", // name of the pipe
        PIPE_ACCESS_DUPLEX, // 1-way pipe -- send only
        PIPE_TYPE_BYTE, // send data as a byte stream
        1, // only allow 1 instance of this pipe
        100, // no outbound buffer
        100, // no inbound buffer
        0, // use default wait time
        PIPE_ACCEPT_REMOTE_CLIENTS // use default security attributes
    );

    if (pipe == NULL || pipe == INVALID_HANDLE_VALUE) {
        wcout << "Failed to create outbound pipe instance.";
        // look up error code here using GetLastError()
        system("pause");
        return 1;
    }

    wcout << "Waiting for a client to connect to the pipe..." << endl;

    // This call blocks until a client process connects to the pipe
    BOOL result = ConnectNamedPipe(pipe, NULL);
    if (!result) {
        wcout << "Failed to make connection on named pipe." << endl;
        // look up error code here using GetLastError()
        CloseHandle(pipe); // close the pipe
        system("pause");
        return 1;
    }

    wcout << "Sending data to pipe..." << endl;
    int i = 0 ;
    for(i=0 ; i<100 ; i++){
    Sleep(25);

    // This call blocks until a client process reads all the data
    const wchar_t *data = L"25343,398843292,55871844,492974923,-0.042,-0.022,2.474\n";
    //const wchar_t *data = L"1,1,1,1,1,1,1\r\n";
    DWORD numBytesWritten = 0;
    result = WriteFile(
        pipe, // handle to our outbound pipe
        data, // data to send
        wcslen(data) * sizeof(wchar_t), // length of data to send (bytes)
        &numBytesWritten, // will store actual amount of data sent
        NULL // not using overlapped IO
    );


    if (result) {
        wcout << "Number of bytes sent: " << numBytesWritten << endl;
    } else {
        wcout << "Failed to send data." << endl;
        // look up error code here using GetLastError()
    }

    }
    // Close the pipe (automatically disconnects client too)
    CloseHandle(pipe);

    wcout << "Done." << endl;

    system("pause");
    return 0;
}

Java 中读取管道的代码:

public void run() {
        mIsOn = true;
        RandomAccessFile pipe = null;
        try {
            pipe = new RandomAccessFile("\\\\.\\pipe\\my_pipe", "r");
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        // Main loop
        while (mIsOn) {
            String inData="";
            try {
                // Read from the pipe one line at a time
                inData = pipe.readLine();
                if (inData != null) {
                    System.out.println("Read from pipe :" + inData);//**Here it prints with spaces**
                    parseData(inData);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // Close the pipe at the end
        try {
            pipe.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

** 如果你想知道 parseData() 中发生了什么,那么不要,因为空格在 System.out(Read from pipe :"+data) 中的那个之前

提前致谢

最佳答案

发送时使用宽字符,接收时使用常规 java 字符串。 wchar_t 通常为 2 个字节,因此 C++ 应用程序为字符串中的每个字符发送 2 个字节。然后,java 应用程序读取它并基于 1 字节字符创建一个字符串。

您需要在发送时使用普通字符,或者将它们作为字节接收并在创建字符串之前进行转换。

关于java - 从 C++ 到 Java 行的命名管道连接,每个字符之间有空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18735605/

相关文章:

java - 在两点之间绘制正方形

java - 开始使用 Swing?

java - 将文件上传到 Spring 时出现 net::ERR_CONNECTION_ABORTED 的可能原因是什么

C# File.ReadAllBytes 与 std::ifstream (Windows)

c# - C#的string.IndexOf怎么能执行的这么快,比普通的for循环find快10倍?

Java反向字符串

c++ - 关于c++中的 'compare'函数

c++ - 递归类定义会在C++中产生无效的指针

sql - 使用给定模式在 Postgres 中生成列

c# - 检查子字符串的有效方法 C#