Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Java NIO Socket communication

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要讲解了"Java NIO Socket通信的方法是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java NIO Socket通信的方法是什么"吧!

服务器代码:

import java.net.*; import java.nio.*; import java.nio.channels.*; import java.util.*; public class server { ServerSocketChannel ssc ; public void start() { try { Selector selector = Selector.open(); ServerSocketChannel ssc=ServerSocketChannel.open(); ssc.configureBlocking(false); ServerSocket ss=ssc.socket(); InetSocketAddress address = new InetSocketAddress(55555); ss.bind(address); ssc.register(selector, SelectionKey.OP_ACCEPT); System.out.println("端口注册完毕!"); while(true) { selector.select(); Set selectionKeys=selector.selectedKeys(); Iterator iter=selectionKeys.iterator(); ByteBuffer echoBuffer=ByteBuffer.allocate(20); SocketChannel sc; while(iter.hasNext()) { SelectionKey key=iter.next(); if((key.readyOps()&SelectionKey.OP_ACCEPT)==SelectionKey.OP_ACCEPT) { ServerSocketChannel subssc=(ServerSocketChannel)key.channel(); sc=subssc.accept(); sc.configureBlocking(false); sc.register(selector, SelectionKey.OP_READ); iter.remove(); System.out.println("有新连接:"+sc); } else if((key.readyOps()&SelectionKey.OP_READ)==SelectionKey.OP_READ) { sc=(SocketChannel) key.channel(); while(true) { echoBuffer.clear(); int a; try { a=sc.read(echoBuffer); } catch(Exception e) { e.printStackTrace(); break; } if(a==-1) break; if(a>0) { byte[] b=echoBuffer.array(); System.out.println("接收数据: "+new String(b)); echoBuffer.flip(); sc.write(echoBuffer); System.out.println("返回数据: "+new String(b)); } } sc.close(); System.out.println("连接结束"); System.out.println("============================="); iter.remove(); } } } } catch (Exception e) { e.printStackTrace(); } } }

客户端代码:

import java.net.*; import java.nio.*; import java.nio.channels.*; public class client { public void start() { try { SocketAddress address = new InetSocketAddress("localhost",55555); SocketChannel client=SocketChannel.open(address); client.configureBlocking(false); String a="asdasdasdasddffasfas"; ByteBuffer buffer=ByteBuffer.allocate(20); buffer.put(a.getBytes()); buffer.clear(); int d=client.write(buffer); System.out.println("发送数据: "+new String(buffer.array())); while(true) { buffer.flip(); int i=client.read(buffer); if(i>0) { byte[] b=buffer.array(); System.out.println("接收数据: "+new String(b)); client.close(); System.out.println("连接关闭!"); break; } } } catch(Exception e) { e.printStackTrace(); } }感谢各位的阅读,以上就是"Java NIO Socket通信的方法是什么"的内容了,经过本文的学习后,相信大家对Java NIO Socket通信的方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report