0

我在套接字上编写客户端-服务器应用程序,我的任务是设计自己的协议。客户端和服务器使用 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);
            }
        }
    }
}

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

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

DefaultValidationEventHandler:[FATAL_ERROR]:元素类型“xmlProtent”必须后跟属性规范“>”或“/>”。位置:第 2 行 javax.xml.bind.UnmarshalException:prolog 中不允许内容。- 带有链接异常:[org.xml.sax.SAXParseException; 行号:1;列号:1;序言中不允许内容。] DefaultValidationEventHandler:[FATAL_ERROR]:序言中不允许内容。位置:com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.propagateEvent(ErrorHandlerAdaptor.java:82) com.sun.xml.bind.v2.runtime.SAXUnmarshallerHandlerImpl.handleEvent(SAXUnmarshallerHandlerImpl.java:870) 的第 1 行在 com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.fatalError(ErrorHandlerAdaptor.java: AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214) at dataart.practice.server.ServerThread.run(ServerThread.java:41) 引起:org.xml.sax.SAXParseException; 行号:1;列号:1;序言中不能有内容。在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198) 在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177) ... 16 更多 javax.xml.bind.UnmarshalException:元素类型“xmlProtent”必须后跟属性规范“>”或“/>”。- 带有链接异常:[org.xml.sax.SAXParseException; 行号:2;列号:12;元素类型“xmlProtent” 160) 在 javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214) 在 dataart.practice.server.ServerThread.run (ServerThread.java:41) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 在 java.lang.Thread。 run(Thread.java:722) 引起:org.xml.sax.SAXParseException; 行号:2;列号:12;元素类型“xmlProtent”必须后跟属性规范“>”或“/>”。在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.

4

1 回答 1

0

你试过调试线路吗

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

也许它的结果是错误的

于 2012-06-08T09:10:51.337 回答