当前位置:网站首页>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
边栏推荐
- Rhcsa certification exam exercise (configured on the first host)
- Solution of deleting path variable by mistake
- Detailed explanation of express framework
- ES6 Promise 对象
- L2-006 树的遍历 (25 分)
- 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
- AcWing 242. A simple integer problem (tree array + difference)
- What does BSP mean
- SQL time injection
- ES6 promise object
猜你喜欢

Learning question 1:127.0.0.1 refused our visit

AI benchmark V5 ranking

Nanny level problem setting tutorial

QT creator support platform

Request object and response object analysis

Composition des mots (sous - total)

MySQL与c语言连接(vs2019版)

Machine learning -- census data analysis

AcWing 242. A simple integer problem (tree array + difference)
![[yarn] yarn container log cleaning](/img/1d/b48059ae2e6133ea9d9c38d31c4a86.png)
[yarn] yarn container log cleaning
随机推荐
How to set up voice recognition on the computer with shortcut keys
QT creator support platform
Codeforces Round #753 (Div. 3)
Password free login of distributed nodes
Codeforces Round #753 (Div. 3)
L2-001 紧急救援 (25 分)
Aborted connection 1055898 to db:
Base de données Advanced Learning Notes - - SQL statements
ES6 let 和 const 命令
Pytorch基础
【CDH】CDH/CDP 环境修改 cloudera manager默认端口7180
[mrctf2020] dolls
使用lambda在循环中传参时,参数总为同一个值
vs2019 第一个MFC应用程序
QT creator custom build process
C语言读取BMP文件
AcWing 1294.樱花 题解
Codeforces Round #771 (Div. 2)
TypeScript
[MRCTF2020]套娃