CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    3

    IO Handling Problem

    Hi,

    I'm in a big mess and kindly need your advices(and it's kind of urgent too)..

    I have written a Named Pipe client in java where the Named Pipe server is in C++ and cannot be modified. In the client, i have used java's "RandomAccessFile" to create a bidirectional pipe to the server.

    From another class a separate thread is continuously trying to read the Named Pipe Client where the main thread tries to write to the Named Pipe Client when required.

    In the ideal situation i want the Named Pipe Client's read method to return -1 or null when there in no incoming stream from the server and sleep for 100ms (which will allow the main thread to perform write operations if required) and then go for the next iteration of listening. Or at-least implementing a timeout for the read function is required.

    I can not change the server/client communication to sockets since server is already implemented in C++ and can not be altered.

    Please give me a hit to solve this...
    Thanks!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: IO Handling Problem

    Please can you show the relevant bits of your code.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2010
    Posts
    3

    Re: IO Handling Problem

    This is the code segment which create the pipe
    Code:
    private RandomAccessFile rafPipe = null;
    ....
    //create the pipe
    try {
    	rafPipe = new RandomAccessFile( pipeName, "rws");
    } catch (FileNotFoundException e) {
    	System.out.println("FileNotFound exception while creating the pipe...");
    	e.printStackTrace();
    }
    This is the write function
    Code:
    public boolean write(byte[] m_pTxDataBuffer, short totalpktLength) { 
    	boolean wSuccess = false;
    	//m_pTxDataBuffer is a pointer to the buffer that contains data to be written
    	if(totalpktLength<0){
    		totalpktLength = 0;
    	}
    	try {
    		rafPipe.write(m_pTxDataBuffer, 0, totalpktLength);
    		wSuccess = true;
    	}catch (IOException e){
    		System.out.println("IOException while writing to the pipe...");
    		e.printStackTrace();
    	}
    	return wSuccess;
    }
    This is the read function
    Code:
    public int Read(byte[] pchBuff, int inBufferSize) {
    	int byteTransfer = 0;
    	if(inBufferSize<0){
    		inBufferSize = 0;
    	}
    	try {
    		byteTransfer = rafPipe.read(pchBuff, 0, inBufferSize);
    	}catch (IOException e) {
    		System.out.println("IO Exception while reading from the pipe.");
    		e.printStackTrace();
    	}
    	return byteTransfer;
    }

  4. #4
    Join Date
    Oct 2010
    Posts
    6

    Re: IO Handling Problem

    i am thinking of if nothing in inputstream, sleep for 100ms, this is if your read is in a seperate thread from your write


    if(Inputstream.available()==0) Thread.sleep(100);

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: IO Handling Problem

    Use the getChannel() method to get a FileChannel which can be read from without blocking.

    If you are reading and writing to the same file on separate threads you may also want to implement some form of locking to ensure the read thread doesn't attempt to read whilst a write is taking place.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Oct 2010
    Posts
    3

    Smile Re: IO Handling Problem

    Hi,

    Finally I managed to improve the code to avoid it from getting blocked until at-least one byte is read from its read() method. To implement that, before entering I check the length() of the pipe by invoking
    Code:
    rafPipe.lengthz()
    , if the value returned is greater than "0", I invoke the read() method or otherwise avoid it. Likewise I managed to overcome the undesirable state arose because of using RandomAccessFile to implement a bidirectional named pipe.

    Thanks for all your comments . I tried all of it, but finally only this method, worked for me..

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured