示例代码见附件:很遗憾,太懒,代码内容还是我分的包,如果你要运行的话,还需要自己下一点小的功夫,改变一下包路径。
Server端代码:
package com.henushang.socket.chapter4nio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.util.Iterator; import java.util.Set; public class EchoServerNoBlock { private int port = 8000; private ServerSocketChannel serverSocketChannel; Selector selector; public EchoServerNoBlock() throws Exception { selector = Selector.open(); serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().setReuseAddress(true); serverSocketChannel.configureBlocking(false);// 设置通信为非阻塞模式,没有这一配置,则无法实现非阻塞 serverSocketChannel.socket().bind(new InetSocketAddress(port)); System.out.println("等待连接..."); } public void service() throws Exception { serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (selector.select() > 0) { Set<SelectionKey> readyKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = readyKeys.iterator(); SelectionKey key = null; while (iterator.hasNext()) { try { key = iterator.next(); iterator.remove(); if (key.isAcceptable()) { System.out.println("accept"); ServerSocketChannel ssc = (ServerSocketChannel) key .channel(); SocketChannel sc = ssc.accept(); System.out.println("接收到链接" + sc.socket().getInetAddress() + ":" + sc.socket().getPort()); sc.configureBlocking(false); ByteBuffer buffer = ByteBuffer.allocate(1024); sc.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, buffer); } if (key.isReadable()) { System.out.println("read"); receive(key); } if (key.isWritable()) { send(key); } } catch (Exception e) { e.printStackTrace(); if (key != null) { key.cancel(); key.channel().close(); } } } } } /** * 发送消息处理方法 * @param key * @throws Exception * @author henushang */ public void send(SelectionKey key) throws Exception { ByteBuffer buffer = (ByteBuffer) key.attachment(); SocketChannel socketChannel = (SocketChannel) key.channel(); buffer.flip(); String data = decode(buffer); if (data.indexOf("\r\n") == -1) { return; } String outputData = data.substring(0, data.indexOf("\n") + 1); System.out.println(outputData); ByteBuffer outputBuffer = encode("henushang:" + outputData); while (outputBuffer.hasRemaining()) { socketChannel.write(outputBuffer); } ByteBuffer temp = encode(outputData); buffer.position(temp.limit()); buffer.compact(); if (outputData.equals("byt\r\n")) { key.cancel(); socketChannel.close(); System.out.println("关闭连接"); } } /** * 接受链接处理方法 * @param key * @throws IOException * @author henushang */ public void receive(SelectionKey key) throws IOException { SocketChannel sc = (SocketChannel) key.channel(); ByteBuffer buffer = (ByteBuffer) key.attachment(); ByteBuffer readBuffer = ByteBuffer.allocate(32); sc.read(readBuffer); readBuffer.flip(); buffer.limit(readBuffer.capacity()); buffer.put(readBuffer); } static Charset charset = Charset.forName("UTF-8"); /** * 解码 * @param byteBuffer * @return * @author henushang */ public static String decode(ByteBuffer byteBuffer) { CharBuffer charBuffer = charset.decode(byteBuffer); return charBuffer.toString(); } /** * 编码 * @param msg * @return * @author henushang */ public static ByteBuffer encode(String msg) { return charset.encode(msg); } }
客户端代码:
package com.henushang.socket.chapter4nio; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.channels.SocketChannel; import com.henushang.socket.util.SocketUtils; public class EchoClient { private SocketChannel socketChannel; private int port = 8000; public EchoClient() throws Exception { socketChannel = SocketChannel.open(); InetAddress inetAddress = InetAddress.getLocalHost(); InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, port); socketChannel.connect(inetSocketAddress); System.out.println("准备连接服务器"); } public static void main(String[] args) throws Exception { new EchoClient().talk(); } public void talk() { try { BufferedReader reader = SocketUtils.getReader(socketChannel.socket()); PrintWriter pw = SocketUtils.getWriter(socketChannel.socket()); BufferedReader localreaderReader = new BufferedReader(new InputStreamReader(System.in)); String msg = null; while ((msg = localreaderReader.readLine()) != null) { System.out.println(msg); pw.println(msg); pw.flush(); System.out.println(reader.readLine()); if ("bye".equals(msg)) { break; } } } catch (IOException e) { e.printStackTrace(); }finally{ SocketUtils.close(socketChannel); } } }
工具类代码:
package com.henushang.socket.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.nio.channels.SocketChannel; public class SocketUtils { public static PrintWriter getWriter(Socket socket) throws IOException { OutputStream os = socket.getOutputStream(); return new PrintWriter(os); } public static PrintWriter getWriter(SocketChannel socketChannel) throws IOException { return getWriter(socketChannel.socket()); } public static BufferedReader getReader(Socket socket) throws IOException{ InputStream is = socket.getInputStream(); return new BufferedReader(new InputStreamReader(is, "UTF-8")); } public static BufferedReader getReader(SocketChannel socketChannel) throws IOException{ return getReader(socketChannel.socket()); } public static void close(Socket socket) { try { if (socket != null) { socket.close(); } } catch (Exception e) { e.printStackTrace(); } } public static void close(SocketChannel socketChannel) { try { if (socketChannel != null) { socketChannel.close(); } } catch (Exception e) { e.printStackTrace(); } } public static String echo(String msg) { return "echo:" + msg; } }
这种方式是一个线程在不停的循环检测监听到的事件,然后分别作出相应的处理。可是这种方式只适合那种处理过程时间短的情况,如果某一个操作处理的事件太长的话,则会让其他的事件一直处于等待状态,比如发送或者接收一个很大的文件,这样显然是很不合适的。
使用NIO的NoBlocking的操作方式固然减少了系统使用多线程建立连接的开销,但是也并不是完全适合任何情况的。
所以,在几种方式中没有最好的,只有最合适的。
而做程序也是一样的,没有那个语言是最好的,只有最合适的语言。
相关推荐
javasocket通信编程 代码,基本的javasocket通信
JavaSocket聊天室实现_源码
基于JavaSocket多客户端并发通信聊天程序的设计与实现..pdf基于JavaSocket多客户端并发通信聊天程序的设计与实现..pdf基于JavaSocket多客户端并发通信聊天程序的设计与实现..pdf基于JavaSocket多客户端并发通信聊天...
基于JavaSocket多客户端并发通信聊天程序的设计与实现..docx基于JavaSocket多客户端并发通信聊天程序的设计与实现..docx基于JavaSocket多客户端并发通信聊天程序的设计与实现..docx基于JavaSocket多客户端并发通信...
一个用java做的简单类似于QQ,飞鸽的局域网聊天系统
基于多线程实现的JavaSocket客户端-服务端点对点异步通信程序代码
基于JavaSocket多客户端并发通信聊天程序的设计与实现
java教程-javasocket例子,包含多线程部分实现
javaSocket的即时通信系统客户端代码分享
基于JavaSocket技术的多功能网络通信系统
两步学会JavaSocket编程两步学会JavaSocket编程两步学会JavaSocket编程
socket学习资料刚接触socket的同学呢。可以学习。如果说是老手阿那就是免了。
JavaSocket实现多人聊天室.pdf
javasocket使用TCP协议实现的聊天室.pdf
学习javasocket服务器给浏览器发信息.pdf
利用Socket原理java实现双方实时通信。只要将代码中客户端的回环地址更改成通信的另一方的IP地址,再把服务器端程序在此IP地址上编译运行即可。在服务端开启后,再使本机的客户端程序运行即可实现双方通信。
Java课程大作业JavaSocket聊天室实现源码+项目说明.7z 【实现功能】 用Java图形用户界面编写聊天室服务器端和客户端, 支持多个客户端连接到一个服务器。每个客户端能够输入账号。 可以实现群聊(聊天记录显示在所有...
有关javasocket实现qq聊天功能,和javaMail的demo详解,还是多线程的