package test.io;
import java.nio.channels.*;
import java.nio.charset.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.nio.*;
public class FileServer {
private int port = 8050;
private ServerSocketChannel serverSocketChannel;
private Charset charset = Charset.forName("GBK");
private Selector selector = null;
public FileServer() throws IOException {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
System.out.println("服务器启动");
}
/* 编码过程 */
public ByteBuffer encode(String str) {
return charset.encode(str);
}
/* 解码过程 */
public String decode(ByteBuffer bb) {
return charset.decode(bb).toString();
}
/* 服务器服务方法 */
public void service() throws IOException {
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
/** 外循环,已经发生了SelectionKey数目 */
while (selector.select() > 0) {
/* 得到已经被捕获了的SelectionKey的集合 */
Iterator iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = null;
try {
key = (SelectionKey) iterator.next();
iterator.remove();
if (key.isAcceptable()) {
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc = ssc.accept();
System.out.println("客户端机子的地址是 " + sc.socket().getLocalAddress() + " 客户端机机子的端口号是 " + sc.socket().getLocalPort());
sc.configureBlocking(false);
ByteBuffer buffer = ByteBuffer.allocate(1024);
sc.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, buffer);
}
if (key.isReadable()) {
reveice(key);
}
if (key.isWritable()) {
send(key);
}
} catch (IOException e) {
e.printStackTrace();
try {
if (key != null) {
key.cancel();
key.channel().close();
}
} catch (ClosedChannelException cex) {
e.printStackTrace();
}
}
}
/* 内循环完 */
}
/* 外循环完 */
}
/* service方法完 */
public void reveice(SelectionKey key) throws IOException {
if (key == null)
return;
ByteBuffer buff = (ByteBuffer) key.attachment();
SocketChannel sc = (SocketChannel) key.channel();
buff.limit(buff.capacity());
buff.position(0);
sc.read(buff);
buff.flip();
String reviceData = decode(buff);
}
/* 发送文件 */
public void send(SelectionKey key) {
if (key == null)
return;
ByteBuffer buff = (ByteBuffer) key.attachment();
SocketChannel sc = (SocketChannel) key.channel();
String data = decode(buff);
if (data.indexOf("get") == -1)
return;
String subStr = data.substring(data.indexOf(" "), data.length());
System.out.println("截取之后的字符串是 " + subStr);
FileInputStream fileInput = null;
try {
fileInput = new FileInputStream(subStr);
FileChannel fileChannel = fileInput.getChannel();
fileChannel.transferTo(0, fileChannel.size(), sc);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInput.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new FileServer().service();
}
}
http://www.java2000.net/viewthread.jsp?tid=6079
http://topic.csdn.net/u/20080610/02/6e33be0a-152f-481a-a10e-f9c11c8fd9ad.html?seed=577363641
<script type="text/javascript">
</script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script>
分享到:
相关推荐
NIO(服务端和客户端代码) 博文链接:https://songjianyong.iteye.com/blog/1757406
java的ServerSocketChannel与SocketChannel的使用
jdk api-ServerSocketChannel、Selector、ByteBuffer结合实现网络报文间的通讯
我们在此示例中实现了服务器端应用程序,因此将使用两个NioEventLoopGroup。第一个,通常称为“老板”,接受传入连接。第二个,通常称为“工人”,一旦老板接受连接并将接受的连接注册到工作人员,就处理被接受连接...
一个关于SocketChannel、ServerSocketChannel、Selector的综合案例
java api之ServerSocketChannel基础、应用场景、实战讲解
【IT十八掌徐培成】Java基础第27天-02.NIO-ServerSocketChannel-SocketChannel.zip
用Java实现非阻塞通信 java.nio包提供了支持非阻塞通信的类,主要包括: ● ServerSocketChannel:ServerSocket的替代类,支持阻塞通信与非阻塞通信。 ● SocketChannel:Socket的替代类,支持阻塞通信与非阻塞通信...
相反,必须使用手动编排SSL交换。 该项目提供了和,可以像和一样使用。入门直接下载您可以直接下载并将其放置在您的项目中。使用Maven 将以下依赖项添加到您的Maven项目中: < dependency> < groupId>org.baswell...
Java中的NIO其实是NewIO(另一种解释是NO-blocking IO,确实,在Java中可以通过ServerSocketChannel实现非双向IO) 注意:ServerSocketChannel至少是一对的,所以可以代替JDK1.4之前的BIO(ServerSocket) 其实Java...
使用netty实现一个游戏服务器,地址为127.0.0.1,端口为12345,游戏前端测试地址为 测试结果如图: 服务器代码如下: package com.qzx.herostory; import io.netty.bootstrap.ServerBootstrap; import io.netty....
使用socket channel 连接服务器,断线重连,发送保活等的封装类。 Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道。可以通过以下2种方式创建SocketChannel: 打开一个SocketChannel并连接到互联网上的...
1.ServerSocketChannel与SocketChannel通讯 2.java Fork/Join 与ThreadPool使用
The networking capabilities of the Java platform have been extended considerably since the first edition of the book. This new edition covers version 1.5-1.7, the most current iterations, as well as ...
Java NIO非堵塞技术实际是采取Reactor模式,或者说是Observer模式为我们监察I/O端口,如果有内容进来,会自动通知我们,这样,我们就不必开启多个线程死等,从外界看,实现了流畅的I/O读写,不堵塞了。 Java NIO...
本文来自于csdn,本文主要介绍了关于Netty的原理架构解析,介绍的Netty Reactor模型以及服务端Netty的工作架构,希望对您的学习有所...使用时需要熟练掌握Selector、ServerSocketChannel、SocketChannel、ByteBuffer等
initAndRegister()[初始化注册] newChannel()[创建服务器端频道]通过用户在ServerBootstrap.channel中创建的NioServerSocketChannel创建ReflectiveChannelFactory反射创建服务器端Channel()newSocket()...
1) 创建ServerSocketChannel,并设置相应参数 2) 创建Selector并注册到ServerSocketChannel上 3) 调用Selector的select方法等待请求 4) Selector接收到请求后使用selectedKeys返回SelectionKey集合 5) ...
08-Java NIO-Channel-ServerSocketChannel.mp4 09-Java NIO-Channel-SocketChannel.mp4 10-Java NIO-Channel-DatagramChannel.mp4 11-Java NIO-Channel-分散和聚集.mp4 12-Java NIO-Buffer-概述.mp4 13-Java NIO-...