Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 客户端服务器,设计协议_Java_Jaxb_Client Server_Unmarshalling - Fatal编程技术网

Java 客户端服务器,设计协议

Java 客户端服务器,设计协议,java,jaxb,client-server,unmarshalling,Java,Jaxb,Client Server,Unmarshalling,我在套接字上编写客户机-服务器应用程序,我的任务是设计自己的协议。客户端和服务器使用xml进行通信。我使用JAXB库。客户端完美地将XML写入输出流。但我无法在服务器中读取它。您能说明如何正确地从客户端接收数据吗 这是一个客户端: public class Client { public static final String SERVER_HOST = "localhost"; public static final Integer SERVER_PORT = 4444;

我在套接字上编写客户机-服务器应用程序,我的任务是设计自己的协议。客户端和服务器使用xml进行通信。我使用JAXB库。客户端完美地将XML写入输出流。但我无法在服务器中读取它。您能说明如何正确地从客户端接收数据吗

这是一个
客户端

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);
            }
        }
    }
}
public类ServerThread扩展线程{
私有静态最终记录器LOG=Logger.getLogger(ServerThread.class);
专用插座;
中的私有缓冲区读取器;
私人打印输出;
私有字符串缓冲区;
公共服务器线程(套接字)引发IOException{
插座=s;
in=新的BufferedReader(新的InputStreamReader(socket.getInputStream());
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()),true);
start();
}
公开募捐{
试一试{
while(true){
//if((buffer=in.readLine()).endsWith(“”){
JAXBContext JAXBContext=JAXBContext.newInstance(XMLProtocol.class);
解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller();
sXMLProtocol=(XMLProtocol)jaxbUnmarshaller.unmarshal(in);
LOG.trace(“从用户获取消息”+protocol.getContent());
}
日志跟踪(“Nop”);
}
}捕获(JAXBEException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
socket.close();
日志跟踪(“套接字关闭”);
}捕获(IOE异常){
日志错误(“插座未关闭”+e);
}
}
}
}
我应该在服务器上写些什么来解析我得到的XML?我尝试解析InputStream

编辑我的问题:
现在它向我显示异常。我改变了马歇尔参数

DefaultValidationEventHandler:[致命错误]:元素类型“xmlProtent”后面必须跟属性规范“>”或“/>”。 地点:2号线 javax.xml.bind.UnmarshaleException:prolog中不允许包含内容。 -除此之外: [org.xml.sax.SAXParseException;行号:1;列号:1;prolog中不允许包含内容。] DefaultValidationEventHandler:[致命错误]:prolog中不允许包含内容。 地点:1号线 位于com.sun.xml.bind.v2.runtime.SAXUnmarshallerHandlerImpl.handleEvent(SAXUnmarshallerHandlerImpl.java:870) 位于com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.propagateEvent(ErrorHandlerAdaptor.java:82) 位于com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.fatalError(ErrorHandlerAdaptor.java:58) 位于com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:180) 请访问com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441) 请访问com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368) 在com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1375) 在com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:996) 位于com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607) 在com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116) 位于com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488) 位于com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835) 位于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) 位于com.sun.org.apache.xerces.internal.jaxp.saxpasserimpl$jaxpsaxpasser.parse(saxpasserimpl.java:568) 位于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) 位于dataart.practice.server.ServerThread.run(ServerThread.java:41) 原因:org.xml.sax.saxpasseeption;行号:1;列数:1;prolog中不允许包含内容。 位于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.UnmarshaleException:元素类型“xmlProtent”后面必须跟属性规范“>”或“/>”。 -除此之外: [org.xml.sax.saxpasseeption;行号:2;列号:12;元素类型“xmlProtent”后面必须跟属性规范“>”或“/>” 位于com.sun.xml.bind.v2.runtime.SAXUnmarshallerHandlerImpl.handleEvent(SAXUnmarshallerHandlerImpl.java:870) 位于com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.propagateEvent(ErrorHandlerAdaptor.java:82) 位于com.sun.xml.bind.v2.runtime.ErrorHandlerAdaptor.fatalError(ErrorHandlerAdaptor.java:58) 位于com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:180) 请访问com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441) 在com.sun.org
    (buffer = in.readLine()).endsWith("</xmlProtocol>")