首页 新闻 搜索 专区 学院

Exception in thread "main" java.io.IOException: 您的主机中的软件放弃了一个已建立的连接。

0
悬赏园豆:50 [待解决问题]

服务器端代码:

package com.tracy.www;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class NIOServer {
private int ports[];
private ByteBuffer echoBuffer = ByteBuffer.allocate(1024);

public NIOServer(int ports[]) throws IOException {
this.ports = ports;

go();
}

private void go() throws IOException {
// Create a new selector
Selector selector = Selector.open();

// Open a listener on each port, and register each one
// with the selector
for (int i = 0; i < ports.length; ++i) {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ServerSocket ss = ssc.socket();
InetSocketAddress address = new InetSocketAddress(ports[i]);
ss.bind(address);

SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);

System.out.println("Going to listen on " + ports[i]);
}

while (true) {
int num = selector.select();
Set selectedKeys = selector.selectedKeys();
Iterator it = selectedKeys.iterator();

while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();

if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
// Accept the new connection
ServerSocketChannel ssc = (ServerSocketChannel) key
.channel();
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);

// Add the new connection to the selector
SelectionKey newKey = sc.register(selector,
SelectionKey.OP_READ);
it.remove();

System.out.println("Got connection from " + sc);
} else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
// Read the data
SocketChannel sc = (SocketChannel) key.channel();
// System.out.println(sc.isOpen());
// Echo data
int bytesEchoed = 0;
echoBuffer.clear();
int num1 = sc.read(echoBuffer);

while (num1 > 0) {

echoBuffer.flip();

sc.write(echoBuffer); //此处会报错,由于客户端已经关闭。但是怎么避免这种情况,如果以报错,服务器就会断掉。
bytesEchoed += num1;
num1 = sc.read(echoBuffer);

}

System.out.println(bytesEchoed);

if(num1<0){
System.out.println("关闭");
sc.close();
}

it.remove();
}

}

// System.out.println( "going to clear" );
// selectedKeys.clear();
// System.out.println( "cleared" );
}
}

static public void main(String args[]) throws Exception {
int[] ports = new int[] { 8081, 8080, 45 };

new NIOServer(ports);
}
}

客户端代码:

package com.tracy.client.www;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("10.152.5.172",8080);
System.out.println( "连接服务器成功。");
OutputStream outputStream=socket.getOutputStream();
OutputStreamWriter oWriter=new OutputStreamWriter(outputStream);
BufferedWriter bWriter=new BufferedWriter(oWriter);
bWriter.write("Hi,NIOServer\r\n\r\n");
bWriter.flush();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bWriter.write("Hi,NIOServer\r\n\r\n");
bWriter.flush();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

Tracy_wang的主页 Tracy_wang | 初学一级 | 园豆:152
提问于:2014-04-14 16:59
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册