java.io.IOException: 远程主机强迫关闭了一个现有的连接

如题,这是我的代码,请大侠们帮忙看看:

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();
// HOST = "127.0.0.1" ;
PORT = Integer
.parseInt(prop.getProperty("SOCKETPROT").trim());
// PORT = 5678;
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() {
//心跳访问

StringBuffer send = new StringBuffer();
Tool tool = new Tool();
String messageType = tool.getMessageType("04");// 报文序列号
send.append("0000").append(messageType).append("YDCZ").append("000119");
try {
sendData(send.toString().getBytes("UTF-16"));
System.out.println("心跳访问");
} 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)  {
try{
ChannelClient c = new ChannelClient();
MobileRecharge n = new MobileRecharge();
System.out.println("登陆请求");
String send = n.doLogin();
System.out.println(send);
c.sendData(send.getBytes("UTF-16"));
}catch(Exception ex){
ex.printStackTrace();
}
}
}

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(1024);
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();
}
}

}
=======================================================================

抛出:

java.io.IOException: 远程主机强迫关闭了一个现有的连接。
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:25)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
at sun.nio.ch.IOUtil.read(IOUtil.java:206)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:207)
at com.ofcard.mobile.channel.ChannelReadClient.run(ChannelReadClient.java:31)
at java.lang.Thread.run(Thread.java:534)

这是我客户端的问题还是服务端的问题?如果我本地起服务端是正常的,但是当我连接到远程的服务端时候就会出现上面的问题

你可能感兴趣的:(java,thread,socket,mobile,sun)