我用NIO 写一个 简单的HTTP服务器
private void read(SelectionKey key) throws IOException {
readBuffer.clear();
SocketChannel socketChannel=(SocketChannel) key.channel();
int num=0;
try {
num=socketChannel.read(readBuffer);
LOG.info("读入 Byte数为 "+num);
} catch (IOException e) {
key.cancel();
socketChannel.close();
LOG.error("exception in read"+e);
}
if(num==-1){
key.channel().close();
key.cancel();
return;
}
LOG.info("读入 数据为 "+readBuffer.toString());
workerPool.processData(this, socketChannel, readBuffer.array(),num );
}
<html>
<head>
<title>test my server</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<p>meran</p>
powered by meran <br>
<form name="UploadForm" method="post" action="http://localhost:8084/">
<input type="text" name="myname" /><br>
<select name="myage">
<option value="18">18</option>
<option value="20">20</option>
<option value="22">22</option>
</select><br>
<input type="submit"value="Sutmit">
</form>
</body>
</html> 当我在浏览器输入 url 的时候可以解析成功,上面是我获取的 html 但是我点击确认的时候
36617 [Thread-1] ERROR com.meran.server.Server - exception in readjava.io.IOException: 远程主机强迫关闭了一个现有的连接。
36617 [Thread-1] INFO com.meran.server.Server - 读入 数据为 java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192]抛出下面这个异常 谁能给我解释一下这是什么意思??还有怎么解决??为什么客户端关闭连接 会使得 服务器端可读? 但是确什么都读不到?????
socket 连接关闭的时候到底执行哪写操作? 关闭时三次握手,不是应该服务器 与 客户端的 socket 都处于关闭状态么 ?为什么 在服务器端 调用 close() 方法 只是关闭服务器端 socket?
网络课本上说把socket 看成两个双工的 连接, 那么关闭的时候是怎么关闭的???
求大神解决 现在只能给100 分 帮帮小弟吧困惑许久
private void read(SelectionKey key) throws IOException {
readBuffer.clear();
SocketChannel socketChannel=(SocketChannel) key.channel();
int num=0;
try {
num=socketChannel.read(readBuffer);
LOG.info("读入 Byte数为 "+num);
} catch (IOException e) {
key.cancel();
socketChannel.close();
LOG.error("exception in read"+e);
}
if(num==-1){
key.channel().close();
key.cancel();
return;
}
LOG.info("读入 数据为 "+readBuffer.toString());
workerPool.processData(this, socketChannel, readBuffer.array(),num );
}
<html>
<head>
<title>test my server</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<p>meran</p>
powered by meran <br>
<form name="UploadForm" method="post" action="http://localhost:8084/">
<input type="text" name="myname" /><br>
<select name="myage">
<option value="18">18</option>
<option value="20">20</option>
<option value="22">22</option>
</select><br>
<input type="submit"value="Sutmit">
</form>
</body>
</html> 当我在浏览器输入 url 的时候可以解析成功,上面是我获取的 html 但是我点击确认的时候
36617 [Thread-1] ERROR com.meran.server.Server - exception in readjava.io.IOException: 远程主机强迫关闭了一个现有的连接。
36617 [Thread-1] INFO com.meran.server.Server - 读入 数据为 java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192]抛出下面这个异常 谁能给我解释一下这是什么意思??还有怎么解决??为什么客户端关闭连接 会使得 服务器端可读? 但是确什么都读不到?????
socket 连接关闭的时候到底执行哪写操作? 关闭时三次握手,不是应该服务器 与 客户端的 socket 都处于关闭状态么 ?为什么 在服务器端 调用 close() 方法 只是关闭服务器端 socket?
网络课本上说把socket 看成两个双工的 连接, 那么关闭的时候是怎么关闭的???
求大神解决 现在只能给100 分 帮帮小弟吧困惑许久
LZ你看一下别人是怎么写的。。
我想问一下 ,每次 用浏览器 访问一个页面都要建立一次连接么??? 还有 在 socket 关闭的时候具体是怎么回事。 我之前在服务器端调用 close()方法 ,但是在 客户端检测socekt是连接的。 是不是 可以把 Socket 连接看成是 两个 单向双工 的 ?
2:阻塞式多线程服务器(BN)
3:阻塞式线程池服务器(BP)
4:非阻塞单线程服务器(N1)
5:非阻塞双线程服务器(N2)
求解答
关闭连接 是否只是单项的关闭连接。 因为我写过测试代码,服务器端关闭相应socket 后, 在客户端检测,该 socket 并没有关闭 。 我关心的关闭过程 。 因为 TCP 关闭连接需要三次握手,可是握手后应该是两边Socket 都关闭啊 为什么会出现一边测试关闭一边测试结果却是连接的状况。