当前位置:网站首页>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
边栏推荐
- How to set up voice recognition on the computer with shortcut keys
- Kept VRRP script, preemptive delay, VIP unicast details
- Codeforces Round #753 (Div. 3)
- L2-006 tree traversal (25 points)
- ES6 let and const commands
- 小L的试卷
- Software I2C based on Hal Library
- Machine learning notes week02 convolutional neural network
- Double to int precision loss
- 【flink】flink学习
猜你喜欢
随机推荐
JS array + array method reconstruction
Heating data in data lake?
[蓝桥杯2017初赛]包子凑数
【yarn】CDP集群 Yarn配置capacity调度器批量分配
[Blue Bridge Cup 2017 preliminary] grid division
ImportError: libmysqlclient. so. 20: Cannot open shared object file: no such file or directory solution
2019 Tencent summer intern formal written examination
Detailed explanation of nodejs
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
2020 WANGDING cup_ Rosefinch formation_ Web_ nmap
[Bluebridge cup 2021 preliminary] weight weighing
Word排版(小计)
数据库面试常问的一些概念
[AGC009D]Uninity
Machine learning notes week02 convolutional neural network
DICOM: Overview
yarn安装与使用
AcWing 242. A simple integer problem (tree array + difference)
SQL时间注入
UDS learning notes on fault codes (0x19 and 0x14 services)







![[Blue Bridge Cup 2017 preliminary] grid division](/img/e9/e49556d0867840148a60ff4906f78e.png)
