socket available 方法请教
package com.error; import java.io.IOException; import java.io.InputStream; import java.io.PushbackInputStream; import java.net.InetSocketAddress; import java.net.Socket; public class testclient { private Socket socket; private InputStream in ; //private PushbackInputStream pin ; private boolean isRead = false; private byte[] data = new byte[1024 * 10 * 10]; private long notDataTime = 1000 * 3; private long time = 0; void startClient(){ while(true){ if(!validConnection(socket)){ createConnection(); if(validConnection(socket)){ isRead = true; } } if(isRead){ try { /* * * 正确读取 * * int c = (int) pin.read(); if(c < 0){ continue; } pin.unread(c); int len = pin.available(); if(len> 0){ int temlen = pin.read(data,0,len); System.out.println("收到服务端数据:" + new String(data,0,temlen)); }else{*/ /**获取输入流中的有效字节 **/ int len = in.available(); if(len> 0){ int temlen = in.read(data,0,len); System.out.println("收到服务端数据:" + new String(data,0,temlen)); }else{ if(time == 0){ time = System.currentTimeMillis(); } long currentTime = System.currentTimeMillis(); if((currentTime - time ) > notDataTime){ System.out.println("3s 没有收到数据,断开连接"); shutdown(); time = 0; } } } catch (IOException e) { e.printStackTrace(); shutdown(); time = 0; } } } } void createConnection(){ socket = new Socket(); try { socket.connect(new InetSocketAddress("127.0.0.1",10011)); System.out.println("连接服务端成功!"); in = socket.getInputStream(); // pin = new PushbackInputStream(in); } catch (IOException e) { e.printStackTrace(); } } boolean validConnection(Socket socket){ return (socket != null && socket.isBound() && socket.isConnected()); } void shutdown(){ if(socket !=null){ try { socket.close(); socket = null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } isRead = false; } public static void main(String[] args) { new testclient().startClient(); } } package com.error; import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.Timer; import java.util.TimerTask; public class testserver { private Socket socket; private OutputStream out ; void createserver(){ try { ServerSocket server = new ServerSocket(); server.bind(new InetSocketAddress("127.0.0.1",10011)); System.out.println("start server."); socket = server.accept(); System.out.println("新连接到到达!"); out = socket.getOutputStream(); Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { int i = 0; @Override public void run() { try { if(i < 2 || i > 5){ out.write(("发送给客户端数据 " + i ).getBytes()); out.flush(); }else{ System.out.println("不发送数据 " + i); } } catch (IOException e) { System.out.println("发送数据异常,断开客户端连接!"); e.printStackTrace(); } i++; } }, 1000, 1000); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } void shutdown(){ if(socket !=null){ try { socket.close(); socket = null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if( out != null){ try { out.close(); out = null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { new testserver().createserver(); } }
客户端连接服务端,服务端开始发送数据,如果客户端3s没有收到数据,那么中断连接,然后再次尝试重新建立连接。
问题是:
当客户端3s没有数据中断连接后,in.available方法返回永远是0 了。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
永远是零是什么意思?你shutdown之后重新connect还是零?