下面是我的代码,请大虾们帮忙给看看啊,在线等:
package com.ofcard.mobile.channel;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;public class ChannelClient {
private SocketChannel socketChannel;
private static  String HOST;
private static  int PORT ;
private Selector selector = null;
// private InputStream in;
// private OutputStream out;

public static String busType;// 业务类型
public static String loginMessageHead; // 登陆报文头
public static String loginMessageCode;// 登陆报文消息码
public static String carrierCode;// 运营商代码
public static String corporateCode;// 企业代码
public static String version;// 协议版本号
public static String psd;
public static String logoutMessageCode;// 登出消息码
public static String transPsd;// 企业交易密码

static {
Properties prop = new Properties();
InputStream in = Object.class.getResourceAsStream("/config.properties");
try {
prop.load(in);
HOST = prop.getProperty("SOCKETIP").trim();
PORT = Integer
.parseInt(prop.getProperty("SOCKETPROT").trim());
busType = prop.getProperty("BUSTYPE").trim();
loginMessageHead = prop.getProperty("LOGINMESSAGEHEAD").trim();
loginMessageCode = prop.getProperty("LOGINMESSAGECODE").trim();
carrierCode = prop.getProperty("CARRIERCODE").trim();
corporateCode = prop.getProperty("CORPORATECODE").trim();
version = prop.getProperty("VERSION").trim();
psd = prop.getProperty("PSD").trim();
logoutMessageCode = prop.getProperty("LOGOUTMESSAGECODE").trim();
transPsd = prop.getProperty("TRANSPSD").trim();
} catch (IOException e) {
e.printStackTrace();
}
}


public ChannelClient() throws IOException {
// TODO Auto-generated constructor stub
initialize();//初始化
comhandNet();
}
private void comhandNet() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
String send = "心跳访问";
try {
sendData(send.getBytes("UTF-16"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 0, 10000);
} //初始化客户端连接
private void initialize() throws IOException {
// TODO Auto-generated method stub
//打开监听通道
socketChannel  = SocketChannel.open(new InetSocketAddress(HOST,PORT));
//设置非阻塞模式
socketChannel.configureBlocking(false);

//打开选择器并注册到信道
selector = Selector.open();
socketChannel.register(selector,SelectionKey.OP_READ|SelectionKey.OP_WRITE);//读取集
//启动读取的线程----
new ChannelReadClient(selector);
}
/**
 * 发送数据到服务器
 * @param byout 
 * @throws IOException 
 */
public void sendData(byte[] byout) throws IOException{
ByteBuffer writeBuf = ByteBuffer.wrap(byout);
socketChannel.write(writeBuf);
// out = socketChannel.socket().getOutputStream();
// out.write(byout);
}
public static void main(String[] args) throws IOException {
ChannelClient c = new ChannelClient();
MobileRecharge n = new MobileRecharge();
String send = n.doLogin();
c.sendData(send.getBytes("UTF-16"));
}
}
package com.ofcard.mobile.channel;import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;public class ChannelReadClient extends Thread { private Selector selector;

public ChannelReadClient(Selector selector) {
this.selector = selector;
new Thread(this).start();
} public void run() {
// TODO Auto-generated method stub
try{
while(selector.select()>0){//有事件发生时
Iterator it = selector.selectedKeys().iterator();//对可用通道操作对应的selectedKey迭代
while(it.hasNext()){
SelectionKey skey = (SelectionKey) it.next();
if(skey.isValid()&&skey.isReadable()){//如果有效且可读
//读取channel中数据
SocketChannel channel = (SocketChannel) skey.channel();
ByteBuffer bf = ByteBuffer.allocate(4000000);
channel.read(bf);
bf.flip();
// 将字节转化为为UTF-16的字符串   
            String receivedString=Charset.forName("UTF-16").newDecoder().decode(bf).toString();
            // 控制台打印出来
            System.out.println("接收到来自服务器"+channel.socket().getRemoteSocketAddress()+"的信息:"+receivedString);
            //为下次读取准备
            skey.interestOps(SelectionKey.OP_READ|SelectionKey.OP_WRITE);
}
it.remove();//删除已经处理的SelectionKey
}
}
}catch(Exception e){
e.printStackTrace();
}
}}

解决方案 »

  1.   

    唉, 您的主机中的软件放弃了一个已建立的连接 是心跳数据包有问题,但是我改了之后有出现了:java.io.IOException: 远程主机强迫关闭了一个现有的连接。
    at sun.nio.ch.SocketDispatcher.write0(Native Method)
    at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:33)
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:104)
    at sun.nio.ch.IOUtil.write(IOUtil.java:75)
    at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:300)
    at com.ofcard.mobile.channel.ChannelClient.sendData(ChannelClient.java:105)
    at com.ofcard.mobile.channel.ChannelClient$1.run(ChannelClient.java:74)
    at java.util.TimerThread.mainLoop(Timer.java:432)
    at java.util.TimerThread.run(Timer.java:382)
    求大虾指点