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

📄 serverthreadio.java

📁 JAVA解析MSNP15协议
💻 JAVA
字号:
package jm.framework.msn.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import jm.framework.msn.SystemManager;
import jm.framework.msn.msg.LoginMessage;
import jm.framework.msn.msg.group.MsgGroupFactory;
import jm.framework.msn.msg.group.UserGroupFactory;
import jm.framework.msn.protocol.out.OutVER;
import jm.framework.msn.usr.User;
import jm.framework.msn.util.Log;
import jm.util.JMRpcThread;


/**
 * 登录验证的线程 梦界家园MSNP15
 * 
 * @author ISHome
 * @since 0.5.0.1
 * @version 0.1
 */
public class ServerThreadIO extends JMRpcThread 
{
	protected Socket socket = null;
	protected InputStream in = null;
	protected OutputStream out = null;
	protected MsgGroupFactory messageGroup = null;
	protected SystemManager systemManager = null;

	public static void main(String[] args) {
		try {
			UserGroupFactory userFactory = SystemManager.getInstance().getUserGroup();
			User loginer = new User();
			loginer.setLoginID("ishometest@live.cn");
			loginer.setPassWord("123456789");
			userFactory.setLoginer(loginer);
			ServerThreadIO login = SystemManager.getInstance().getThreadGroup().getServerThread();
			login.login();
			login.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ServerThreadIO() {
		super();
		this.systemManager = SystemManager.getInstance();
		this.messageGroup = systemManager.getMessageGroup();
	}

	/**
	 * 登录MSN
	 */
	public void login() {
		try {
			// 打开连接
			Log.info("openConnection ... START");
			while (!openConnection()) {
				Log.info("    ... openConnection ... ");
			}
			// 开始登录
			Log.info("login ... START");
			messageGroup.addLoginMessage((new OutVER()).outVoker(null));
			//*********************************************************//
			//不稳定,线程交换的时候需要重新解决
			messageGroup.getLoginMessagesCount();
			sleep(500);
		} catch (Exception ex) {
		}
	}

	/**
	 * 运行线程
	 */
	public void run() {
		try {
			// 开始线程
			while (true) {
				sleep(500);
				// 发送数据
				if (messageGroup.getLoginMessagesCount() > 0) {
					LoginMessage outMsg = messageGroup.getLoginMessage();
					if (outMsg!=null&&!"".equals(outMsg.toString())) {
						writeData(outMsg.toString());
					}
				}
				// 等待下次处理
				sleep(500);
				// 接收数据
				if (readData()) {
					try {
						// 处理返回
						LoginMessage inMsg = new LoginMessage();
						inMsg.load(inBuffer);
						LoginThread login = systemManager.getThreadGroup().getLoginThread(this);
						login.setInMsg(inMsg);
						login.start();
					} catch (Exception ex) {
						// MIME信息
					}
				}
			}
		} catch (Exception ex) {
			this.interrupt();
		} finally {
			closeConnection();
		}
	}

	// 服务器返回数据
	private String inBuffer = null;

	/**
	 * 读取数据
	 * 
	 * @return
	 * @throws IOException
	 */
	private void writeData(String msg) {
		try {
			Log.debug(">>> " + msg);
			out.write((msg + "\r\n").getBytes("UTF-8"));
			out.flush();
		} catch (Exception e) {
		}
	}

	/**
	 * 读取数据
	 * 
	 * @return
	 * @throws IOException
	 */
	private boolean readData() {
		try {
			ByteArrayOutputStream inbuf = new ByteArrayOutputStream();
			inbuf.reset();
			while (true) {
				int v = in.read();
				if (v == -1)
					return false;
				if (v == 13)
					continue;
				if (v == 10)
					break;
				inbuf.write(v);
			}
			inBuffer = new String(inbuf.toByteArray(), "UTF-8");
		} catch (Exception e) {
			return false;
		}
		Log.debug("<<< " + inBuffer);
		return true;
	}

	/**
	 * 利用tcp/ip连接到server,生成输入输出流。基本编码为UTF-8。
	 */
	protected boolean openConnection() {
		try {
			InetSocketAddress serverAddress = systemManager.getServerAddress();
			Log.info(" MSN Server >>> " + serverAddress);
			this.socket = new Socket(serverAddress.getAddress(), serverAddress.getPort());
			this.in = socket.getInputStream();
			this.out = socket.getOutputStream();
		} catch (Exception ex) {
			// 关闭服务
			return false;
		}
		Log.info("openConnection ... OK");
		return true;
	}

	/**
	 * 关闭连接
	 * 
	 * @throws IOException
	 */
	protected boolean closeConnection() {
		try {
			if (in != null)
				in.close();
			if (out != null)
				out.close();
			if (socket != null)
				socket.close();
		} catch (Exception ex) {
			return false;
		}
		Log.info("closeConnection ... OK");
		return true;
	}

}

⌨️ 快捷键说明

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