我是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