You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Michael Frumin <mf...@gghcwest.com> on 2002/04/30 00:06:50 UTC

Problems with Xerces SAX parser closing my socket?!?!?!

Hello,
	I'm writing an application in Java that is using the xerces SAX
parser to parse incoming requests and I'm encountering a problem where,
after the client writes all of its requests and does a TCP half-close,
the parser returns but the socket that the parser is reading from (via
an InputSource created off the socket) seems to be closed, even for
output.  Writes to the socket after the parse() function returns
generate the following exception:

java.io.IOException: Bad file descriptor
        at java.net.SocketOutputStream.socketWrite(Native Method)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:71)

If anyone else has encountered this problem, and maybe has a solution,
please let me know.

[Following is a more detailed explanation of the server class in
question]

	This server (embodied primarily in the below ConnectionHandler
class) is designed to be a one-shot request type deal where the client
writes all of the request XML and half-closes the socket.  During the
SAX parse, the request-handing objects are dispatched (with a handle to
the ConnectionHandler) and asynchronously do their duty, writing their
output when appropriate, and letting the ConnectionHandler know they are
finished.  When all requests have finished, the ConnectionHandler closes
the other half of the socket and terminates.

Thanks,
mike


########################################################################
##
package com.gghc.multicello;

import java.util.*;
import java.net.*;
import java.io.*;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;


public class ConnectionHandler extends Thread
{
    private Socket    m_sock   = null;
    private XMLReader m_parser = null;
    private OutputStream m_outStream = null;
    private Hashtable m_requests = null;
    private boolean m_oneShot = true;

    public ConnectionHandler(Socket socket) throws
org.xml.sax.SAXException, java.io.IOException
    {
        m_sock = socket;
	m_outStream = m_sock.getOutputStream();
	m_requests = new Hashtable();
        m_parser =
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
    }
    
    public synchronized void run()
    {
        System.err.println("Firing up SAX parser");
        
        try {
    
            InputSource src = new InputSource(m_sock.getInputStream());

	    RequestStreamParser rp = new RequestStreamParser(this,
m_parser);

	    m_parser.setContentHandler(rp);
	    m_parser.setErrorHandler(rp);

	    output("<responseStream>\n");
	    
            m_parser.parse(src);


            System.err.println("Parser returned from parse()");

	    // if there are any requests still open, wait()
	    if(m_requests.size() > 0) {
		System.err.println("wait()'ing");
		wait();
	    }
	    output("</responseStream>");
	    m_sock.shutdownOutput();
	    m_sock.close();
	    
	    
        } catch (Exception e) {
            System.out.println("Exception caught in CH::run(): " + e);
	    e.printStackTrace();
        }

    }

    public synchronized int output(String output) {
	try {
	    System.err.println("Sending: " + output.length() + "
bytes");

	    m_outStream.write(output.getBytes());
	    m_outStream.flush();
	    //System.err.println("Sent: " + output);
	    int sent =0;
	    System.err.println("Sent: " + output.length() + " bytes");
	    return 0;
	}
	catch(IOException e) {
	    System.err.println("Exception in output: " + e.getClass() +
e);
	    e.printStackTrace();
	    return -1;
	}
    }

    public boolean oneShot() {
	return m_oneShot;
    }
    

    public synchronized void request(Object o, boolean status) {
	System.err.println(o + " " + (status ? "attached" :
"detached"));

	if(status) {
	    m_requests.put(o, new Boolean(status));
	}
	else {
	    if(m_requests.containsKey(o)) {
		m_requests.remove(o);
		if(m_requests.size() == 0) {
		    requestsFinished();
		}
mmm	    }
	}
    }

    private synchronized void requestsFinished() {
	System.err.println("All requestsFinished");
	notifyAll();
    }    

}


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org