当前位置:网站首页>Vert. x: A simple TCP client and server demo
Vert. x: A simple TCP client and server demo
2022-07-06 11:47:00 【You are little KS】
1. Statement
The current content is mainly for recording and learning Vert.x establish Tcp Service and realize communication demo, Current content reference Official documents
2. Server Demo
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.impl.BufferImpl;
public class TcpServer extends AbstractVerticle {
@Override
public void start(Promise<Void> startPromise) throws Exception {
vertx.createNetServer().connectHandler(socket -> {
socket.handler(buffer -> {
String context = buffer.toString("utf-8");
System.out.println("server received some bytes: " + buffer.length()+",context :"+context);
BufferImpl bufferImpl = new BufferImpl();
bufferImpl.appendBytes(" Hello world ......".getBytes());
socket.write(bufferImpl);
});
socket.closeHandler(handler->{
System.out.println("connect close :"+handler);
});
}).exceptionHandler(socket->{
socket.printStackTrace();
}).listen(8888).onSuccess(handler -> {
System.out.println("tcp server start up in port 8888 success!");
});
}
public static void main(String[] args) {
Vertx vertx2 = Vertx.vertx();
vertx2.deployVerticle(new TcpServer());
}
}
3. Client Demo
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.impl.BufferImpl;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetSocket;
public class TcpClient extends AbstractVerticle {
@Override
public void start(Promise<Void> startPromise) throws Exception {
vertx
.createNetClient()
.connect(8888, "localhost")
.onComplete(socket->{
NetSocket result = socket.result();
result.handler(buffer->{
String content = buffer.toString("utf-8");
System.out.println("client received len:"+buffer.length()+",content:"+content);
});
result.closeHandler(handler->{
System.out.println("socket close....");
});
})
.onSuccess(handler->{
System.out.println("connect localhost:8888 tcp server success!");
BufferImpl bufferImpl = new BufferImpl();
bufferImpl.appendBytes(" Hello, server !".getBytes());
handler.write(bufferImpl);
});
}
public static void main(String[] args) {
Vertx vertx2 = Vertx.vertx();
vertx2.deployVerticle(new TcpClient());
}
}
4. test
Turn on the server
Open client
Test success
边栏推荐
- QT creator test
- AcWing 1298.曹冲养猪 题解
- [Bluebridge cup 2021 preliminary] weight weighing
- Base de données Advanced Learning Notes - - SQL statements
- 牛客Novice月赛40
- {one week summary} take you into the ocean of JS knowledge
- 【CDH】CDH5.16 配置 yarn 任务集中分配设置不生效问题
- 【yarn】CDP集群 Yarn配置capacity调度器批量分配
- express框架详解
- Yarn installation and use
猜你喜欢
Vs2019 desktop app quick start
wangeditor富文本引用、表格使用问题
4. Install and deploy spark (spark on Yan mode)
MySQL主从复制的原理以及实现
快来走进JVM吧
Pytorch基础
Case analysis of data inconsistency caused by Pt OSC table change
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
MTCNN人脸检测
[Blue Bridge Cup 2017 preliminary] grid division
随机推荐
4. Install and deploy spark (spark on Yan mode)
[Bluebridge cup 2020 preliminary] horizontal segmentation
C语言读取BMP文件
Solve the problem of installing failed building wheel for pilot
UDS learning notes on fault codes (0x19 and 0x14 services)
How to configure flymcu (STM32 serial port download software) is shown in super detail
Codeforces Round #753 (Div. 3)
MATLAB学习和实战 随手记
{one week summary} take you into the ocean of JS knowledge
Vs2019 use wizard to generate an MFC Application
机器学习笔记-Week02-卷积神经网络
yarn安装与使用
Machine learning -- census data analysis
Word排版(小計)
Nodejs connect mysql
What does usart1 mean
【CDH】CDH/CDP 环境修改 cloudera manager默认端口7180
Codeforces Round #771 (Div. 2)
[蓝桥杯2021初赛] 砝码称重
Codeforces Round #753 (Div. 3)