首页 / JAVA / 网小鱼Java的bug小集锦0012
网小鱼Java的bug小集锦0012
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了网小鱼Java的bug小集锦0012,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3504字,纯文字阅读大概需要6分钟。
内容图文
![网小鱼Java的bug小集锦0012](/upload/InfoBanner/zyjiaocheng/597/8d64b7727bbf4c9f8cf45ecf08a10a78.jpg)
这个bug出现在学习网络通信的时候出现的一个问题,mark一下下~
报错提示: Java.net.SocketException:Socket is closed.
报错原因:关流操作后,在服务器端调用这个客户端的输出流就不能使用了.
解决方案一:把关流操作后置,放到服务器端调用完数据之后再进行关闭,这样才能保证服务器端访问数据流的时候可以获得访问权限.
解决方案二:把关流操作的.close 换成刷新流的操作.flush,这样也可以保证服务器端能够访问到数据
解决方案一:如上图
结局方案二:如上图.
关于.close和.flush的用法源码如下:
package java.io
/**
This abstract class is the superclass of all classes representing
an input steam of bytes.
这个抽象类时所有类中输出的字符
<p>Applications that need to define a subclass of <code>Inputstream </code>
must always provide a method that returns the next byte of input.
*申请需要去定义一个输入的父类,必须要提供一个方法能够返回输入的下一个字符/
public abstract class InputStream implements Closeable{
private static final int MAX_SKIP_BUFFER_SIZE = 2048;
}
/**
Reads the next byte of date from the inputstream . The value byte is returned
as an <code>int </code> in hte range <code>0</code>to <code> 255</code>.If no byte is
available becausse the end of the Stream has been reachered ,the value<code>-1 <code> is returned.
This method blocks until input date is available ,the end of the stream is detected,or an exception is thrown.
从输入流中读取下一个字符,这个读取到的值会被返回,返回的值是在0-255之间的值(byte的范围是-128到127之间),如果读取不到下一个字符,就会返回一个-1,如果能够读取到,方法就会一直往下读取,当遇到最后一个字符的时候,就会抛出一个异常
<p> A subclass must provide an implementation of this method.
父类方法必须要提供一个可以继承的方法
@return the next byte of date, or <code> -1 <code> if the end of the stream is reached.
返回下一个字节中的数据,或者是返回-1
*/
public abstract int read() throws IOException;
public int read (byte b [])throws IOException{
return read(b,0,b.length);
public int read (byte b [],int len)throws IOException{
if(b==null){
throws new NullPointException();
}else if(off < 0 || len < 0 || b.length - off){
throw new IndexOutOfBoundsException();
}else if(){
return 0;
}
int c = read();
if(c==-1){
return -1;
}
b[off] = (byte)c;
int i = 1;
try{
for(; i< len ;i++){
c = read();
if(c==-1){
break;
}
b[off + i] = (byte)c;
}
}catch(IOException ee){
}
return i
}
public long skip(long n) throws IOException {
long remaining = n;
int nr;
if (n <= 0) {
return 0;
}
int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
byte[] skipBuffer = new byte[size];
while (remaining > 0) {
nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
if (nr < 0) {
break;
}
remaining -= nr;
}
return n - remaining;
}
public int available() throws IOException {
return 0;
}
public void close() throws IOException {}
public synchronized void mark(int readlimit) {}
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
public boolean markSupported() {
return false;
}
}
在源码中,关于flush的用法如下:
public abstract class OutputStream implements Closeable, Flushable {
public abstract void write(int b) throws IOException;
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
public void write(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
for (int i = 0 ; i < len ; i++) {
write(b[off + i]);
}
}
public void flush() throws IOException {
}
public void close() throws IOException {
}
}
内容总结
以上是互联网集市为您收集整理的网小鱼Java的bug小集锦0012全部内容,希望文章能够帮你解决网小鱼Java的bug小集锦0012所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。