[jdom-interest] Re: Problems with JSP/JDOM

Per Norrman per.norrman at austers.se
Thu Sep 23 08:27:52 PDT 2004


Flávio Marim wrote:

> Yes, I see. I tried to entabilish a protocol that writes first the buffer
> size to be allocated and then the data itself. But for some reason, the
> size passed corresponded only the first line of my text. It seems to stop
> at the first line-break.
> I'm thinking about sending plain text and stop receiving it when </main>
> tag appears... is there some "famous" guy who thought about this before
> ;-) ?
> 

Maybe the two attached examples can help you. SocketWriter spawns a number
of threads that just writes a string to a socket using DataOutputStream.
SocketWriter receives the data using DataInputStream and builds a Document.

/pmn

-------------- next part --------------
package socket;
import java.io.DataOutputStream;
import java.net.Socket;

/**
 * @author Per Norrman
 *
 */
public class SocketWriter {
    

	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 10; ++i)
			new Client().start();
	}

	public static class Client extends Thread {

		private String _fileName;
        private String _xml = "<root><child id='1'/><child id='2'/></root>";

		public Client() {
			super("Client");
		}

		public void run() {
			try {
				Socket s = new Socket("localhost", 4711);
                byte[] buffer = _xml.getBytes();
                DataOutputStream out = new DataOutputStream(s.getOutputStream());
                out.writeInt(buffer.length);
                out.write(buffer);
				Thread.sleep(1000);
                System.out.println("closing");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
-------------- next part --------------
package socket;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.jdom.input.SAXBuilder;

/**
 * @author Per Norrman
 *
 */
public class SocketReader {

	public static void main(String[] args) throws Exception {
		new Server().start();
	}

	public static class Server extends Thread {
		ServerSocket _ss;

		public Server() throws Exception {
			super("Server");
			_ss = new ServerSocket(4711);
		}

		public void run() {
			try {
				while (true) {
					Socket cs = _ss.accept();
					new Parser(cs).start();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public static class Parser extends Thread {
		private Socket _s;
		private static int counter = 0;

		public Parser(Socket s) {
			super("Parser-" + (++counter));
			_s = s;
		}

		public void run() {
			try {
				DataInputStream in = new DataInputStream(_s.getInputStream());
				int length = in.readInt();
				byte[] data = new byte[length];
				in.readFully(data);
				System.out.println(getName() + ": start ..");
				new SAXBuilder().build(new ByteArrayInputStream(data));
				System.out.println(getName() + ": stop ..");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}


More information about the jdom-interest mailing list