Javasocke编程读取信息信息发生阻塞的解决方法
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:31
1、该socket联网对象类会导致攫取办事端的数据产生壅塞
public class ConnectToServer { private static boolean D=true; private static InputStream mInputStream; private static OutputStream mOutputStream; public ConnectToServer() { // TODO Auto-generated constructor stub } /** * 建立TCP连接 * @param ip 办事端ip * @param port 端口 */ public static Socket conn(String ip,int port){ try { Socket socket=new Socket(InetAddress.getByName(ip), port); if(D){ Log.i("Socket", (socket==null)+""); } return socket; } catch (Exception e) { // TODO: handle exception } return null; } /** * 把数据msg发送到办事端 * @param socket * @param msg 待发送的数据 */ public static void sendReq(Socket socket,byte[] msg){ try { mOutputStream=socket.getOutputStream(); mOutputStream.write(msg); mOutputStream.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 大年夜办事端返回的数据 * @param socket * @return */ public static byte[] recData( Socket socket){ int count = 0; try { mInputStream=socket.getInputStream(); byte[] inDatas = null; while (count == 0) { count = mInputStream.available(); } inDatas = new byte[count]; mInputStream.read(inDatas); return inDatas; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return null; } /** * 封闭连接释放资本 * @param socket */ public static void closeConn(Socket socket){ if(mOutputStream!=null){ try { mOutputStream.close(); mOutputStream=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(mInputStream!=null){ try { mInputStream.close(); mInputStream=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null){ try { socket.close(); socket=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
2、在攫取办事端数据的时刻,如不雅一段时光后没有攫取到数据,则封闭连接释放资本。(本对象类以5秒为例)
public class ConnectToServer { private static boolean D=true; private static InputStream mInputStream; private static OutputStream mOutputStream; public ConnectToServer() { // TODO Auto-generated constructor stub } /** * 建立TCP连接 * @param ip 办事端ip * @param port 端口 */ public static Socket conn(String ip,int port){ try { Socket socket=new Socket(InetAddress.getByName(ip), port); return socket; } catch (Exception e) { Log.i("异常信息", e.toString()); } return null; } /** * 把数据msg发送到办事端 * @param socket * @param msg 待发送的数据 */ public static void sendReq(Socket socket,byte[] msg){ try { mOutputStream=socket.getOutputStream(); mOutputStream.write(msg); mOutputStream.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 大年夜办事端返回的数据 * @param socket * @return */ public static byte[] recData( Socket socket){ int count = 0; try { mInputStream=socket.getInputStream(); byte[] inDatas = null; while (count == 0) { count = mInputStream.available(); try { //5秒后无响应 Thread.sleep(5000); if(mInputStream.available()==0){ //封闭连接释放资本 closeConn(socket); break; } } catch (Exception e) { // TODO: handle exception } if(D){ Log.i("攫取办事端数据", mInputStream.available()+""); } } if(count==0){ return null; }else{ inDatas = new byte[count]; mInputStream.read(inDatas); return inDatas; } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return null; } /** * 封闭连接释放资本 * @param socket */ public static void closeConn(Socket socket){ if(mOutputStream!=null){ try { mOutputStream.close(); mOutputStream=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(mInputStream!=null){ try { mInputStream.close(); mInputStream=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null){ try { socket.close(); socket=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }