java?NIO實(shí)現(xiàn)簡單聊天程序
本文實(shí)例為大家分享了java NIO實(shí)現(xiàn)簡單聊天程序的具體代碼,供大家參考,具體內(nèi)容如下
服務(wù)端
功能:
1、接受客戶端連接
2、發(fā)送消息
3、讀取客戶端消息
Server.java
public class Server {
private Selector selector;
private ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
private ByteBuffer readBuffer = ByteBuffer.allocate(1024);
Msg msg = new Msg();
MsgSender msgSender = new MsgSender(msg);
public static void main(String[] args) {
Server server = new Server();
new Thread(server.msgSender).start();
server.start();
}
//初始化服務(wù)端
public Server() {
try {
this.selector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ssc.bind(new InetSocketAddress(8899));
ssc.register(this.selector, SelectionKey.OP_ACCEPT);
System.out.println("服務(wù)端初始化完畢。。。。");
} catch (IOException e) {
e.printStackTrace();
}
}
//啟動服務(wù)端等待selector事件
public void start() {
while (true) {
try {
int num = this.selector.select();
if (num > 0) {
Iterator<SelectionKey> it = this.selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
it.remove();
if (key.isValid()) {
if (key.isAcceptable()) {
this.accept(key);
}
if (key.isReadable()) {
this.read(key);
}
if (key.isWritable()) {
this.write(key);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//發(fā)送消息
private void write(SelectionKey key) {
SocketChannel sc = (SocketChannel) key.channel();
try {
sc.configureBlocking(false);
} catch (IOException e) {
e.printStackTrace();
}
//判斷是否有消息需要發(fā)送
if(msg!=null && msg.getFlag()){
System.out.println(msg);
try {
msg.setFlag(false);
writeBuffer.clear();
writeBuffer.put(msg.getContent().getBytes());
writeBuffer.flip();
sc.write(writeBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//讀取客戶端消息
private void read(SelectionKey key) {
SocketChannel sc = (SocketChannel) key.channel();
try {
sc.configureBlocking(false);
readBuffer.clear();
int read = sc.read(readBuffer);
if (read == -1) {
sc.close();
key.cancel();
}
readBuffer.flip();
System.out.println(new String(readBuffer.array()));
} catch (IOException e) {
e.printStackTrace();
}
}
//接受客戶端連接
private void accept(SelectionKey key) {
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
try {
SocketChannel sc = ssc.accept();
System.out.println(String.format("a new client join!!!host:%s;port:%d", sc.socket().getLocalAddress(), sc.socket().getPort()));
sc.configureBlocking(false);
sc.register(this.selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Msg.java
class Msg {
private Boolean flag=false;
private String content;
public Boolean getFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Msg{" +
"flag=" + flag +
", content='" + content + '\'' +
'}';
}
}
MsgSender.java
class MsgSender implements Runnable{
private Msg msg;
public MsgSender(Msg msg) {
this.msg = msg;
}
@Override
public void run() {
while (true) {
System.out.println("input:\n");
Scanner scanner = new Scanner(System.in);
this.msg.setContent(scanner.next());
this.msg.setFlag(true);
}
}
}
客戶端
采用的時(shí)BIO,簡單的使用線程實(shí)現(xiàn)消息的讀寫
功能:
1、連接服務(wù)端
2、讀取消息
3、發(fā)送消息
public class Client {
private SocketChannel sc;
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
public static void main(String[] args) {
new Client();
}
public Client() {
try {
sc = SocketChannel.open();
//連接服務(wù)端
sc.connect(new InetSocketAddress(8899));
//發(fā)送消息
this.write(sc);
//讀取消息
this.read(sc);
} catch (IOException e) {
e.printStackTrace();
}
}
private void read(SocketChannel sc) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
readBuffer.clear();
int read = sc.read(readBuffer);
readBuffer.flip();
System.out.println(new String(readBuffer.array()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
private void write(SocketChannel sc) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
writeBuffer.clear();
writeBuffer.put(next.getBytes());
writeBuffer.flip();
try {
sc.write(writeBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring?cloud?gateway中配置uri三種方式
gateway?組件是SpringCloud?組件中的網(wǎng)關(guān)組件,主要是解決路由轉(zhuǎn)發(fā)的問題,跟nginx有點(diǎn)類似,區(qū)別是nginx多用在前端上,gateway用在后端上,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-08-08
深入理解Java中的HashMap的實(shí)現(xiàn)機(jī)制
這篇文章主要介紹了深入理解Java中的HashMap的實(shí)現(xiàn)機(jī)制,同時(shí)也有助于理解Java中對于哈希函數(shù)的相關(guān)處理方式,需要的朋友可以參考下2015-07-07
IDEA 2020.1 for Mac 下載安裝配置及出現(xiàn)的問題小結(jié)
這篇文章主要介紹了IDEA 2020.1 for Mac 下載安裝配置及出現(xiàn)的問題小結(jié),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
SpringBoot 回滾操作的幾種實(shí)現(xiàn)方式
回滾操作是一種常見的操作,用于撤銷之前執(zhí)行的操作,本文主要介紹了SpringBoot回滾操作的幾種實(shí)現(xiàn)方式,包含基于異常類型的回滾、基于自定義邏輯的回滾和基于數(shù)據(jù)庫狀態(tài)的回滾,感興趣的可以了解一下2024-03-03
java日期格式化YYYY-MM-dd遇坑指南小結(jié)
本文主要介紹了java日期格式化YYYY-MM-dd遇坑指南小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

