Javasocke编程读取信息信息发生阻塞的解决方法 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

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();
			}
			
		}
		
	}

}

相关案例查看更多