java – BufferUnderflowException? here?
•
Java
I am writing a small UDP server in Java When the server receives a command ('get_video '), it reads a file ('video. Raw') and sends it to the client
This is my code:
public class ServeurBouchon {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
byte[] buff = new byte[64];
int port = 8080;
DatagramPacket packet = new DatagramPacket(buff,buff.length);
DatagramSocket socket = new DatagramSocket(port);
System.out.println("Server started at 8080 ...");
while (true) {
socket.receive(packet);
new ThreadVideo(socket,packet).run();
}
}
public static class ThreadVideo extends Thread {
private DatagramSocket socket;
private DatagramPacket packet;
public ThreadVideo(DatagramSocket socket,DatagramPacket packet) {
this.packet = packet;
this.socket = socket;
}
public void run() {
String cmd = new String(packet.getData(),packet.getLength());
System.out.println("S:CMD reçu :" + cmd);
if ("GET_VIDEO".equals(cmd)) {
read_and_send_video(this.packet.getAddress());
} else if ("TIMEOUT_REQUEST".equals(cmd)) {
return;
} else {
System.out.println(" Exiting ...");
return;
}
System.out.println("Fin .....");
}
private void read_and_send_video(InetAddress address) {
System.out.println(" reading and sending video ...");
File file = new File("./video/video.raw");
ByteBuffer ibb = ByteBuffer.allocate(4);
ibb.order(ByteOrder.BIG_ENDIAN);
FileInputStream fis = null;
DatagramPacket pack;
byte[] buff = new byte[4];
System.out.println(" Sending ...");
try {
fis = new FileInputStream(file);
int size = 0;
while ( size != -1) {
size = fis.read(buff,buff.length);
System.out.println(" size = " + size);
ibb.put(buff);
System.out.println("Size : " + ibb.getInt());
int length = ibb.getInt();
byte[] fbuff = new byte[length];
fis.read(fbuff,length);
pack = new DatagramPacket(fbuff,fbuff.length,address,packet.getPort());
socket.send(pack);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The original file format is a continuous "size frame" The "size" variable contains the size of the next frame to be read (int) My problem is that when I read the file (in line IBB. Getint()), I get this exception:
Exception in thread "main" java.nio.BufferUnderflowException at java.nio.Buffer.nextGetIndex(Buffer.java:480) at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:336) at fr.sar.dss.bouchon.ServeurBouchon$ThreadVideo.read_and_send_video(ServeurBouchon.java:75) at fr.sar.dss.bouchon.ServeurBouchon$ThreadVideo.run(ServeurBouchon.java:48) at fr.sar.dss.bouchon.ServeurBouchon.main(ServeurBouchon.java:29)
Maybe I made a mistake, but someone can tell me where my mistake is?
Thansk's help;)
Solution
This reads two integers
System.out.println("Size : " + ibb.getInt());
int length = ibb.getInt();
Use this:
int length = ibb.getInt();
System.out.println("Size : " + length);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
