使用mina框架传输对象实体
使用mina框架传输对象实体方法如下:
?
对像实体
?服务端程序
?
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();}}}}?
?