使用mina框架传输对象实体

使用mina框架传输对象实体方法如下:

 

对像实体

public class TransDataEntity {
	private String md5;
	private String path;
	private String fromIP;
	private byte[] filedata;
	public String getMd5() {
		return md5;
	}
	public void setMd5(String md5) {
		this.md5 = md5;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getFromIP() {
		return fromIP;
	}
	public void setFromIP(String fromIP) {
		this.fromIP = fromIP;
	}
	public byte[] getFiledata() {
		return filedata;
	}
	public void setFiledata(byte[] filedata) {
		this.filedata = filedata;
	}

}

 

服务端处理类

import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;





public class ServerHandler extends IoFilterAdapter implements IoHandler {
	private static ServerHandler samplMinaServerHandler = null;

	public static ServerHandler getInstances() {
		if (null == samplMinaServerHandler) {
			samplMinaServerHandler = new ServerHandler();
		}
		return samplMinaServerHandler;
	}

	private ServerHandler() {
	}

	// 当连接后打开时触发此方法,一般此方法与 sessionCreated 会被同时触发
	public void sessionOpened(IoSession session) throws Exception {
	}

	public void sessionClosed(IoSession session) {
	}

	public void messageReceived(IoSession session, Object message) throws Exception {
		if (message instanceof TransDataEntity) {
			TransDataEntity text = (TransDataEntity) message;
			
			System.out.println("服务器接收到从客户端的getFromIP:" + text.getFromIP());
			System.out.println("服务器接收到从客户端的getMd5:" + text.getMd5());
			System.out.println("服务器接收到从客户端的getPath:" + text.getPath());
			System.out.println("服务器接收到从客户端的文件内容:" + new String(text.getFiledata(), "GBK"));

		}
	}

	public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception {

	}

	// 当消息传送到客户端后触发
	public void messageSent(IoSession arg0, Object arg1) throws Exception {

	}

	// 当一个新客户端连接后触发此方法.
	public void sessionCreated(IoSession arg0) throws Exception {

	}

	// 当连接空闲时触发此方法.
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {

	}

}

 服务端程序

 

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.koubei.syn.utils.ConfigUtils;

public class MainServer {
	private static MainServer mainServer = null;
	private SocketAcceptor acceptor = new NioSocketAcceptor();
	private DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
	private int bindPort = ConfigUtils.PORT;

	public static MainServer getInstances() {
		if (null == mainServer) {
			mainServer = new MainServer();
		}
		return mainServer;
	}

	private MainServer() {
		chain.addLast("myChin", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
		acceptor.setHandler(ServerHandler.getInstances());
		try {
			acceptor.bind(new InetSocketAddress(bindPort));			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		MainServer.getInstances();
	}
}

 客户段处理类

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

public class ClientHandler extends IoHandlerAdapter {
	private static ClientHandler samplMinaClientHandler = null;


	public static ClientHandler getInstances() {
		if (null == samplMinaClientHandler) {
			samplMinaClientHandler = new ClientHandler();
		}
		return samplMinaClientHandler;
	}

	private ClientHandler() {

	}

	public void sessionOpened(IoSession session) throws Exception {
		session.write("客户端与服务器的会话打开了……");
	}

	public void sessionClosed(IoSession session) {
	}

	public void messageReceived(IoSession session, Object message) throws Exception {
	}

	public void messageSent(IoSession session, Object tdEntity) throws Exception {
		
		if (null != tdEntity){
			session.write(tdEntity);
		}
		System.out.println("客户端已经向服务器发送了:");
	}
}
 

客户端

 

import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;


import com.koubei.syn.utils.ConfigUtils;


public class MainClient {
	private static MainClient mainClient = null;
	NioSocketConnector connector = new NioSocketConnector();
	DefaultIoFilterChainBuilder chain = connector.getFilterChain();

	public static MainClient getInstances() {
		if (null == mainClient) {
			mainClient = new MainClient();
		}
		return mainClient;
	}

	private MainClient() {		

	}

	/**
	 * @author    展俊
	 * @description 客户端传数据给服务器端 
	 * @param groupips ip数组 127.0.0.1,127.0.0.2 ...
	 * @param msg 数据实体
	 * 
	 */
	public void messgeSent(String[] groupips, Object msg) {
		chain.addLast("myChin", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
		connector.setHandler(ClientHandler.getInstances());
		connector.setConnectTimeoutMillis(30);
		if ((null != groupips) && (groupips.length > 0)) {
			try {
				for (String ip : groupips) {
					ConnectFuture cf = connector.connect(new InetSocketAddress(ip, ConfigUtils.PORT));
					connector.getHandler().messageSent(cf.getSession(), msg);
				}
			} catch (Exception e) {
				
				e.printStackTrace();
			}

		}
	}

	
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值