知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們在追求其視覺表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏壧峁┍憬莸闹С郑?
Javasocke編程讀取信息信息發(fā)生阻塞的解決方法
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):41
1、該socket聯(lián)網(wǎng)對象類會導(dǎo)致攫取辦事端的數(shù)據(jù)產(chǎn)生壅塞
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; } /** * 把數(shù)據(jù)msg發(fā)送到辦事端 * @param socket * @param msg 待發(fā)送的數(shù)據(jù) */ 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(); } } /** * 大年夜辦事端返回的數(shù)據(jù) * @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、在攫取辦事端數(shù)據(jù)的時(shí)刻,如不雅一段時(shí)光后沒有攫取到數(shù)據(jù),則封閉連接釋放資本。(本對象類以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; } /** * 把數(shù)據(jù)msg發(fā)送到辦事端 * @param socket * @param msg 待發(fā)送的數(shù)據(jù) */ 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(); } } /** * 大年夜辦事端返回的數(shù)據(jù) * @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秒后無響應(yīng) Thread.sleep(5000); if(mInputStream.available()==0){ //封閉連接釋放資本 closeConn(socket); break; } } catch (Exception e) { // TODO: handle exception } if(D){ Log.i("攫取辦事端數(shù)據(jù)", 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(); } } } }
相關(guān)案例查看更多
相關(guān)閱讀
- 麗江小程序開發(fā)
- 云南etc微信小程序
- 網(wǎng)站建設(shè)案例
- 貴州小程序開發(fā)
- 軟件定制
- 云南網(wǎng)站建設(shè)公司哪家好
- 網(wǎng)站排名
- 汽車報(bào)廢回收軟件
- 支付寶小程序被騙
- 網(wǎng)站建設(shè)哪家強(qiáng)
- 網(wǎng)絡(luò)公司報(bào)價(jià)
- 網(wǎng)站建設(shè)選
- 紅河小程序開發(fā)
- 云南網(wǎng)站建設(shè)案例
- 汽車報(bào)廢拆解管理系統(tǒng)
- 公眾號模板消息
- Web開發(fā)框架
- 云南網(wǎng)站設(shè)計(jì)
- 云南網(wǎng)絡(luò)營銷顧問
- 云南小程序開發(fā)首選品牌
- 云南小程序開發(fā)費(fèi)用
- 買小程序被騙
- 云南省城鄉(xiāng)建設(shè)廳網(wǎng)站
- 網(wǎng)站優(yōu)化哪家好
- 云南軟件開發(fā)
- 云南小程序公司
- 排名
- 云南網(wǎng)站建設(shè)哪家好
- 云南做軟件
- 楚雄網(wǎng)站建設(shè)公司