⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pub_connector.java

📁 福建鑫诺话机客户端接受程序
💻 JAVA
字号:
package telproject;

import java.net.*;
import java.io.*;

/**
 * Agent 、Client 的连接请求
 */
public class Pub_Connector {
	private Socket socket; // 连接

	private int iType = -1; // 连接类型

	public java.io.DataInputStream in = null; // 从 Socket 中取得的数据输入流

	public java.io.DataOutputStream out = null; // 从 Socket 中取得的数据输出流

	public String ip = ""; // IP 地址

	public Pub_Connector(Socket s, int it) {
		try {
			s.setSoTimeout(100 * 1000);//10*1000 before 2007-04-05 modify
		} catch (Exception e) {
		}
		iType = it;
		socket = s;
		init();
	}

	public Pub_Connector(Socket s) {
		socket = s;
		init();
	}

	public Socket getSocket() {
		return socket;
	}

	public void setType(int i) {
		iType = i;
	}

	public int getType() {
		return iType;
	}

	public String getIPAddr() {
		return ip;
	}

	public boolean init() {
		if (in != null)
			return true;
		try {
			in = new DataInputStream(socket.getInputStream());
			out = new DataOutputStream(socket.getOutputStream());
			InetAddress ia = socket.getInetAddress();
			ip = ia.getHostAddress();
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	/**
	 * 写数据到输出流
	 */
	synchronized public boolean write(byte[] b) {
		if (socket == null)
			return false;
		if (out == null)
			init();
		try {
			out.write(b);
			out.flush();
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	/**
	 * 写数据到输出流
	 * 
	 * @param b
	 *            数据
	 * @param iFrom
	 *            起始位置
	 * @param iLen
	 *            数据长度
	 * @return 0 写成功, -1 失败, 1 参数错误
	 */
	synchronized public int write(byte[] b, int iFrom, int iLen) {
		if (socket == null)
			return -1;
		try {
			if (b != null && iFrom >= 0 && iFrom + iLen <= b.length) {
				out.write(b, iFrom, iLen);
				out.flush();
				return 0;
			} else
				return 1;
		} catch (Exception e) {
		}
		return -1;
	}

	/**
	 * 取得可以从输入流读取的字节数 如果 -1 表示连接流出现问题
	 */
	synchronized public int available() {
		if (socket == null)
			return -1;
		if (in == null)
			init();
		int iLen = -1;
		try {
			iLen = in.available();
		} catch (Exception e) {
		}
		return iLen;
	}

	/**
	 * 从输入流读取指定字节长度的数据
	 * 
	 * @param iLen
	 *            数据长度
	 * @return 读取的字节, 如为 null 表示读取过程可能发生异常
	 */
	synchronized public byte[] read(int iLen) {
		if (socket == null)
			return null;
		if (iLen < 1)
			return null;

		byte[] b = new byte[iLen];
		int iRead = read(b, 0, iLen);
		if (iRead < 1)
			return null;
		if (iRead < iLen) {
			byte[] br = new byte[iRead];
			System.arraycopy(b, 0, br, 0, iRead);
			return br;
		}
		return b;
	}

	/**
	 * 从输入流读取指定字节长度的数据
	 * 
	 * @param b
	 *            数据 buffer
	 * @param iFrom
	 *            存放数据起点
	 * @param iLen
	 *            读取数据长度
	 * @return >=0 取得的数据字节数, -1 错误
	 */
	synchronized public int read(byte[] b, int iFrom, int iLen) {
		if (socket == null)
			return -1;
		if (b == null || iLen < 1 || iFrom < 0 || b.length < iFrom + iLen)
			return 0;
		int iGot = 0, iRead = 0;

		try {
			while (iRead < iLen && iGot >= 0) {
				iGot = in.read(b, iFrom + iRead, iLen - iRead);
				if (iGot > 0)
					iRead += iGot;
			}
			return iRead;
		} catch (Exception e) {
		}
		return -1;
	}

	public void close() {
		try {
			in.close();
		} catch (Exception e) {
		}
		try {
			out.close();
		} catch (Exception e) {
		}
		try {
			socket.close();
		} catch (Exception e) {
		}
		socket = null;
	}
	// {{DECLARE_CONTROLS
	// }}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -