Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.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 服务体系结构-inputStream正在丢失字节_Java_Inputstream_Outputstream - Fatal编程技术网

Java 服务体系结构-inputStream正在丢失字节

Java 服务体系结构-inputStream正在丢失字节,java,inputstream,outputstream,Java,Inputstream,Outputstream,我的服务器中有一种服务架构,监听TCP IP套接字 messageType = protocolHandler.getMessageType(); ITaskProtocol serviceProtocol = serverServices.get(messageType); if (p != null) { String result = serviceProtocol.handleTask(protocolHandler); } 我正在哈希映射中存储特定任务的协议。 我的问题是,应

我的服务器中有一种服务架构,监听TCP IP套接字

messageType = protocolHandler.getMessageType();
ITaskProtocol serviceProtocol = serverServices.get(messageType);
if (p != null) {
    String result = serviceProtocol.handleTask(protocolHandler);
}
我正在哈希映射中存储特定任务的协议。 我的问题是,应该处理该任务的协议在inputStream中找不到任何字节。inputStream和socked位于“protocolHandler”内部

例如:

public class ProtocolHandler implements ITaskProtocolHandler {
    @Override
    public int getMessageType() throws IOException {
        return new DataInputStream(stream).readInt();
    }
但是,我可以看到(由于调试)消息已发送。在映射中还找到了协议(服务),并调用了服务的“handleTask(…)”方法。但是,服务没有收到任何消息,因为字节丢失,协议正在等待对方

我的猜测是,搜索服务需要的时间太长,同时消息也丢失了

重要信息: 有时有效,有时无效。客户端和服务器在同一台pc上运行,这可能是线程问题

客户端协议:

clientWorker.send(ServerProtocol.MessageRequestType.JoinTopicRequest.getRequestNumber());
clientWorker.send("some xml-message");
服务器服务协议:

clientWorker.send(ServerProtocol.MessageRequestType.JoinTopicRequest.getRequestNumber());
clientWorker.send("some xml-message");
public String handleTask(ITaskProtocolHandler protocolHandler) throws Exception {
    Message m = protocolHandler.getMessage());
建筑有什么问题


非常感谢

这是一个猜测,但可能是你的问题。你的线程是否被阻塞并等待数据

如果是这样,那么您需要修复请求套接字上的流的顺序,您需要首先从套接字获取inputStream,然后请求outputStream,否则您将在阻塞的线程上等待数据

我希望这能解决你的问题

有关完整的解释,请参阅本帖。


问题解决了!我有一个阅读问题。。。“totalRead+=nRead;”已丢失

    int size = readInt();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead = 0;
    int totalRead = 0;
    byte[] data = new byte[1024];

    while (totalRead < size) {
        int read;
        if (data.length <= (size - totalRead))
            read = data.length;
        else
            read = (size - totalRead);
        if ((nRead = inputStream.read(data, 0, read)) == -1)
            throw new IOException("End of stream");
        buffer.write(data, 0, nRead);
        totalRead += nRead;
    }
int size=readInt();
ByteArrayOutputStream缓冲区=新建ByteArrayOutputStream();
int nRead=0;
int totalRead=0;
字节[]数据=新字节[1024];
while(总读取<大小){
int-read;

if(data.length)有太多重要的线程。一个是客户端,一个是服务器。如果等待数据,每个线程都会阻塞。这些流像这样存储在服务器
public ProtocolHandler(Socket Socket)抛出IOException{this.Socket=Socket;this.outputStream=Socket.getOutputStream();this.inputStream=socket.getInputStream();}
您必须匹配我添加的链接上解释的顺序,祝您好运:)顺序正确!我发现了问题!这是一个阅读问题:(