Compare commits

...

2 Commits

  1. 28
      src/main/java/com/louzin/niodemo/demo.java
  2. 30
      src/main/java/com/louzin/niodemo/nioFileChannelTest.java
  3. 14
      src/main/java/com/louzin/niodemo/scatterandgather/package-info.java
  4. 39
      src/main/java/com/louzin/niodemo/socketnioclienttest/datagramChannel.java
  5. 89
      src/main/java/com/louzin/niodemo/socketnioclienttest/datagramChannel2.java
  6. 28
      src/main/java/com/louzin/niodemo/socketnioclienttest/socketClientDemo.java
  7. 41
      src/main/java/com/louzin/niodemo/socketniotest/ServerSocketChannelDemo.java

28
src/main/java/com/louzin/niodemo/demo.java

@ -0,0 +1,28 @@
package com.louzin.niodemo;
import static java.lang.Thread.sleep;
public class demo{
public static void main(String[] args) throws InterruptedException {
Runnable rb=new Data();
Thread td =new Thread(rb);
td.start();
while(true){
sleep(1000);
System.out.print(3);
}
}
}
class Data implements Runnable {
@Override
public void run() {
while(true) {
try {
sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.print("2");
}
}
}

30
src/main/java/com/louzin/niodemo/nioFileChannelTest.java

@ -0,0 +1,30 @@
package com.louzin.niodemo;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
public class nioFileChannelTest {
@Test
public void testRead() throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile("src/main/resources/mytest.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
//buffer
ByteBuffer allocate = ByteBuffer.allocate(1024);
fileChannel.read(allocate);
System.out.println(allocate.position());
allocate.put("aac".getBytes(StandardCharsets.UTF_8));
System.out.println(allocate.position());
allocate.flip();
while (allocate.hasRemaining())
System.out.print((char) allocate.get());
fileChannel.close();
allocate.clear();
randomAccessFile.close();
}
}

14
src/main/java/com/louzin/niodemo/scatterandgather/package-info.java

@ -0,0 +1,14 @@
package com.louzin.niodemo.scatterandgather;
//scatter 分散:将数据分散到多个buffer中
//ByteBuffer header = ByteBuffer.allocate(128);
//ByteBuffer body=ByteBuffer.allocate(1024);
//ByteBuffer[] byteBuffers={header,body};
//channel.read(byteBuffers);
//
//scatter Reads只适合固定消息,不适合动态消息
//gahter 聚集:将多个buffer的数据发送到同一个channel
//ByteBuffer header = ByteBuffer.allocate(128);
//ByteBuffer body=ByteBuffer.allocate(1024);
//ByteBuffer[] byteBuffers={header,body};
//channel.write(byteBuffers);

39
src/main/java/com/louzin/niodemo/socketnioclienttest/datagramChannel.java

@ -0,0 +1,39 @@
package com.louzin.niodemo.socketnioclienttest;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.StandardCharsets;
//无连接,面向udp,可任意发送和接受数据包
//UDP不存在真正意义上的连接
//以下的连接是向特定服务器read和write接受和发包
public class datagramChannel {
@Test
public void datagramChannelTest() throws IOException {
DatagramChannel open = DatagramChannel.open();
open.bind(new InetSocketAddress("www.baidu.com",10086));
//receive udp data package
ByteBuffer allocate = ByteBuffer.allocate(64);
allocate.clear();
SocketAddress receive = open.receive(allocate);
//发送数据包
//打开同DatagramChannel open
//配置发送内容
ByteBuffer wrap = ByteBuffer
.wrap("client send".getBytes(StandardCharsets.UTF_8));
//发送数据包
open.send(wrap,new InetSocketAddress("",80));
//read和write只有在connect后才能使用,不然NotYetConnectException
//read未接受到包时PortUnreachableException
open.connect(new InetSocketAddress("",10086));
int readSize = open.read(allocate);
open.write(wrap);
}
}

89
src/main/java/com/louzin/niodemo/socketnioclienttest/datagramChannel2.java

@ -0,0 +1,89 @@
package com.louzin.niodemo.socketnioclienttest;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.DatagramChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class datagramChannel2 {
//send
@Test
public void sendDataPackage() throws IOException, InterruptedException {
//get Channel
DatagramChannel open = DatagramChannel.open();
InetSocketAddress l = new InetSocketAddress("localhost", 9999);
open.configureBlocking(false);
//send
while (true){
open.send(ByteBuffer.wrap("UDPUDPUDP".getBytes(StandardCharsets.UTF_8)),l);
System.out.println("发送完成");
Thread.sleep(1000);
}
}
@Test
public void receiveDataPackage() throws IOException {
DatagramChannel open = DatagramChannel.open();
open.bind(new InetSocketAddress("localhost",9999));
//通过buffer读取
ByteBuffer allocate = ByteBuffer.allocate(1024);
while (true){
allocate.clear();
SocketAddress receive = open.receive(allocate);
allocate.flip();
System.out.println(receive.toString());
//发送时含有中文,编码处理
System.out.println(Charset.forName("UTF-8").decode(allocate));
}
}
@Test()
public void readWriteTest() throws IOException {
DatagramChannel open = DatagramChannel.open();
open.bind(new InetSocketAddress(9999));
open.connect(new InetSocketAddress("localhost",9999));
open.write(ByteBuffer.wrap("发送12332111111!!!".getBytes(StandardCharsets.UTF_8)));
ByteBuffer allocate = ByteBuffer.allocate(64);
while (true){
allocate.clear();
open.read(allocate);
allocate.flip();
System.out.println(Charset.forName("UTF-8").decode(allocate));
}
}
@Test
public void writeTest() throws IOException, InterruptedException {
DatagramChannel open = DatagramChannel.open();
open.bind(new InetSocketAddress("localhost",9990));
open.connect(new InetSocketAddress("localhost",9990));
while (true){
open.write(ByteBuffer.wrap("发送!!!".getBytes(StandardCharsets.UTF_8)));
System.out.println("发送成功");
Thread.sleep(1000);
}
}
@Test
public void readTest() throws IOException, InterruptedException {
DatagramChannel open = DatagramChannel.open();
open.connect(new InetSocketAddress("localhost",9990));
ByteBuffer allocate = ByteBuffer.allocate(64);
while (true){
Thread.sleep(500);
System.out.println("等待写入");
allocate.clear();
open.read(allocate);
allocate.flip();
System.out.println(Charset.forName("UTF-8").decode(allocate));
}
}
}

28
src/main/java/com/louzin/niodemo/socketnioclienttest/socketClientDemo.java

@ -0,0 +1,28 @@
package com.louzin.niodemo.socketnioclienttest;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class socketClientDemo {
@Test
public void createChannel() throws IOException {
//创建SocketChannel
SocketChannel open = SocketChannel
.open(new InetSocketAddress(
"www.baidu.com", 80));
//第二种方法
// SocketChannel open1 = SocketChannel.open();
// open1.connect(new InetSocketAddress(
// "www.baidu.com", 80));
//设置阻塞/非阻塞模式
open.configureBlocking(false);
ByteBuffer allocate = ByteBuffer.allocate(16);
open.read(allocate);
open.close();
System.out.println("ReadOver");
}
}

41
src/main/java/com/louzin/niodemo/socketniotest/ServerSocketChannelDemo.java

@ -0,0 +1,41 @@
package com.louzin.niodemo.socketniotest;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
public class ServerSocketChannelDemo {
@Test
public void createPort() throws IOException, InterruptedException {
int port = 8888;
ByteBuffer wrap = ByteBuffer.wrap("hello socket".getBytes(StandardCharsets.UTF_8));
//ServerSocketChannel
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
//ServerSocketChannel未实现绑定,所以需要手动绑定
//得到socket对象,完成对其绑定
serverSocketChannel.socket().bind(new InetSocketAddress(port));
//非阻塞模式运行
serverSocketChannel.configureBlocking(false);
//监听是否有新连接传入
while (true){
//返回值若为null,则无传入
//若为阻塞模式,则会卡到这里直到连接建立
SocketChannel accept = serverSocketChannel.accept();
System.out.println("等待连接!");
if(accept==null){
System.out.println("未发现新连接!");
Thread.sleep(2000);
}else {
System.out.println("发现新连接!!"+accept.socket().getRemoteSocketAddress());
wrap.rewind();//指针0
accept.write(wrap);
accept.close();
}
}
}
}
Loading…
Cancel
Save