socketchannel 编程(一)
来源:互联网 发布:淘宝十大平面女模特 编辑:程序博客网 时间:2024/06/04 01:19
1、 服务端
public class HelloWorld() { public static void main(String[] args) throws IOException { ServerSocketChannel channel = ServerSocketChannel.open(); channel.socket().bind(new InetSocketAddress(9999)); //配置连接模式: 阻塞或非阻塞 channel.configureBlocking(false);//非阻塞 while(true) { SocketChannel socket = channel.accept(); if (socket != null ) { //非阻塞的模式需要判断是否连接 ByteBuffer buf = ByteBuffer.allocate(48); int byteRead = socket.read(buf); if ( byteRead >= 1 ) { // others } } } }}//需要关闭socket和serversocketchannel
2、 客户端
public class HelloWorld() { public static void main(String[] args) throws IOException { InetAddress addr = InetAddress.getByName("127.0.0.1"); SocketAddress remote = new InetSocketAddress(addr, 9999); SocketChannel clientChannel = SocketChannel.open(remote); String msg = "fuck you shit"; ByteBuffer buf = ByteBuffer.allocate(200); buf.clear(); buf.put(msg.getBytes()); buf.flip(); while (buf.hasRemaining()) { clientChannel.write(buf); } clientChannel.close(); }}
3、网络七层模型:
应用层: Telnet/http/ftp/nfs/smtp
表示层: 加密,ascii
会话层: RPC/SQL
传输层: TCP/UDP/SPX
网络层:IP,IPX //报文
数据链路层: ATM/FDDI/Ethernet
物理层
0 0
- socketchannel 编程(一)
- (八) SocketChannel
- NIO(4) SocketChannel
- SocketChannel
- SocketChannel
- SocketChannel
- 网络编程之SocketChannel & ServerSocketChannel & Selector
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(五) SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(五) SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java--二维数组中对角线的一边为相同的数字
- JAVA--网络编程(UDP)
- 卷积神经网络——输入层、卷积层、激活函数、池化层、全连接层
- Linux 编写一个简单的Shell脚本
- python爬虫
- socketchannel 编程(一)
- Web4 标签和html基本概念
- 2017/4/16 哈尔滨理工大学省选题解Day2
- 队列(下)之循环队列
- linux管理之系统延迟及定时机制
- Android学习视频Mars-Android开发视频教程(全集)
- LaTeX局部改变字体类型
- 微博登录过程分析以及scrapy模拟登录微博
- 欢迎使用CSDN-markdown编辑器