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

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

}


你可能感兴趣的:(android,socket)