Java socke编程 读取信息信息发生阻塞的解决方法

本文介绍了Java Socket编程中遇到的读取信息阻塞问题,特别是当从服务端读取数据时,如何通过设置超时时间(如5秒)来避免阻塞,并在超时后关闭连接释放资源,以确保程序的正常运行。

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

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值