Link to home
Start Free TrialLog in
Avatar of joannayang
joannayang

asked on

how to fix broken pipe error in Java Application running on RedHat

I have a Java proxy server, which is public to the client,  the client connects to the destination server through proxy server with SSL. The proxy server and destination server both got "Broken Pipe" Error, when the servers sent out xml messages. this happens intermittently. BTW, everything was fine when applications was running on windows until they were moved on to Linux. Can some experts help me out?

Following is the part code of proxy server pipe for fowarding messages to the destination server and sending messages back to the client.

protected class Pipe extends Thread
  {
    private BufferedInputStream inStream;
    private BufferedOutputStream outStream;
    private String label;
   
    public Pipe(Socket in, Socket out, String label) throws IOException
    {
        super("Pipe");
      inStream = new BufferedInputStream(in.getInputStream(), BUFFER_SIZE);
      outStream = new BufferedOutputStream(out.getOutputStream(), BUFFER_SIZE);
      this.label = label + " " + in.getInetAddress().getHostAddress()
      + " -> " + out.getInetAddress().getHostAddress();
    }
   
    public void run()
    {
      byte[] bytes = new byte[BUFFER_SIZE];
      try
      {
        while (isRunning())
        {
          int count = inStream.read(bytes);
         
          if (count > 0)
          {
            long readTime = System.currentTimeMillis();
            outStream.write(bytes,0,count);
            outStream.flush();
            Thread.yield();
           
            //transfer statistics
            getServerTypeStatistics().
            stat(ProxyByTypeStatistics.TRANSFER,count);
          }
          else if (count < 0)
            throw new EOFException("Normal End of Stream");
          else if (count == 0)
            try
            {
              Thread.sleep(SLEEP_TIME);
            }
            catch (InterruptedException ie)
            {
            }
        }
      }
      catch (EOFException eofe)
      {
        if ( running )
        {
          Log.getLog().log("INFO",label+" EOF: Socket closed");
          shutdown();
        }
      }
      catch (IOException ioe)
      {
        if ( running )
        {
          Log.getLog().log("ERROR","PIPE ERROR"+label);
          Log.getLog().log("ERROR",ioe);

          shutdown();
        }
      }
      catch (Throwable e)
      {
          Log.getLog().log("ERROR",e);
      }
      finally
      {
       
        try
        {
          inStream.close();
          outStream.close();
        }
        catch (IOException ioe)
        {
        }
      }
    }
  }
}
SOLUTION
Avatar of bala_72
bala_72

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joannayang
joannayang

ASKER

Thanks for first response.
what could cause the broken pipe? why the socket was killed? my application will never kill it when the servers are running.
Someone said it maybe caused by the network, or the data has bad charactor, or the outStream is filling up faster than it is read off.
any idea about these?
Thanks
it is socket connection, basically, the proxy establishs the connection for the client and the destination server.

OK

Someone said it maybe caused by the network, or the data has bad charactor, or the outStream is filling up faster than it is read off.

It may happen some time, you can try by making sender and receiver in seperate thread.
do you know whether it is linux resources issues? I have my application running on windows 2000 for a year, never had this type of error, but once I moved to linux, it happened. Someone said it maybe resources issues( memory, cpu, multi-threads limits or message buffer limits). What do you think? Thanks Very Much!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
memory corruption? how could that happen? Do you mean I probably had "OutOfMemory" error or Physical memory has problem? If it was memory corruption, how do I prevent it or fix it? Your metion about memory corruption did reminds me I had two identical servers running at the same time, the one had broken pipe errors with very high memory usage, could it be the problem?

Thanks
In the case of memory problem then linux would have given Segmentation fault.

Also this code was working fine in windows and hence there will not be memory problem
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
still could not figure it out and fix it. If any of you have a great idea, please give me some suggestions!
Thanks!
what are the debugging steps you took so far ?