0

我是java新手,我在通信客户​​端-服务器中遇到问题,我不知道如何读取多个我发送的向量。我会很感激你的帮助:)。我的代码是:服务器:

import java.net.*;
import java.util.Vector;
import java.io.*;

   public class FileServer {
   private static ObjectOutputStream outStream;

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

    // create socket
      ServerSocket servsock = new ServerSocket(2926);
   while (true) {
  System.out.println("Waiting...");


  Socket sock = servsock.accept();
  System.out.println("Accepted connection : " + sock);
  outStream = new ObjectOutputStream(sock.getOutputStream());
  // sendfile
  File myFile = new File ("D://music.mp3");


  Vector<Integer> send = new Vector<Integer>(); 
  int CRC=0;
  int part=0;
      for(int num=1;num<=(int) myFile.length()/60;num++){
          CRC= (part+num)%3;
          part = num*60;
          send.addElement(num);          
          send.addElement(part);
          send.addElement(CRC);
          outStream.writeObject(send);
          System.out.println(send);
          send.removeElement(num);
          send.removeElement(part);
          send.removeElement(CRC);

          if((myFile.length()<=(num+1)*60)&&(myFile.length()%60!=0)){
              int left = (int) (myFile.length()%60+part);
              int Num=num+1;
              CRC = (left + num)%2;

              send.addElement(Num);
              send.addElement(left);                 
              send.addElement(CRC);
              outStream.writeObject(send);
              System.out.println(send);
              send.removeElement(Num);
              send.removeElement(part);
              send.removeElement(CRC);



  System.out.println("Sending...");

          }
    }
  outStream.close();

  sock.close();

}
}
}

服务器端错误:

[1 60 1]
....
[64367, 3862020, 2]
[64368, 3862047, 0]
Sending...
Exception in thread "main" java.net.SocketException: Socket closed
  at java.net.SocketOutputStream.socketWrite(Unknown Source)
  at java.net.SocketOutputStream.write(Unknown Source)
  at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
  at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
  at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
  at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
  at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
  at java.io.ObjectOutputStream.writeObject0(Unknown Source)
  at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
  at java.io.ObjectOutputStream.writeObject(Unknown Source)
  at FileServer.main(FileServer.java:61)

我的客户端:

import java.net.*;
import java.util.Vector;
import java.io.*;

public class FileClient{

private static InetAddress host; 
public static void main (String [] args ) throws ClassNotFoundException,    UnknownHostException, IOException {

  host = InetAddress.getLocalHost();
  Socket sock = new Socket(host,2926);
  System.out.println("Connecting...");
  ObjectInputStream inStream = new ObjectInputStream(sock.getInputStream());

  long start = System.currentTimeMillis();

  for(int a=1;a<=100;a++){
  System.out.println("seq number of received vector: "+a);

  @SuppressWarnings("unchecked")   
  Vector <send> response = (Vector<send>)inStream.readObject();
  long end = System.currentTimeMillis();
  System.out.println(response);
  System.out.println("time: "+(end-start));


  }


  sock.close();


}


 }

以及来自客户端的错误:(没有错误,但我希望看到至少 100 个向量,而不是第一个向量的 100 倍):

seq number of received vector: 1
[1, 60, 1]
time: 71
...
seq number of received vector: 100
[1, 60, 1]
time: 79
4

2 回答 2

1

将对象写入输出流后,如果您再次发送它,该流只会发送对已发送内容的引用。因此,要么每次创建一个新向量,要么在第二次发送向量之前在流上调用reset() 。

请注意,多年来,ArrayList 应该比 Vector 更受欢迎。你不应该再使用 Vector 了。

于 2012-07-29T12:07:27.247 回答
0

对于服务器端异常。客户端在读取 100 个向量后关闭套接字。而服务器端还有更多的向量要写。所以它报告'java.net.SocketException:套接字关闭'

对于客户端“重复问题”,您应该new每次在发送循环中创建一个向量

在底层,Java ObjectOutputStream/InputStream 会记住每个被序列化对象的“引用”,因此可以在客户端重建对象引用映射。如果一个对象之前已经序列化并发送过,那么只会标记并发送一个“引用”。在您的情况下,写入单个对象,客户端仅接收单个对象。有关更多详细信息,请深入研究源代码writeObject

于 2012-07-29T12:45:27.260 回答