客户端服务器,设计协议

时间:2012-06-08 09:04:15

标签: java jaxb client-server unmarshalling

我在套接字上编写客户端 - 服务器应用程序,我有设计自己的协议的任务。客户端和服务器与xml通信。我使用JAXB库。客户端完美地将XML写入输出流。但我无法在服务器中阅读它。您能否展示如何正确地从客户端收回数据?

这是Client

public class Client {
    public static final String SERVER_HOST = "localhost";
    public static final Integer SERVER_PORT = 4444;
    public static final Logger LOG = Logger.getLogger(Client.class);

    public static void main(String[] args) throws IOException {

        System.out.println("Welcome to Client side");
        XMLProtocol protocol = new XMLProtocol();
        Socket fromserver = null;

        fromserver = new Socket(SERVER_HOST, SERVER_PORT);

        BufferedReader in = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));

        PrintWriter out = new PrintWriter(fromserver.getOutputStream(), true);

        BufferedReader inu = new BufferedReader(new InputStreamReader(System.in));

        String fuser, fserver;

        while ((fuser = inu.readLine()) != null) {

            protocol.setComId((long) 0);
            protocol.setContent("Client content");
            protocol.setLogin("User");

            try {

                JAXBContext jaxbContext = JAXBContext.newInstance(XMLProtocol.class);
                Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                jaxbMarshaller.marshal(protocol, out);

            } catch (JAXBException e) {
                LOG.error("Error while processing protocol"+e);             
            }

            fserver = in.readLine();

            System.out.println(fserver);

            if (fuser.equalsIgnoreCase("close"))
                break;
            if (fuser.equalsIgnoreCase("exit"))
                break;
        }

        out.close();
        in.close();
        inu.close();
        fromserver.close();
    }

}

和服务器:

package dataart.practice.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.log4j.Logger;

public class Server {

    public static final Logger LOG = Logger.getLogger(Server.class);
    public static final String SERVER_HOST = "localhost";
    public static final Integer SERVER_PORT = 4444;
    public static Integer userCount = 0;

    public static void main(String[] args) throws IOException {

        LOG.trace("Server started");
        ServerSocket s = new ServerSocket(SERVER_PORT);

        try {
            while (true) {
                LOG.trace("Waiting for connections...");
                Socket socket = s.accept();

                try {
                    new ServerThread(socket);
                    LOG.trace("Users in the chat: "+(userCount+1));
                    userCount++;

                } catch (IOException e) {
                    socket.close();
                }
            }
        } finally {
            s.close();
        }
    }
}

我的主题。

public class ServerThread extends Thread {
    private static final Logger LOG = Logger.getLogger(ServerThread.class);
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private String buffer;
        public ServerThread(Socket s) throws IOException {
        socket = s;
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
        start();
    }

    public void run() {
        try {
            while (true) {

            //  if ((buffer = in.readLine()).endsWith("</xmlProtocol>")) {
                    JAXBContext jaxbContext = JAXBContext.newInstance(XMLProtocol.class);
                    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                    sXMLProtocol protocol = (XMLProtocol) jaxbUnmarshaller.unmarshal(in);
                    LOG.trace("Getting message from user" + protocol.getContent());
                }
                LOG.trace("Nop");
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                socket.close();
                LOG.trace("Socket closed");
            } catch (IOException e) {
                LOG.error("Socket no closed" + e);
            }
        }
    }
}

我应该在Server上编写什么来解析我得到的XML?我尝试解析InputStream。

Edit my question: 现在它显示异常。我改变了马歇尔参数。

  

DefaultValidationEventHandler:[FATAL_ERROR]:元素类型&#34; xmlProtent&#34;必须遵循属性规范,&#34;&gt;&#34;或&#34; /&gt;&#34;。            地点:2号线       javax.xml.bind.UnmarshalException:prolog中不允许使用内容。         - 链接异常:       [org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;序言中不能有内容。]       DefaultValidationEventHandler:[FATAL_ERROR]:prolog中不允许使用内容。            地点:1号线           at com.sun.xml.bind.v2.runtime.SAXUnmarshallerHandlerImpl.handleEvent(SAXUnmarshallerHandlerImpl.java:870)           at com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.propagateEvent(ErrorHandlerAdaptor.java:82)           at com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.fatalError(ErrorHandlerAdaptor.java:58)           at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:180)           at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)           at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)           at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1375)           at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(XMLDocumentScannerImpl.java:996)           at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)           at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)           at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)           在com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)           at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)           在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)           在com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)           at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(SAXParserImpl.java:568)           at com.sun.xml.bind.v2.runtime.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:160)           在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)           在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)           at dataart.practice.server.ServerThread.run(ServerThread.java:41)       引起:org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;序言中不能有内容。           at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)           at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)           ......还有16个       javax.xml.bind.UnmarshalException:元素类型&#34; xmlProtent&#34;必须遵循属性规范,&#34;&gt;&#34;或&#34; /&gt;&#34;。         - 链接异常:       [org.xml.sax.SAXParseException; lineNumber:2; columnNumber:12;元素类型&#34; xmlProtent&#34;必须遵循属性规范,&#34;&gt;&#34;或&#34; /&gt;&#34;。]           at com.sun.xml.bind.v2.runtime.SAXUnmarshallerHandlerImpl.handleEvent(SAXUnmarshallerHandlerImpl.java:870)           at com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.propagateEvent(ErrorHandlerAdaptor.java:82)           at com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.fatalError(ErrorHandlerAdaptor.java:58)           at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:180)           at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)           at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)           at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1375)           at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.seekCloseOfStartTag(XMLDocumentFragmentScannerImpl.java:1354)           at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:245)           at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl $ NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602)           at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3063)           at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(XMLDocumentScannerImpl.java:881)           at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)           at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)           at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)           在com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)           at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)           在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)           在com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)           at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(SAXParserImpl.java:568)           at com.sun.xml.bind.v2.runtime.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:160)           在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)           在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)           at dataart.practice.server.ServerThread.run(ServerThread.java:41)           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)           at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)           在java.lang.Thread.run(Thread.java:722)       引起:org.xml.sax.SAXParseException; lineNumber:2; columnNumber:12;元素类型&#34; xmlProtent&#34;必须遵循属性规范,&#34;&gt;&#34;或&#34; /&gt;&#34;。           at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)           at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)           ......还有23个

1 个答案:

答案 0 :(得分:0)

您是否尝试过调试行

    (buffer = in.readLine()).endsWith("</xmlProtocol>")

可能结果是假的