first commit
This commit is contained in:
commit
2c1e62b911
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Created by .ignore support plugin (hsz.mobi)
|
||||
### Example user template template
|
||||
### Example user template
|
||||
|
||||
# IntelliJ project files
|
||||
.idea
|
||||
*.iml
|
||||
out
|
||||
gen
|
||||
.idea/
|
||||
target/
|
9
2.txt
Normal file
9
2.txt
Normal file
@ -0,0 +1,9 @@
|
||||
三次握手
|
||||
B-> SYN -> A
|
||||
B-> SYN/ACK ->A
|
||||
B <-ACK<- A
|
||||
四次分手
|
||||
B-> ACK/FIN -> A
|
||||
B<- ACK <- A
|
||||
B<- ACK/FIN <- A
|
||||
B-> ACK -> A
|
11
nettyPro/.gitignore
vendored
Normal file
11
nettyPro/.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Created by .ignore support plugin (hsz.mobi)
|
||||
### Example user template template
|
||||
### Example user template
|
||||
|
||||
# IntelliJ project files
|
||||
.idea
|
||||
*.iml
|
||||
out
|
||||
gen
|
||||
.idea/
|
||||
target/
|
19
nettyPro/pom.xml
Normal file
19
nettyPro/pom.xml
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>netty_ALL</artifactId>
|
||||
<groupId>cn.zyjblogs.netty</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>nettyPro</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
59
nettyPro/src/main/java/cn/zyjblogs/netty/bio/BIOServer.java
Normal file
59
nettyPro/src/main/java/cn/zyjblogs/netty/bio/BIOServer.java
Normal file
@ -0,0 +1,59 @@
|
||||
package cn.zyjblogs.netty.bio;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class BIOServer {
|
||||
public static void main(String[] args) {
|
||||
|
||||
//线程池机制
|
||||
|
||||
//思路
|
||||
//1. 创建一个线程池
|
||||
//2. 如果有客户端链接,就创建一个线程与之通讯
|
||||
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
|
||||
|
||||
//创建ServerSocket
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(6666);
|
||||
System.out.println("服务器启动了");
|
||||
while (true) {
|
||||
System.out.println("等待连接");
|
||||
final Socket socket = serverSocket.accept();
|
||||
System.out.println("连接到一个客户端了");
|
||||
//就创建一个线程与之通讯
|
||||
newCachedThreadPool.execute(() -> {
|
||||
//可以和客户端通讯
|
||||
handler(socket);
|
||||
});
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void handler(Socket socket) {
|
||||
try (InputStream inputStream = socket.getInputStream()) {
|
||||
System.out.println("线程信息id = "+Thread.currentThread().getId()+" 名称 = "+Thread.currentThread().getId());
|
||||
byte[] bytes = new byte[1024];
|
||||
while (true) {
|
||||
System.out.println("read....");
|
||||
int read = inputStream.read(bytes);
|
||||
System.out.println("线程信息id = "+Thread.currentThread().getId()+" 名称 = "+Thread.currentThread().getId());
|
||||
if (read == -1) {
|
||||
break;
|
||||
}
|
||||
//输出客户端发送的数据
|
||||
System.out.println(new String(bytes,0,read));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
System.out.println("关闭客户端链接");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package cn.zyjblogs.netty.nio;
|
||||
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
public class BasicBuffer {
|
||||
public static void main(String[] args) {
|
||||
//创建一个buffer
|
||||
IntBuffer intBuffer = IntBuffer.allocate(5);
|
||||
//向buffer中存入数据
|
||||
for (int i=0;i <intBuffer.capacity();i++){
|
||||
intBuffer.put(i<<1);
|
||||
}
|
||||
|
||||
//如何从buffer读取数据
|
||||
//将buffer转换,读写切换
|
||||
intBuffer.flip();
|
||||
|
||||
while (intBuffer.hasRemaining()){
|
||||
System.out.println(intBuffer.get());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package cn.zyjblogs.netty.nio;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
|
||||
/**
|
||||
* 1. MappedByteBuffer可以让文件直接在内存(堆外内存)中修改,操作系统不需要拷贝
|
||||
*/
|
||||
public class MappedByteBufferTest {
|
||||
public static void main(String[] args) {
|
||||
try (RandomAccessFile randomAccessFile = new RandomAccessFile("2.txt", "rw")) {
|
||||
//获取对应通道
|
||||
FileChannel channel = randomAccessFile.getChannel();
|
||||
/**
|
||||
* 参数1: FileChannel.MapMode.READ_WRITE 使用读写
|
||||
* 参数2: 0 可以直接修改的起始位置
|
||||
* 参数3: 5 使映射到内存的大小,即将1.txt的多个字节映射到内存
|
||||
* 可以直接修改的范围只0-5 (使内存的大小不是索引位置)
|
||||
* 实际类型 DirectByteBuffer
|
||||
*/
|
||||
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
|
||||
mappedByteBuffer.put(0, (byte) 'H');
|
||||
mappedByteBuffer.put(3, (byte) '9');
|
||||
System.out.println("修改成功~~~");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package cn.zyjblogs.netty.nio;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* copyright (C), 2021, 北京同创永益科技发展有限公司
|
||||
*
|
||||
* @author zhuyijun
|
||||
* @version 1.0.0
|
||||
* <author> <time> <version> <description>
|
||||
* zhuyijun 2021/10/8 16:22 1.0
|
||||
* @program netty_study
|
||||
* @description
|
||||
* @create 2021/10/8 16:22
|
||||
*/
|
||||
public class NIOByteBufferPutGet {
|
||||
public static void main(String[] args) {
|
||||
//创建buffer
|
||||
ByteBuffer buffer = ByteBuffer.allocate(64);
|
||||
|
||||
//类型化放入数据
|
||||
buffer.putInt(100);
|
||||
buffer.putLong(9);
|
||||
buffer.putChar('逝');
|
||||
buffer.putShort((short) 4);
|
||||
//取出 翻转缓冲区
|
||||
buffer.flip();
|
||||
//类型化取出顺序不对可能抛出异常
|
||||
System.out.println(buffer.getInt());
|
||||
System.out.println(buffer.getLong());
|
||||
System.out.println(buffer.getChar());
|
||||
System.out.println(buffer.getShort());
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package cn.zyjblogs.netty.nio;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
|
||||
public class NIOFileChannel01 {
|
||||
public static void main(String[] args) {
|
||||
String str = "hello 逝水无痕";
|
||||
//同创一个输出流-> channel
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt")){
|
||||
//通过fileOutputStream 获取对应的FileChannel 这个FileChannel 正是类型使FileChannelImpl
|
||||
FileChannel channel = fileOutputStream.getChannel();
|
||||
|
||||
//创建一个缓冲区ByteBuffer
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
//将src放入byteBuffer中
|
||||
buffer.put(str.getBytes());
|
||||
|
||||
//对byteBuffer进行flip 切换为读
|
||||
buffer.flip();
|
||||
//将ByteBuffer缓冲区数据写入到通道中
|
||||
channel.write(buffer);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package cn.zyjblogs.netty.nio;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class NIOFileChannel02 {
|
||||
public static void main(String[] args) {
|
||||
//创建文件的输入流
|
||||
File file = new File("d:\\file01.txt");
|
||||
try(FileInputStream fileInputStream = new FileInputStream(file)) {
|
||||
//通过fileInputStream 获取对应的FileChannel 这个FileChannel 正是类型使FileChannelImpl
|
||||
FileChannel fileChannel = fileInputStream.getChannel();
|
||||
|
||||
//创建一个缓冲区ByteBuffer
|
||||
ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
|
||||
|
||||
//将fileChannel通道的数据读入到buffer
|
||||
fileChannel.read(buffer);
|
||||
//ByteBuffer 的字节数据 转成string
|
||||
System.out.println(new String(buffer.array()));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package cn.zyjblogs.netty.nio;
|
||||
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class NIOFileChannel03 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
//创建文件的输入流
|
||||
try(FileInputStream fileInputStream = new FileInputStream(NIOFileChannel03.class.getClassLoader().getResource("1.txt").getFile());
|
||||
FileOutputStream fileOutputStream = new FileOutputStream("2.txt")) {
|
||||
FileChannel fileInputChannel = fileInputStream.getChannel();
|
||||
|
||||
FileChannel FileOutputchannel = fileOutputStream.getChannel();
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(512);
|
||||
|
||||
while (true){
|
||||
//重要的操作 清空和复位buffer 如果clear会死循环
|
||||
buffer.clear();
|
||||
int read = fileInputChannel.read(buffer);
|
||||
System.out.println("read: "+read);
|
||||
if (read ==-1){
|
||||
//读取结束
|
||||
break;
|
||||
}
|
||||
buffer.flip();
|
||||
FileOutputchannel.write(buffer);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package cn.zyjblogs.netty.nio;
|
||||
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class NIOFileChannel04 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
//创建文件的输入流
|
||||
try(FileInputStream fileInputStream = new FileInputStream("logo.png");
|
||||
FileOutputStream fileOutputStream = new FileOutputStream("logo2.png")) {
|
||||
FileChannel sourceChannel = fileInputStream.getChannel();
|
||||
|
||||
FileChannel destchannel = fileOutputStream.getChannel();
|
||||
//使用transferFrom完成拷贝
|
||||
destchannel.transferFrom(sourceChannel,0,sourceChannel.size());
|
||||
sourceChannel.close();
|
||||
destchannel.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package cn.zyjblogs.netty.nio;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* copyright (C), 2021, 北京同创永益科技发展有限公司
|
||||
*
|
||||
* @author zhuyijun
|
||||
* @version 1.0.0
|
||||
* <author> <time> <version> <description>
|
||||
* zhuyijun 2021/10/8 16:22 1.0
|
||||
* @program netty_study
|
||||
* @description
|
||||
* @create 2021/10/8 16:22
|
||||
*/
|
||||
public class ReadOnlyBuffer {
|
||||
public static void main(String[] args) {
|
||||
//创建buffer
|
||||
ByteBuffer buffer = ByteBuffer.allocate(64);
|
||||
for (int i = 0; i < 64; i++){
|
||||
buffer.put((byte)i);
|
||||
}
|
||||
//读取
|
||||
buffer.flip();
|
||||
|
||||
//得到一个这只读buffer
|
||||
ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
|
||||
System.out.println(readOnlyBuffer.getClass());
|
||||
|
||||
//读取
|
||||
while (readOnlyBuffer.hasRemaining()){
|
||||
System.out.println(readOnlyBuffer.get());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package cn.zyjblogs.netty.nio;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Scattering: 将数据写入到Buffer时,可以采用buffer数组,依次写入
|
||||
* Gethering: 从buffer读取数据时,可以采用buffer数组,依次读取
|
||||
*/
|
||||
public class ScatteringAndGetheringTest {
|
||||
public static void main(String[] args) {
|
||||
//使用 ServerSocketChannel 和 SocketChannel 网络
|
||||
try (ServerSocketChannel serverSocketChannel = ServerSocketChannel.open()) {
|
||||
InetSocketAddress inetSocketAddress = new InetSocketAddress(7000);
|
||||
//绑定端口到socket,并启动
|
||||
serverSocketChannel.socket().bind(inetSocketAddress);
|
||||
//创建buffer数组
|
||||
ByteBuffer[] byteBuffers = new ByteBuffer[2];
|
||||
byteBuffers[0] = ByteBuffer.allocate(5);
|
||||
byteBuffers[1] = ByteBuffer.allocate(3);
|
||||
|
||||
//等待客户端连接(telnet)
|
||||
SocketChannel socketChannel = serverSocketChannel.accept();
|
||||
//假设从客户端接受8个字节
|
||||
int messageLenght = 8;
|
||||
//循环读取
|
||||
while (true) {
|
||||
int byteRead = 0;
|
||||
while (byteRead < messageLenght) {
|
||||
long read = socketChannel.read(byteBuffers);
|
||||
//累计读取的字节数
|
||||
byteRead += read;
|
||||
System.out.println("byteRead=" + byteRead);
|
||||
//使用流打印 当前buffer的postion 和limit
|
||||
Arrays.asList(byteBuffers).stream().map(buffer -> {
|
||||
return "postion=" + buffer.position() + ", limit=" + buffer.limit();
|
||||
}).forEach(System.out::println);
|
||||
}
|
||||
//将所有的buffer进行flip
|
||||
Arrays.asList(byteBuffers).forEach(Buffer::flip);
|
||||
long byteWrite = 0;
|
||||
//将数据读取出到客户端
|
||||
while (byteWrite < messageLenght) {
|
||||
long write = socketChannel.write(byteBuffers);
|
||||
byteWrite += write;
|
||||
}
|
||||
//将所有的buffer 进行clear
|
||||
Arrays.asList(byteBuffers).forEach(Buffer::clear);
|
||||
System.out.println("byteRead:=" + byteRead + " byteWrite" + byteWrite + " messageLenght" + messageLenght);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
9
nettyPro/src/main/resources/1.txt
Normal file
9
nettyPro/src/main/resources/1.txt
Normal file
@ -0,0 +1,9 @@
|
||||
三次握手
|
||||
B-> SYN -> A
|
||||
B-> SYN/ACK ->A
|
||||
B <-ACK<- A
|
||||
四次分手
|
||||
B-> ACK/FIN -> A
|
||||
B<- ACK <- A
|
||||
B<- ACK/FIN <- A
|
||||
B-> ACK -> A
|
20
pom.xml
Normal file
20
pom.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>cn.zyjblogs.netty</groupId>
|
||||
<artifactId>netty_ALL</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.0</version>
|
||||
<modules>
|
||||
<module>nettyPro</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue
Block a user