我的发件人和服务器 UDP 有问题。SenderUDP
发送一个对象并且必须ServerUDP
接收它。对于第一个对象,一切正常,但是当我尝试发送另一个对象时,服务器会抛出一个StreamCorruptedException
. 这里是客户端代码:
public class SenderUDP implements Runnable {
private java.net.DatagramSocket clientsocket;
private ObjectOutputStream out;
private int port;
private InetAddress ip;
private Packet objToSend;
private ByteArrayOutputStream baos;
public SenderUDP(String ip, int port, Packet p) throws UnknownHostException{
this.ip =InetAddress.getByName(ip);
this.port = port;
this.objToSend = p;
this.out = null;
this.clientsocket = null;
}
@Override
public void run(){
try{
clientsocket = new DatagramSocket ();
System.out.println("Inside senderUDP");
byte[] sendData;
baos = new ByteArrayOutputStream(1024);
out = new ObjectOutputStream(baos);
out.writeObject(objToSend);
sendData = baos.toByteArray();
DatagramPacket sendpacket = new DatagramPacket(sendData,sendData.length,ip,port);
clientsocket.send(sendpacket);
System.out.println("Sended packet with UDP");
out.flush();
if(this.objToSend.getOP() == 1){
byte[] buf = new byte[1024];
int read;
ByteArrayOutputStream bas = new ByteArrayOutputStream((int)this.objToSend.getFile().length());
FileInputStream fis = new FileInputStream(objToSend.getFile());
while((read = fis.read(buf)) != -1){
bas.write(buf, 0, read);
}
DatagramPacket sendfile = new DatagramPacket(bas.toByteArray(), bas.toByteArray().length, ip, port);
clientsocket.send(sendfile);
}
out.close();
}
catch(UnknownHostException uhe) {
System.err.println(uhe.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
}
}
这是服务器端代码:
class ServerUDP implements Runnable {
private DatagramSocket socket;
private int port;
private Controller controller;
private byte[] buffer;
private DatagramPacket packet;
private Packet p;
private ObjectInputStream ois;
public ServerUDP(int port, Controller controller){
this.socket = null;
this.port = port;
this.controller = controller;
}
@Override
public void run() {
try {
socket = new DatagramSocket(port);
} catch (SocketException ex) {
Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
}
while(true){
buffer = new byte[1000000];
packet = new DatagramPacket(buffer,buffer.length);
System.out.println("Ascolto UDP!");
try {
socket.receive(packet);
System.out.println(packet);
System.out.println("1");
} catch (IOException ex) {
Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Packet UDP Received!");
try {
ois = new ObjectInputStream(new ByteArrayInputStream(buffer));
} catch (IOException ex) {
Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
}
try {
p = (Packet) ois.readObject();
System.out.println("Pacchetto/Evento arrivato con UDP!");
System.out.println(p);
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
}
if(p.getOP() == 1){
Thread t = new Thread(new FilesManager(socket,p,false, controller));
t.start();
}
controller.enqueue(p);
try {
ois.close();
} catch (IOException ex) {
Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
被StreamCorruptedException
抛出
ois = new ObjectInputStream(new ByteArrayInputStream(buffer));
和
p = (Packet) ois.readObject();
谢谢!