当前位置:网站首页>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
边栏推荐
- Machine learning notes week02 convolutional neural network
- Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
- Codeforces Round #753 (Div. 3)
- Learn winpwn (2) -- GS protection from scratch
- nodejs 详解
- 使用lambda在循环中传参时,参数总为同一个值
- [Flink] Flink learning
- 库函数--(持续更新)
- Learn winpwn (3) -- sEH from scratch
- QT creator uses Valgrind code analysis tool
猜你喜欢
随机推荐
How to configure flymcu (STM32 serial port download software) is shown in super detail
Word排版(小计)
[蓝桥杯2021初赛] 砝码称重
解决安装Failed building wheel for pillow
Come and walk into the JVM
Project practice - background employee information management (add, delete, modify, check, login and exit)
Library function -- (continuous update)
Mtcnn face detection
Pytoch Foundation
Word typesetting (subtotal)
使用LinkedHashMap实现一个LRU算法的缓存
JS array + array method reconstruction
vs2019 桌面程序快速入门
nodejs连接Mysql
MySQL主从复制的原理以及实现
Double to int precision loss
[BSidesCF_2020]Had_a_bad_day
PHP - whether the setting error displays -php xxx When PHP executes, there is no code exception prompt
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
QT creator support platform