作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在用 Java 编写一个小型 UDP 服务器。当服务器收到命令('GET_VIDEO')时,他读取一个文件('video.raw'),然后将其发送给客户端。
这是我的代码:
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(), 0, 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, 0, 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, 0, length);
pack = new DatagramPacket(fbuff, fbuff.length, address,
packet.getPort());
socket.send(pack);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
原始文件格式是连续的“大小+帧”。 “大小”变量包含要读取的下一帧的大小(一个整数)。我的问题是当我读取文件时(在 ibb.getInt() 行),我得到这个异常:
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)
也许我做错了,但有人可以告诉我我的错误在哪里吗?
感谢您的帮助;)
最佳答案
这读取两个 整数。
System.out.println("Size : " + ibb.getInt());
int length = ibb.getInt();
使用这个:
int length = ibb.getInt();
System.out.println("Size : " + length);
关于java - 缓冲区下溢异常?这里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5261547/
我是一名优秀的程序员,十分优秀!