Tcp传输小练习,我的程序复制的MP3文件少了4KB却还是可以打开,这是为什么?- import java.io.*;
- import java.net.*;
- class ClientDemo4
- {
- public static void main(String[] args) throws Exception
- {
- Socket s=new Socket("192.168.1.100",10001);
- BufferedInputStream bufIs=new BufferedInputStream(new FileInputStream("1.mp3"));
- BufferedOutputStream bufOut=new BufferedOutputStream(s.getOutputStream());
- byte[] buf=new byte[1024];
- int len=0;
- while ((len=bufIs.read(buf))!=-1)
- {
- bufOut.write(buf,0,len);
- }
- s.shutdownOutput();//关闭客户端输出流,相当于给一个-1结束标记。
- BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
- System.out.println(bufIn.readLine());
- bufIs.close();
- s.close();
- }
- }
- class ServerDemo4
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss=new ServerSocket(10001);
- Socket s=ss.accept();
- String ip=s.getInetAddress().getHostAddress();
- System.out.println(ip+"....connection");
- BufferedInputStream bufIs=new BufferedInputStream(s.getInputStream());
- BufferedOutputStream bufw=new BufferedOutputStream(new FileOutputStream("2.mp3"));
- byte[] buf=new byte[1024];
- int len=0;
- while ((len=bufIs.read(buf))!=-1)
- {
- bufw.write(buf,0,len);
- bufw.flush();
- }
- PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
- pw.println("上传成功");
- bufw.close();
- s.close();
- ss.close();
- }
- }
复制代码
|
|