Socket编程InputStream中的read()方法的问题!!!

while(is.read(buf)!=-1)//服务器端程序
			{
				String str=new String(buf);
				System.out.println("来自客服端:"+str);
				os.write(str.toUpperCase().getBytes());
			}

while((str=bw.readLine())!=null)//客户端程序{os.write(str.getBytes());if(str.equals("over"))break;byte []buf=new byte[1024];while(is.read(buf)!=-1)//这一行一直阻塞,是因为服务器端的输出流不关闭的话read方法就一直等待数据,而不会读到-1;{System.out.println("来自服务器:"+new String(buf));}System.out.println("呵呵呵");}
 
  
 
  
对此问题的英文解释:It returns -1 if it's end of stream. If stream is still open (i.e. socket connection) but no data has reached the reading side (server is slow, networks is slow,...) the read() blocks. 


你可能感兴趣的:(Java)