网小鱼Java的bug小集锦0012

这个bug出现在学习网络通信的时候出现的一个问题,mark一下下~
网小鱼Java的bug小集锦0012报错提示: Java.net.SocketException:Socket is closed.

报错原因:关流操作后,在服务器端调用这个客户端的输出流就不能使用了.

解决方案一:把关流操作后置,放到服务器端调用完数据之后再进行关闭,这样才能保证服务器端访问数据流的时候可以获得访问权限.

解决方案二:把关流操作的.close 换成刷新流的操作.flush,这样也可以保证服务器端能够访问到数据

网小鱼Java的bug小集锦0012
解决方案一:如上图

网小鱼Java的bug小集锦0012
结局方案二:如上图.

关于.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 {
   	}
  }
上一篇:IO与网络编程


下一篇:elasticsearch7.10.2测试