无法从Java中的命名管道读取 [英] Not able to read from named pipe in Java

查看:121
本文介绍了无法从Java中的命名管道读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过调用mkfifo()命令使用JNI创建命名管道。我正在使用mode = 0666。

I am creating a named pipe using JNI by calling the mkfifo() command. I am using mode = 0666.

然后我尝试使用Java写入管道,但是在写入管道时我遇到了问题。我被困在以下一行并且无法通过它。我也没有收到任何错误。

I am then trying to write into the pipe using Java, but I get stuck while writing into the pipe. I get stuck at the following line and not able to go past it. I am not getting any error either.

PrintWriter out = new PrintWriter((new BufferedWriter(new FileWriter(pipePath))));

请帮忙。

问候,

-H

PipePeer.java

import java.io.*;*

public class PipePeer {

    private native int createPipe(String pipeName, int mode);* // --------> native method to call mkfifo(pipeName, mode); 
    static {
        System.load("/home/user/workspace/Pipes/libpipe.so");
    }

    public static void main(String[] args) {

        PipePeer p = new PipePeer();
        PipeImplement pi = new PipeImplement();
        String pipePath = "/home/user/workspace/Pipes/pipeFolderpipe1";
        int mode = 0777;
        int createResult = p.createPipe(pipePath, mode);
        if (createResult == 0)
            System.out.println("Named pipe created successfully");
        else
            System.out.println("Error: couldnot create Named pipe");


        String pipeLocation = "/home/user/workspace/Pipes/pipeFolder/pipe1";
        pi.writePipe("From Java", pipeLocation);
        System.out.println(pi.readPipe(pipeLocation));
    }
}

PipeImplement.java

import java.io.*;

public class PipeImplement {

    public BufferedReader in;
    public void writePipe(String strWrite, String pipePath){
        PrintWriter out = null;
        try {
        //-------------> cannot go past this line <--------------------
            out = new PrintWriter((new BufferedWriter(new FileWriter(pipePath))));
        } catch(IOException e) {
            System.out.println("error while writing");
            e.printStackTrace();
        }
        out.println(strWrite);
        System.out.println("writen");
        out.close();
        System.out.println("close");
    }

    public String readPipe(String pipePath) {
        try {
            in = new BufferedReader(new FileReader(pipePath));
        }catch (FileNotFoundException e) {
           e.printStackTrace();
        }
        String lineRead = "";
        try {
            lineRead = in.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return lineRead;
    }
}


推荐答案

那是如何在* nix上命名管道工作。你在打开管道时遇到了困难。
打开一个写入的fifo将阻止,直到有人打开它进行阅读。

That's how named pipes on *nix work. You're getting stuck at opening the pipe. Opening a fifo for writing will block until someone opens it for reading.

如果你尝试从同一个管道读取/写入管道,你将陷入僵局同步线程,你必须切换到NIO,或者创建一个从fifo读取的线程和一个写入它的线程。

You'll deadlock if you try to read/write to the pipe from the same thread synchronously, you'ld have to either switch to NIO, or create one thread that reads from the fifo and one thread that writes to it.

这篇关于无法从Java中的命名管道读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆