Java 输入流数据丢失

Java 输入流数据丢失,java,tcp,Java,Tcp,我正在用Java做简单的客户机服务器。这是我的客户代码 try { socket = new Socket(serverIP, serverport); dataStream = new DataOutputStream(new BufferedOutputStream( socket.getOutputStream())); long[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

我正在用Java做简单的客户机服务器。这是我的客户代码

 try {
        socket = new Socket(serverIP, serverport);
        dataStream = new DataOutputStream(new BufferedOutputStream(
                socket.getOutputStream()));
        long[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        for (int i = 0; i < data.length; i++) {
            dataStream.writeLong(data[i]);
            System.out.println("So far" + dataStream.size());
        }
     }
  } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (socket != null)
            try {
                dataStream.flush();
                socket.close();
                dataStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

但是,没有成功从服务器套接字读取任何数据,connectionSocket.getInputStream.available()返回0字节。让我们假设每个变量都已正确声明。知道为什么吗?感谢您的帮助。

您的循环在每次传递时都会调用accept,这将一直阻止,直到下一个客户端连接。因此,第一个客户端连接,它读取一个长数据,打印该数据,然后循环阻塞,等待另一个连接连接。你需要把它移到圈外。可能是在一个单独的线程中,因为如果您在与accept新客户机相同的线程上迭代流,则在它与第一个客户机完成连接之前无法连接。这是下一步,但以下是解决问题的方法:

try {
   ServerSocket newSocket = new ServerSocket(2503);
   while( !done ) {
       Socket nextClient = newSocket.accept();
       // it's better to start a thread and put the call to this method in there
       // so the thread owning the ServerSocket can loop back around and accept
       // another client should another connect while it's servicing this client
       handleNewClient( nextClient );
   }
} finally {
   newSocket.close();
}

public void handleNewClient( Socket client ) {
   boolean done = false;
   DataInputStream in = new DataInputStream( new BufferedInputStream( client.getInputStream() ) );
   while( !done ) {
      try {
         Long l = in.readLong();
         System.out.println( l );
      } catch( EOFException e ) {
         // this is not a great way to end the conversation from a client.
         // better to either know how much you can read the stream (ie count of bytes), 
         // or use a termination encoding.  Otherwise you have to wait till the client
         // closes their side which throws EOFException.
         done = true;
      }
   }      
}
在关闭套接字之前刷新/关闭流


[Was:在更新代码之前刷新/关闭流]

尝试添加循环
for(int i=0;i实际上我这么做了,我会修改我的代码使其更清晰。谢谢你发现我在关闭套接字之前没有刷新它。现在我得到了它。谢谢你,顺便说一句,我认为DataOutputStream在写入它之后会自动刷新,它没有:(方法是public void nextClient(套接字客户端)吗应该是公共无效handleNewclient(套接字客户端)?谢谢
try {
   ServerSocket newSocket = new ServerSocket(2503);
   while( !done ) {
       Socket nextClient = newSocket.accept();
       // it's better to start a thread and put the call to this method in there
       // so the thread owning the ServerSocket can loop back around and accept
       // another client should another connect while it's servicing this client
       handleNewClient( nextClient );
   }
} finally {
   newSocket.close();
}

public void handleNewClient( Socket client ) {
   boolean done = false;
   DataInputStream in = new DataInputStream( new BufferedInputStream( client.getInputStream() ) );
   while( !done ) {
      try {
         Long l = in.readLong();
         System.out.println( l );
      } catch( EOFException e ) {
         // this is not a great way to end the conversation from a client.
         // better to either know how much you can read the stream (ie count of bytes), 
         // or use a termination encoding.  Otherwise you have to wait till the client
         // closes their side which throws EOFException.
         done = true;
      }
   }      
}