流与socket

所有东西 为自己理解,有的地方会不准,如果有高手可以指出,非常感谢。

 

这里对使用socket 

 

//流读取工具类 

public class StreamUtil {

 

  //正常读取本地文件的时可以用,但用于网络传输读取数据时会有问题

//一旦 网络中另一方的数据没有返回到 这边 则会出现阻塞状态

public static byte[] readStream(InputStream is ) throws IOException{

 

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

  // is.read( buffer,0,lenth  )

while(  is.read( buffer  ) !=-1  ){

outStream.write(buffer );

}

is.close();

outStream.close();

return outStream.toByteArray();

}

 

  //调用 availble 可用于网络上读取数据 

public static byte[] readStreamAvailble(InputStream is ) throws IOException{

int len =0;

while(len==0){

//如果服务端 没有数据返回则 不断的取数据 ,直到取到数据位置 ,一旦出现阻塞则数据则会不准确 

len = is.available();

}

byte[] bs = new byte[len];

is.read( bs );

return bs;

}

  //测试

public static void main(String[] args) throws IOException {

File file = new File("e:/1113.txt");

InputStream is = new FileInputStream(file);;

byte[] str = readStream( is);

System.out.println( new String(str)  );

}

}

 

-------socket 服务端 使用 网络读取数据 

 

public class Server {

 

public static void main(String[] args) throws IOException {

ServerSocket server = new ServerSocket(8001);

while(true ){

Socket socket = server.accept();

//对于每个客户端的请求  每个请求创建一个线程来回应

new Thread(new ServerImpl( socket )).start();

}

}

}



public class ServerImpl implements Runnable {

Socket socket = null;
public ServerImpl(Socket socket){
this.socket = socket ;
}
@Override
public void run() {
InputStream is = null;
OutputStream os = null;
//异常处理要 放到循环的外面,放到循环内部会影响效率
try {

socket.setKeepAlive(true);
socket.setSoTimeout(3 * 1000);
 
while(true){
is = socket.getInputStream();
// 如果调用 readStream方法 则程序会出现阻塞状态 
byte[]  bs = StreamUtil.readStreamAvailble(is);
//当调用这个方法读取客户端的数据时候 ,如果客户端阻塞时间超过服务端 setSoTimeout中设置的时间则read 会超时异常
//byte[]  bs = StreamUtil.readStream(is);

System.out.println( "收到的信息为:"+ new String(bs ));
os = socket.getOutputStream();
os.write( "回应的信息".getBytes()  );
os.flush();

System.out.println( "是否绑定:"+socket.isBound()); // 是否邦定
System.out.println( "是否关闭:"+ socket.isClosed());
System.out.println( "是否保持连接:"+ socket.isConnected());
System.out.println( "输入流是否close:"+ socket.isInputShutdown());
System.out.println( "输出流是否close :"+ socket.isOutputShutdown());

//  服务端使用完的 输入  输出流 和socket 不能关闭 , 因为客户端还与服务端保持联系 ,一点关闭则会出现异常
// is.close();
// os.close();
// socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
// if(is!=null ){
// is.close();
// }
// if(os!=null){
// os.close();
// }
// if(socket!=null){
// socket.close();
// }
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

 

==========客户端 

 

public class Client {

 

  public static void main(String[] args) { client2(); } /** * 测试 过期时间 设置 * @Title: client2 * @Description: * @param * @return void * @throws */ public static void client2(){ Socket socket = null; try { socket = new Socket("127.0.0.1", 8001); socket.setKeepAlive(true); while(true && null != socket){

//服务器阻塞 Thread.sleep(10 * 1000); } } catch ( Exception e) { e.printStackTrace(); } } /** * 测试输入流 * @Title: client1 * @Description: * @param * @return void * @throws */ public static void client1(){ Socket socket = null; OutputStream os = null; InputStream is = null ; try { socket = new Socket("127.0.0.1", 8001); socket.setKeepAlive(true); socket.setSoTimeout( 30*1000 ); os = socket.getOutputStream(); os.write( "服务器你好:".getBytes()); is = socket.getInputStream(); byte[] bs = StreamUtil.readStreamAvailble(is); System.out.println( "服务器的消息:"+ new String(bs )); is.close(); os.close(); socket.close(); } catch ( Exception e) { e.printStackTrace(); }finally{ try { if(is!=null ){ is.close(); } if(os!=null){ os.close(); } if(socket!=null){ socket.close(); } } catch (IOException e) { e.printStackTrace(); } } }

}

 

参考:

http://cuisuqiang.iteye.com/blog/1434416

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值