-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddcc9be
commit 6309d0f
Showing
4 changed files
with
453 additions
and
15 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
zyt-rpc-consumer/src/main/java/consumer/bootstrap/NIOConsumerBootStrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
package consumer.bootstrap; | ||
|
||
import consumer.nio.NIOClient; | ||
|
||
import java.io.IOException; | ||
|
||
/* | ||
以nio为网络编程框架的消费者端启动类 | ||
*/ | ||
public class NIOConsumerBootStrap { | ||
public static void main(String[] args) throws IOException { | ||
NIOClient.start("127.0.0.1",6666); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
zyt-rpc-consumer/src/main/java/consumer/nio/NIOClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,79 @@ | ||
package consumer.nio; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.SelectionKey; | ||
import java.nio.channels.Selector; | ||
import java.nio.channels.SocketChannel; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Iterator; | ||
import java.util.Scanner; | ||
|
||
public class NIOClient { | ||
public static void start(String HostName, int PORT) throws IOException{ | ||
start0(HostName,PORT); | ||
} | ||
|
||
//真正启动在这 | ||
private static void start0(String hostName, int port) throws IOException { | ||
//得到一个网络通道 | ||
SocketChannel socketChannel = SocketChannel.open(); | ||
System.out.println("-----------服务消费方启动-------------"); | ||
socketChannel.configureBlocking(false); | ||
//建立链接 非阻塞连接 但我们是要等他连接上 | ||
if (!socketChannel.connect(new InetSocketAddress(hostName,port))) { | ||
while (!socketChannel.finishConnect()); | ||
} | ||
//创建选择器 进行监听读事件 | ||
Selector selector = Selector.open(); | ||
socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024)); | ||
//创建匿名线程进行监听读事件 | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
while (true) | ||
{ | ||
//捕获异常 监听读事件 | ||
try { | ||
if (selector.select(1000)==0) | ||
{ | ||
continue; | ||
} | ||
Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator(); | ||
while (keyIterator.hasNext()) | ||
{ | ||
SelectionKey key = keyIterator.next(); | ||
ByteBuffer buffer = (ByteBuffer)key.attachment(); | ||
SocketChannel channel = (SocketChannel)key.channel(); | ||
int read = 1; | ||
//用这个的原因是怕 多线程出现影响 | ||
StringBuffer stringBuffer = new StringBuffer(); | ||
while (read!=0) | ||
{ | ||
buffer.clear(); | ||
read = channel.read(buffer); | ||
stringBuffer.append(new String(buffer.array(),0,read)); | ||
} | ||
System.out.println("收到服务端回信"+stringBuffer.toString()); | ||
keyIterator.remove(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}).start(); | ||
|
||
//真正的业务逻辑 等待键盘上的输入 进行发送信息 | ||
Scanner scanner = new Scanner(System.in); | ||
while (true) | ||
{ | ||
int methodNum = scanner.nextInt(); | ||
String message = scanner.next(); | ||
String msg = new String(methodNum+"#"+message); | ||
socketChannel.write(ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8))); | ||
System.out.println("消息发送"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.