输入输出流的shutdownoutput和shutdowninput方法的使用(文件默认有一个-1的结束标志位)
来源:互联网 发布:进击的巨人漫画软件 编辑:程序博客网 时间:2024/05/17 06:57
最近做练习的时候遇到上传文件可以上传成功,但是上传文件之后返回反馈的时候,总是不能成功返回反馈,添加shutdownoutput就可以了,
练习如下:
server端代码:
public class server {public static void main(String[] args) throws IOException {ServerSocket ss=new ServerSocket(8802);Socket a = ss.accept();InputStream in = a.getInputStream();FileOutputStream fos=new FileOutputStream("e.jpg");int len=-1;byte[] b=new byte[1024];while((len=in.read(b))!=-1){fos.write(b, 0, len);}//-----------------------反馈信息----------------------------------OutputStream out = a.getOutputStream();out.write("我已经接收到图片了".getBytes());out.close();fos.close();in.close();ss.close();}}
client端代码:
public class client {public static void main(String[] args) throws IOException {Socket so=new Socket(InetAddress.getLocalHost(), 8802);OutputStream out = so.getOutputStream();FileInputStream fis=new FileInputStream("D:\\a.jpg");int len=-1;byte[] b=new byte[1024];while((len=fis.read(b))!=-1){out.write(b,0,len);}so.shutdownOutput();//------------收到反馈信息----------InputStream in = so.getInputStream();int length=in.read(b);System.out.println(new String(b,0,length));fis.close();out.close();so.close();}}
服务器端代码接收数据的时候是:
服务器端代码读取的时候源代码如下:
public int read(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; }b不等于空,也不符合第二条情况,但是读取的长度是0,所以是第三种情况,因此会返回0.如果服务器端不加shutdownoutput的话,相当于不加结束标志位,然后服务器端读取到结尾的时候读不到文件结束标志位,然后会一直循环下去,每次读0个字节写0个字节。shutdownoutput相当于给文件加一结束标志为。
阅读全文
0 0
- 输入输出流的shutdownoutput和shutdowninput方法的使用(文件默认有一个-1的结束标志位)
- TCP传输模式中,调用Socket的shutdownInput()和shutdownOutput()都做了些什么?
- 标志位的判断方法
- shutdownInput与shutdownOutput
- 接收一个文件内容,通过读取开始和结束标志,分别将其内容保存到不同的文件中
- 函数的开始和结束标志
- Unity的每个界面右下角有一个麦克风标志和一个刷新标志解决方案
- 文件的输入输出流方法
- 输入一个字符串 在输入帧头 帧尾 取合法帧 (标志位的使用)
- 创建一个有吸引力的标志
- 使用Autohotkey结束flash文件的方法
- script使用不同的结束标志产生的效果不同
- 汇编语言标志位的意义和位置
- 汇编语言标志位的意义和位置
- 软件测试的目的、原则和结束标志
- java中结束循环的标志break和continue语句
- 标志位和中断位的区别:USART_ClearFlag和USART_ClearITPendingBit
- 怎样记住标志寄存器的标志位
- 应用程序无法正常启动0xc0000142) 关于win10周年版更新后无法正常使用vc6.0问题的解决方法
- 由内向外的螺旋矩阵
- c++ 将16进制字符串转换成为int数值
- 让我们来谈谈对Linux的认识,值得每一位运维人员深读
- 周末训练笔记+UVA11388+POJ2407+无名题
- 输入输出流的shutdownoutput和shutdowninput方法的使用(文件默认有一个-1的结束标志位)
- LeetCode-116. Populating Next Right Pointers in Each Node【二叉树同层节点构成链表】
- 准备人工智能(未来计划)
- LeeCode 442. Find All Duplicates in an Array题解
- Activity的生命期
- java鬼混笔记:springboot 9、springboot整合mybatis加上分页功能
- python学习笔记-文件操作
- 浅谈unity的NGUI于UGUI的区别
- 文章标题