简体   繁体   English

命名管道连接从c ++到Java Line,每个char之间有空格

[英]Named pipe connection from c++ to Java Line with space between each char

Hello, 你好,

I'm transferring data from a c++ program to java using named pipes . 我正在使用命名管道将数据从c ++程序传输到java。

my issue is that each line i read is received with one space (" ") between each char , example : 我的问题是我读取的每一行都在每个字符之间收到一个空格(“”) ,例如:

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

my Q is why is that happening and how can i avoid this . 我的问题是为什么会发生这种情况,我怎么能避免这种情况。

code for the C++ pipe : 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;
}

Code from java that reads the pipe : 来自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();
        }

    }

** if you wonder what happnes in parseData() then dont , cause the spaces are before that in the System.out(Read from pipe :" +data) **如果你想知道parseData()中有什么开心,那么不要,因为空间在System.out之前(从管道读取:“+数据)

Thanks in advance 提前致谢

You're using wide characters when sending and regular java strings when receiving. 您在发送时使用宽字符,在接收时使用常规Java字符串。 wchar_t is usually 2 bytes, so the c++ app sends 2 bytes for every character in your strings. wchar_t通常是2个字节,因此c ++ app为字符串中的每个字符发送2个字节。 The java app then reads that and creates a string based on 1-byte characters. 然后java应用程序读取它并根据1字节字符创建一个字符串。

You need to either use normal characters when sending, or receive them as bytes and convert them before creating a string. 您需要在发送时使用普通字符,或者以字节形式接收它们并在创建字符串之前转换它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM