[英]Java Video Chat applicataion input output stream not working
我正在制作一个视频聊天应用程序,该应用程序使用Java网络(也称为套接字)将网络摄像头的图像发送到另一个客户端。
我的代码首先发送缓冲图像数据的长度,然后发送实际数据。 服务器还首先读取一个int,然后读取数据本身。 第一个图像有效,但之后,数据输入流读取的长度为负数。
服务器端:
frame = new JFrame();
while (true) {
try {
length = input.readInt();
System.out.println(length);
imgbytes = new byte[length];
input.read(imgbytes);
imginput = new ByteArrayInputStream(imgbytes);
img = ImageIO.read(imginput);
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);
}
catch(IOException e){
e.printStackTrace();
}
}
客户端:
while(true) {
try {
currentimg = webcam.getImage();
ImageIO.write(currentimg, "jpg", imgoutputstream);
imgbytes = imgoutputstream.toByteArray();
out.writeInt(imgbytes.length);
out.write(imgbytes);
} catch (IOException e) {
e.printStackTrace();
}
}
在客户端,您总是将新图像写入现有流。 这导致每次迭代中数组大小的增加。 在java中, int
的最大值为2147483647
。 如果增加此整数,它将跳到最小值auf int
,它是负数(请参阅本文 )。
因此,要解决此错误,您需要在写入下一张图像之前清除流,以使大小永远不会大于整数的最大值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.