package com.bjhy.prison.action.fingerprint;import java.net.InetSocketAddress;import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.DefaultIoFilterChainBuilder;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.nio.SocketConnector;
import org.apache.mina.transport.socket.nio.SocketConnectorConfig;public class FingerPrintSockeClient { private static FingerPrintSockeClient instance = null;
public ConnectFuture cf;
private static final String HOSTNAME ="localhost";
private static final int PORT = 5010;
private static final int CONNECT_TIMEOUT = 30; // seconds
public FingerPrintSockeClient(){ SocketConnector connector = new SocketConnector();
DefaultIoFilterChainBuilder chain = connector.getFilterChain();
SocketConnectorConfig cfg = new SocketConnectorConfig();
cfg.setConnectTimeout( CONNECT_TIMEOUT );
cf = connector.connect(new InetSocketAddress(HOSTNAME, PORT),new ClientSessionHandler(), cfg );
}
public static synchronized FingerPrintSockeClient getInstance(){
if(instance == null) {
instance = new FingerPrintSockeClient();
}
return instance;
}
public void sendMsg(String msg,String[] args){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e.toString());
}
System.out.println("=============0000000000000"+args[0]);
System.out.println("判断是那种方式:"+msg);
if("add".equals(msg)){
String msgs="("+args[0]+",ygmod,"+args[1]+","+args[2]+",1,8888,24)";
IoSession ioSession = cf.getSession();
ByteBuffer buffer = ByteBuffer.allocate(1024, false);
byte[] bytes;
bytes=msgs.getBytes();
buffer.put(bytes);
buffer.flip();
ioSession.write(buffer);
}
if("get".equals(msg)){
String msgs="("+args[0]+",gettemplet,"+args[1]+",0)";
IoSession ioSession = cf.getSession();
ByteBuffer buffer = ByteBuffer.allocate(1024, false);
byte[] bytes;
bytes=msgs.getBytes();
buffer.put(bytes);
buffer.flip();
ioSession.write(buffer);
}
if("share".equals(msg)){
String msgs="("+args[0]+",sendtemplet,"+args[1]+",0)";
IoSession ioSession = cf.getSession();
ByteBuffer buffer = ByteBuffer.allocate(1024, false);
byte[] bytes;
bytes=msgs.getBytes();
buffer.put(bytes);
buffer.flip();
cf.getSession().write(buffer);
}}
/**
* 十六进制转换
* @param hex
* @return
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
} private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
}
请问各位我这样写在执行ioSession.write(buffer);后session数就加1,如果是这样有什么办法可以解决了,谢谢各位
import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.DefaultIoFilterChainBuilder;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.nio.SocketConnector;
import org.apache.mina.transport.socket.nio.SocketConnectorConfig;public class FingerPrintSockeClient { private static FingerPrintSockeClient instance = null;
public ConnectFuture cf;
private static final String HOSTNAME ="localhost";
private static final int PORT = 5010;
private static final int CONNECT_TIMEOUT = 30; // seconds
public FingerPrintSockeClient(){ SocketConnector connector = new SocketConnector();
DefaultIoFilterChainBuilder chain = connector.getFilterChain();
SocketConnectorConfig cfg = new SocketConnectorConfig();
cfg.setConnectTimeout( CONNECT_TIMEOUT );
cf = connector.connect(new InetSocketAddress(HOSTNAME, PORT),new ClientSessionHandler(), cfg );
}
public static synchronized FingerPrintSockeClient getInstance(){
if(instance == null) {
instance = new FingerPrintSockeClient();
}
return instance;
}
public void sendMsg(String msg,String[] args){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e.toString());
}
System.out.println("=============0000000000000"+args[0]);
System.out.println("判断是那种方式:"+msg);
if("add".equals(msg)){
String msgs="("+args[0]+",ygmod,"+args[1]+","+args[2]+",1,8888,24)";
IoSession ioSession = cf.getSession();
ByteBuffer buffer = ByteBuffer.allocate(1024, false);
byte[] bytes;
bytes=msgs.getBytes();
buffer.put(bytes);
buffer.flip();
ioSession.write(buffer);
}
if("get".equals(msg)){
String msgs="("+args[0]+",gettemplet,"+args[1]+",0)";
IoSession ioSession = cf.getSession();
ByteBuffer buffer = ByteBuffer.allocate(1024, false);
byte[] bytes;
bytes=msgs.getBytes();
buffer.put(bytes);
buffer.flip();
ioSession.write(buffer);
}
if("share".equals(msg)){
String msgs="("+args[0]+",sendtemplet,"+args[1]+",0)";
IoSession ioSession = cf.getSession();
ByteBuffer buffer = ByteBuffer.allocate(1024, false);
byte[] bytes;
bytes=msgs.getBytes();
buffer.put(bytes);
buffer.flip();
cf.getSession().write(buffer);
}}
/**
* 十六进制转换
* @param hex
* @return
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
} private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
}
请问各位我这样写在执行ioSession.write(buffer);后session数就加1,如果是这样有什么办法可以解决了,谢谢各位
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货