A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© qixing0918 中级黑马   /  2013-11-2 13:33  /  796 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

客户端
  1. public class TCPClientPic {

  2. public static void main(String[] args) throws Exception{

  3. Socket socket = new Socket("127.0.0.1",20001);

  4. InputStream is = socket.getInputStream();
  5. OutputStream os = socket.getOutputStream();

  6. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("d:\\star.jpg")));
  7. BufferedOutputStream bosout = new BufferedOutputStream(os);
  8. BufferedInputStream bisin = new BufferedInputStream(is);
  9. int len = 0;
  10. while((len = bis.read())!=-1){
  11. bosout.write(len);
  12. }
  13. socket.shutdownOutput();
  14. byte[] buf = new byte[30];
  15. len = bisin.read(buf);
  16. System.out.println(Arrays.toString(buf));
  17. System.out.println(new String(buf) );


  18. bis.close();
  19. bisin.close();
  20. bosout.close();
  21. socket.close();

  22. }
  23. }
复制代码
服务器端
  1. public class TCPReceivePic {

  2. public static void main(String[] args) throws Exception {
  3. ServerSocket ss = new ServerSocket(20001);

  4. Socket socket = ss.accept();
  5. System.out.println(socket.getInetAddress().getHostAddress());

  6. InputStream is = socket.getInputStream();
  7. OutputStream os = socket.getOutputStream();
  8. BufferedInputStream bisin = new BufferedInputStream(is);
  9. BufferedOutputStream bosout = new BufferedOutputStream(os);
  10. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
  11. new File("d:\\copyPid.jpg")));

  12. int len = 0;
  13. while ((len = bisin.read()) != -1) {
  14. bos.write(len);
  15. }
  16. bosout.write("nihao".getBytes());

  17. bisin.close();
  18. bos.close();
  19. bisin.close();
  20. socket.close();
  21. ss.close();

  22. }
  23. }
复制代码
客户端结果  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
也就是说我 在服务器端返回的nihao 没有返回 怎么回事 求解!!!!!!










3 个回复

倒序浏览
服务器加上bosout.flush()

评分

参与人数 1黑马币 +3 收起 理由
qixing0918 + 3 谢了

查看全部评分

回复 使用道具 举报
忘了 bisin.close();

bos.close();

bisin.close();  写了俩个 忘关bosout了 我说的么  谢了
回复 使用道具 举报
加油哈,好好努力,为了黑马
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马