ano*_*123 5 java sockets client network-programming
当我使用套接字编程传输大文件时,接收的文件不完整,即它是一个mp3文件,当我播放时听起来很奇怪.代码是:
服务器端:
File myFile = new File("abc.mp3");
{
Socket sock = servsock.accept();
int packetsize=1024;
double nosofpackets=Math.ceil(((int) myFile.length())/packetsize);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
for(double i=0;i<nosofpackets+1;i++) {
byte[] mybytearray = new byte[packetsize];
bis.read(mybytearray, 0, mybytearray.length);
System.out.println("Packet:"+(i+1));
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0,mybytearray.length);
os.flush();
}
}
Run Code Online (Sandbox Code Playgroud)
客户端:
int packetsize=1024;
FileOutputStream fos = new FileOutputStream("zz.mp3");
BufferedOutputStream bos = new BufferedOutputStream(fos);
double nosofpackets=Math.ceil(((int) (new File("abc.mp3")).length())/packetsize);
for(double i=0;i<nosofpackets+1;i++)
{
InputStream is = sock.getInputStream();
byte[] mybytearray = new byte[packetsize];
int bytesRead = is.read(mybytearray, 0,mybytearray.length );
System.out.println("Packet:"+(i+1));
bos.write(mybytearray, 0,mybytearray.length);
}
sock.close();
bos.close();
Run Code Online (Sandbox Code Playgroud)
在客户端,我new File("abc.mp3")).length只是为了简单而使用(我可以从服务器端发送文件的长度).
如果客户端和服务器是同一台机器,则此代码可以正常工作,但如果文件位于不同的计算机上,则该文件会失真.
use*_*421 19
在Java中复制流的规范方法:
int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
Run Code Online (Sandbox Code Playgroud)
适用于任何大于零的缓冲区大小.应该尽量避免将缓冲区大小与输入大小联系起来的诱惑.
我认为问题是你忽略了各种read调用返回的值,并假设它们完全填满了缓冲区.这是有问题的:
从文件读取时,最后一次读取可能不会填充缓冲区.
从套接字读取时,任何读取都可能在填充缓冲区之前返回.
最终结果是您的写入将垃圾放入流(在服务器端)和目标文件(在客户端).
此外,根据文件的大小将文件分成多个块是毫无意义的.只需阅读,直到你到达文件的末尾.
| 归档时间: |
|
| 查看次数: |
29602 次 |
| 最近记录: |