or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

core-api.mdevent-bus.mdfile-system.mdhttp.mdindex.mdnetworking.mdutilities.md

networking.mddocs/

0

# Networking

1

2

Low-level TCP client/server networking with comprehensive SSL/TLS support, DNS resolution, UDP datagram communication, proxy configurations, and socket-level operations.

3

4

## Capabilities

5

6

### TCP Server Creation

7

8

Create and configure TCP servers for handling socket connections.

9

10

```java { .api }

11

/**

12

* Create a TCP server with default options

13

* @return NetServer instance

14

*/

15

NetServer createNetServer();

16

17

/**

18

* Create a TCP server with custom options

19

* @param options Server configuration options

20

* @return NetServer instance

21

*/

22

NetServer createNetServer(NetServerOptions options);

23

24

/**

25

* TCP Server interface for handling connections

26

*/

27

interface NetServer extends Measured, Closeable {

28

/**

29

* Set connection handler for incoming connections

30

* @param handler Handler for new socket connections

31

* @return this for chaining

32

*/

33

NetServer connectHandler(Handler<NetSocket> handler);

34

35

/**

36

* Start listening on a port

37

* @param port Port to listen on

38

* @return Future that completes when server is listening

39

*/

40

Future<NetServer> listen(int port);

41

42

/**

43

* Start listening on a specific host and port

44

* @param port Port to listen on

45

* @param host Host to bind to

46

* @return Future that completes when server is listening

47

*/

48

Future<NetServer> listen(int port, String host);

49

50

/**

51

* Start listening on a SocketAddress

52

* @param localAddress Address to listen on

53

* @return Future that completes when server is listening

54

*/

55

Future<NetServer> listen(SocketAddress localAddress);

56

57

/**

58

* Get the actual port the server is listening on

59

* @return The port number

60

*/

61

int actualPort();

62

63

/**

64

* Close the server

65

* @return Future that completes when server is closed

66

*/

67

Future<Void> close();

68

69

/**

70

* Update SSL options (for servers with SSL enabled)

71

* @param options New SSL options

72

* @return Future that completes when updated

73

*/

74

Future<Void> updateSSLOptions(SSLOptions options);

75

}

76

```

77

78

### TCP Client Creation

79

80

Create and configure TCP clients for making outbound connections.

81

82

```java { .api }

83

/**

84

* Create a TCP client with default options

85

* @return NetClient instance

86

*/

87

NetClient createNetClient();

88

89

/**

90

* Create a TCP client with custom options

91

* @param options Client configuration options

92

* @return NetClient instance

93

*/

94

NetClient createNetClient(NetClientOptions options);

95

96

/**

97

* TCP Client interface for making connections

98

*/

99

interface NetClient extends Measured, Closeable {

100

/**

101

* Connect to a server

102

* @param port Target port

103

* @param host Target host

104

* @return Future that completes with NetSocket

105

*/

106

Future<NetSocket> connect(int port, String host);

107

108

/**

109

* Connect to a server with connect handler

110

* @param port Target port

111

* @param host Target host

112

* @param connectHandler Handler called when connected

113

* @return this for chaining

114

*/

115

NetClient connect(int port, String host, Handler<AsyncResult<NetSocket>> connectHandler);

116

117

/**

118

* Connect to a SocketAddress

119

* @param remoteAddress Address to connect to

120

* @return Future that completes with NetSocket

121

*/

122

Future<NetSocket> connect(SocketAddress remoteAddress);

123

124

/**

125

* Connect to a SocketAddress from a local address

126

* @param remoteAddress Remote address to connect to

127

* @param localAddress Local address to bind to

128

* @return Future that completes with NetSocket

129

*/

130

Future<NetSocket> connect(SocketAddress remoteAddress, SocketAddress localAddress);

131

132

/**

133

* Close the client

134

* @return Future that completes when client is closed

135

*/

136

Future<Void> close();

137

138

/**

139

* Update SSL options (for clients with SSL enabled)

140

* @param options New SSL options

141

* @return Future that completes when updated

142

*/

143

Future<Void> updateSSLOptions(SSLOptions options);

144

}

145

```

146

147

### Socket Operations

148

149

Low-level socket operations for reading/writing data and managing connections.

150

151

```java { .api }

152

/**

153

* TCP socket connection for data transfer

154

*/

155

interface NetSocket extends ReadStream<Buffer>, WriteStream<Buffer> {

156

/**

157

* Get the write handler ID for event bus integration

158

* @return Write handler ID

159

*/

160

String writeHandlerID();

161

162

/**

163

* Write data to the socket

164

* @param data Data to write

165

* @return Future that completes when written

166

*/

167

Future<Void> write(Buffer data);

168

169

/**

170

* Write string to the socket

171

* @param str String to write

172

* @return Future that completes when written

173

*/

174

Future<Void> write(String str);

175

176

/**

177

* Write string with encoding to the socket

178

* @param str String to write

179

* @param enc Character encoding

180

* @return Future that completes when written

181

*/

182

Future<Void> write(String str, String enc);

183

184

/**

185

* Close the socket

186

* @return Future that completes when closed

187

*/

188

Future<Void> close();

189

190

/**

191

* Set close handler

192

* @param handler Handler called when socket is closed

193

* @return this for chaining

194

*/

195

NetSocket closeHandler(Handler<Void> handler);

196

197

/**

198

* Get remote address

199

* @return Remote socket address

200

*/

201

SocketAddress remoteAddress();

202

203

/**

204

* Get local address

205

* @return Local socket address

206

*/

207

SocketAddress localAddress();

208

209

/**

210

* Check if connection is SSL

211

* @return true if SSL

212

*/

213

boolean isSsl();

214

215

/**

216

* Get SSL session (if SSL)

217

* @return SSL session or null

218

*/

219

SSLSession sslSession();

220

221

/**

222

* Get indicated server name (SNI)

223

* @return Server name or null

224

*/

225

String indicatedServerName();

226

227

/**

228

* Get application layer protocol (ALPN)

229

* @return Protocol name or null

230

*/

231

String applicationLayerProtocol();

232

233

/**

234

* Upgrade connection to SSL

235

* @return Future that completes when upgraded

236

*/

237

Future<Void> upgradeToSsl();

238

239

/**

240

* Upgrade connection to SSL with server name

241

* @param serverName Server name for SNI

242

* @return Future that completes when upgraded

243

*/

244

Future<Void> upgradeToSsl(String serverName);

245

246

/**

247

* Shutdown the socket for writing

248

* @return Future that completes when shutdown

249

*/

250

Future<Void> shutdown();

251

252

/**

253

* Shutdown the socket with timeout

254

* @param timeout Timeout in milliseconds

255

* @return Future that completes when shutdown or timeout

256

*/

257

Future<Void> shutdown(long timeout);

258

}

259

```

260

261

### Socket Address Management

262

263

Network address abstraction for both Internet and domain sockets.

264

265

```java { .api }

266

/**

267

* Network socket address abstraction

268

*/

269

interface SocketAddress {

270

/**

271

* Get the host name

272

* @return Host name

273

*/

274

String host();

275

276

/**

277

* Get the port number

278

* @return Port number

279

*/

280

int port();

281

282

/**

283

* Get the path (for domain sockets)

284

* @return Path string

285

*/

286

String path();

287

288

/**

289

* Check if this is a domain socket address

290

* @return true if domain socket

291

*/

292

boolean isDomainSocket();

293

294

/**

295

* Check if this is an Internet socket address

296

* @return true if Internet socket

297

*/

298

boolean isInetSocket();

299

300

/**

301

* Convert to InetSocketAddress

302

* @return InetSocketAddress

303

*/

304

InetSocketAddress inetSocketAddress();

305

306

/**

307

* Convert to DomainSocketAddress

308

* @return DomainSocketAddress

309

*/

310

DomainSocketAddress domainSocketAddress();

311

312

// Static factory methods

313

static SocketAddress inetSocketAddress(int port, String host);

314

static SocketAddress domainSocketAddress(String path);

315

}

316

```

317

318

### SSL/TLS Configuration

319

320

Comprehensive SSL/TLS support with various certificate and key formats.

321

322

```java { .api }

323

/**

324

* Base SSL configuration options

325

*/

326

class SSLOptions {

327

SSLOptions setSsl(boolean ssl);

328

SSLOptions setKeyCertOptions(KeyCertOptions keyCertOptions);

329

SSLOptions setTrustOptions(TrustOptions trustOptions);

330

SSLOptions addEnabledCipherSuite(String suite);

331

SSLOptions addEnabledSecureTransportProtocol(String protocol);

332

SSLOptions setUseAlpn(boolean useAlpn);

333

SSLOptions setSslHandshakeTimeout(long sslHandshakeTimeout);

334

SSLOptions setSslHandshakeTimeoutUnit(TimeUnit sslHandshakeTimeoutUnit);

335

}

336

337

/**

338

* Interface for key certificate options

339

*/

340

interface KeyCertOptions extends ClusterSerializable {

341

KeyCertOptions copy();

342

KeyManagerFactory getKeyManagerFactory(Vertx vertx);

343

Function<String, X509Certificate[]> certificateChainFunction(Vertx vertx);

344

Function<String, PrivateKey> privateKeyFunction(Vertx vertx);

345

}

346

347

/**

348

* Interface for trust options

349

*/

350

interface TrustOptions extends ClusterSerializable {

351

TrustOptions copy();

352

TrustManagerFactory getTrustManagerFactory(Vertx vertx);

353

Function<String, X509Certificate[]> certificateChainFunction(Vertx vertx);

354

}

355

356

/**

357

* Java KeyStore options

358

*/

359

class JksOptions implements KeyCertOptions, TrustOptions {

360

JksOptions setPath(String path);

361

JksOptions setPassword(String password);

362

JksOptions setValue(Buffer value);

363

}

364

365

/**

366

* PKCS#12 options

367

*/

368

class PfxOptions implements KeyCertOptions, TrustOptions {

369

PfxOptions setPath(String path);

370

PfxOptions setPassword(String password);

371

PfxOptions setValue(Buffer value);

372

}

373

374

/**

375

* PEM format key certificate options

376

*/

377

class PemKeyCertOptions implements KeyCertOptions {

378

PemKeyCertOptions setCertPath(String certPath);

379

PemKeyCertOptions setCertPaths(List<String> certPaths);

380

PemKeyCertOptions setCertValues(List<Buffer> certValues);

381

PemKeyCertOptions setKeyPath(String keyPath);

382

PemKeyCertOptions setKeyPaths(List<String> keyPaths);

383

PemKeyCertOptions setKeyValues(List<Buffer> keyValues);

384

}

385

386

/**

387

* PEM format trust options

388

*/

389

class PemTrustOptions implements TrustOptions {

390

PemTrustOptions setCertPaths(List<String> certPaths);

391

PemTrustOptions setCertValues(List<Buffer> certValues);

392

}

393

```

394

395

### Proxy Support

396

397

HTTP and SOCKS proxy configuration for client connections.

398

399

```java { .api }

400

/**

401

* Proxy configuration options

402

*/

403

class ProxyOptions {

404

ProxyOptions setType(ProxyType type);

405

ProxyOptions setHost(String host);

406

ProxyOptions setPort(int port);

407

ProxyOptions setUsername(String username);

408

ProxyOptions setPassword(String password);

409

}

410

411

/**

412

* Types of proxy servers

413

*/

414

enum ProxyType {

415

HTTP, // HTTP proxy

416

SOCKS4, // SOCKS4 proxy

417

SOCKS5 // SOCKS5 proxy

418

}

419

```

420

421

### Configuration Options

422

423

Network configuration options for servers and clients.

424

425

```java { .api }

426

/**

427

* TCP Server configuration options

428

*/

429

class NetServerOptions extends TCPSSLOptions {

430

NetServerOptions setPort(int port);

431

NetServerOptions setHost(String host);

432

NetServerOptions setBacklog(int backlog);

433

NetServerOptions setRegisterWriteHandler(boolean registerWriteHandler);

434

NetServerOptions setClientAuth(ClientAuth clientAuth);

435

NetServerOptions setSni(boolean sni);

436

NetServerOptions setUseProxyProtocol(boolean useProxyProtocol);

437

NetServerOptions setProxyProtocolTimeout(long proxyProtocolTimeout);

438

NetServerOptions setProxyProtocolTimeoutUnit(TimeUnit proxyProtocolTimeoutUnit);

439

}

440

441

/**

442

* TCP Client configuration options

443

*/

444

class NetClientOptions extends TCPSSLOptions {

445

NetClientOptions setReconnectAttempts(int attempts);

446

NetClientOptions setReconnectInterval(long interval);

447

NetClientOptions setHostnameVerificationAlgorithm(String hostnameVerificationAlgorithm);

448

NetClientOptions addApplicationLayerProtocol(String protocol);

449

NetClientOptions setApplicationLayerProtocols(List<String> protocols);

450

NetClientOptions setRegisterWriteHandler(boolean registerWriteHandler);

451

NetClientOptions setNonProxyHosts(List<String> nonProxyHosts);

452

NetClientOptions setProxyOptions(ProxyOptions proxyOptions);

453

}

454

455

/**

456

* Base TCP and SSL options

457

*/

458

class TCPSSLOptions extends NetworkOptions {

459

TCPSSLOptions setTcpNoDelay(boolean tcpNoDelay);

460

TCPSSLOptions setTcpKeepAlive(boolean tcpKeepAlive);

461

TCPSSLOptions setSoLinger(int soLinger);

462

TCPSSLOptions setIdleTimeout(int idleTimeout);

463

TCPSSLOptions setIdleTimeoutUnit(TimeUnit idleTimeoutUnit);

464

TCPSSLOptions setReadIdleTimeout(int idleTimeout);

465

TCPSSLOptions setWriteIdleTimeout(int idleTimeout);

466

TCPSSLOptions setSsl(boolean ssl);

467

TCPSSLOptions setKeyCertOptions(KeyCertOptions options);

468

TCPSSLOptions setTrustOptions(TrustOptions options);

469

TCPSSLOptions addEnabledCipherSuite(String suite);

470

TCPSSLOptions addEnabledSecureTransportProtocol(String protocol);

471

TCPSSLOptions setUseAlpn(boolean useAlpn);

472

TCPSSLOptions setSslHandshakeTimeout(long timeout);

473

TCPSSLOptions setSslHandshakeTimeoutUnit(TimeUnit timeoutUnit);

474

}

475

476

/**

477

* Base network options

478

*/

479

class NetworkOptions {

480

NetworkOptions setSendBufferSize(int sendBufferSize);

481

NetworkOptions setReceiveBufferSize(int receiveBufferSize);

482

NetworkOptions setReuseAddress(boolean reuseAddress);

483

NetworkOptions setReusePort(boolean reusePort);

484

NetworkOptions setTrafficClass(int trafficClass);

485

NetworkOptions setTcpNoDelay(boolean tcpNoDelay);

486

NetworkOptions setTcpKeepAlive(boolean tcpKeepAlive);

487

NetworkOptions setSoLinger(int soLinger);

488

NetworkOptions setTcpUserTimeout(int tcpUserTimeout);

489

NetworkOptions setTcpFastOpen(boolean tcpFastOpen);

490

NetworkOptions setTcpCork(boolean tcpCork);

491

NetworkOptions setTcpQuickAck(boolean tcpQuickAck);

492

}

493

494

/**

495

* Client authentication modes for SSL

496

*/

497

enum ClientAuth {

498

NONE, // No client authentication

499

REQUEST, // Request client authentication

500

REQUIRED // Require client authentication

501

}

502

```

503

504

## Usage Examples

505

506

**Basic TCP Server:**

507

508

```java

509

import io.vertx.core.Vertx;

510

import io.vertx.core.net.NetServer;

511

import io.vertx.core.net.NetSocket;

512

513

Vertx vertx = Vertx.vertx();

514

NetServer server = vertx.createNetServer();

515

516

server.connectHandler(socket -> {

517

System.out.println("Client connected: " + socket.remoteAddress());

518

519

socket.handler(buffer -> {

520

System.out.println("Received: " + buffer.toString());

521

// Echo back to client

522

socket.write("Echo: " + buffer.toString());

523

});

524

525

socket.closeHandler(v -> {

526

System.out.println("Client disconnected");

527

});

528

});

529

530

server.listen(8080).onSuccess(server -> {

531

System.out.println("TCP server started on port 8080");

532

});

533

```

534

535

**TCP Client Connection:**

536

537

```java

538

import io.vertx.core.net.NetClient;

539

540

NetClient client = vertx.createNetClient();

541

542

client.connect(8080, "localhost").onSuccess(socket -> {

543

System.out.println("Connected to server");

544

545

// Send data

546

socket.write("Hello Server!");

547

548

// Handle response

549

socket.handler(buffer -> {

550

System.out.println("Server response: " + buffer.toString());

551

});

552

553

socket.closeHandler(v -> {

554

System.out.println("Connection closed");

555

});

556

}).onFailure(err -> {

557

System.err.println("Failed to connect: " + err.getMessage());

558

});

559

```

560

561

**SSL Server Configuration:**

562

563

```java

564

import io.vertx.core.net.NetServerOptions;

565

import io.vertx.core.net.PemKeyCertOptions;

566

567

NetServerOptions options = new NetServerOptions()

568

.setSsl(true)

569

.setKeyCertOptions(new PemKeyCertOptions()

570

.setKeyPath("server-key.pem")

571

.setCertPath("server-cert.pem"))

572

.setPort(8443);

573

574

NetServer server = vertx.createNetServer(options);

575

576

server.connectHandler(socket -> {

577

System.out.println("SSL client connected: " + socket.remoteAddress());

578

System.out.println("SSL session: " + socket.sslSession());

579

});

580

581

server.listen();

582

```

583

584

**Client with Proxy:**

585

586

```java

587

import io.vertx.core.net.NetClientOptions;

588

import io.vertx.core.net.ProxyOptions;

589

import io.vertx.core.net.ProxyType;

590

591

NetClientOptions options = new NetClientOptions()

592

.setProxyOptions(new ProxyOptions()

593

.setType(ProxyType.HTTP)

594

.setHost("proxy.example.com")

595

.setPort(8080)

596

.setUsername("user")

597

.setPassword("pass"));

598

599

NetClient client = vertx.createNetClient(options);

600

601

client.connect(80, "example.com").onSuccess(socket -> {

602

System.out.println("Connected through proxy");

603

socket.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n");

604

});

605

```

606

607

### DNS Client

608

609

Asynchronous DNS resolution for various record types with custom DNS server support.

610

611

```java { .api }

612

/**

613

* Create a DNS client with default options (uses system DNS configuration)

614

* @return DnsClient instance

615

*/

616

DnsClient createDnsClient();

617

618

/**

619

* Create a DNS client connecting to a specific DNS server

620

* @param port DNS server port

621

* @param host DNS server host address

622

* @return DnsClient instance

623

*/

624

DnsClient createDnsClient(int port, String host);

625

626

/**

627

* Create a DNS client with custom options

628

* @param options DNS client configuration

629

* @return DnsClient instance

630

*/

631

DnsClient createDnsClient(DnsClientOptions options);

632

633

/**

634

* DNS Client interface for asynchronous DNS operations

635

*/

636

interface DnsClient {

637

// Basic address resolution

638

Future<String> lookup(String name);

639

Future<String> lookup4(String name);

640

Future<String> lookup6(String name);

641

642

// Multi-record resolution

643

Future<List<String>> resolveA(String name);

644

Future<List<String>> resolveAAAA(String name);

645

Future<List<String>> resolveCNAME(String name);

646

Future<List<String>> resolveTXT(String name);

647

Future<List<String>> resolveNS(String name);

648

Future<String> resolvePTR(String name);

649

650

// Structured record types

651

Future<List<MxRecord>> resolveMX(String name);

652

Future<List<SrvRecord>> resolveSRV(String name);

653

654

// Reverse DNS lookup

655

Future<String> reverseLookup(String ipaddress);

656

657

// Client management

658

Future<Void> close();

659

}

660

661

/**

662

* Mail Exchange record

663

*/

664

interface MxRecord {

665

int priority();

666

String name();

667

}

668

669

/**

670

* Service record

671

*/

672

interface SrvRecord {

673

int priority();

674

int weight();

675

int port();

676

String name();

677

String protocol();

678

String service();

679

String target();

680

}

681

682

/**

683

* DNS client configuration options

684

*/

685

class DnsClientOptions {

686

DnsClientOptions setPort(int port);

687

DnsClientOptions setHost(String host);

688

DnsClientOptions setQueryTimeout(long queryTimeout);

689

DnsClientOptions setLogActivity(boolean logActivity);

690

DnsClientOptions setRecursionDesired(boolean recursionDesired);

691

}

692

```

693

694

### UDP/Datagram Sockets

695

696

UDP socket support for connectionless packet-based communication.

697

698

```java { .api }

699

/**

700

* Create a datagram socket with default options

701

* @return DatagramSocket instance

702

*/

703

DatagramSocket createDatagramSocket();

704

705

/**

706

* Create a datagram socket with custom options

707

* @param options Datagram socket configuration

708

* @return DatagramSocket instance

709

*/

710

DatagramSocket createDatagramSocket(DatagramSocketOptions options);

711

712

/**

713

* Datagram Socket interface for UDP communication

714

*/

715

interface DatagramSocket extends Measured, Closeable {

716

/**

717

* Send a packet to a remote address

718

* @param packet Buffer containing packet data

719

* @param port Destination port

720

* @param host Destination host

721

* @return Future that completes when packet is sent

722

*/

723

Future<DatagramSocket> send(Buffer packet, int port, String host);

724

725

/**

726

* Send a packet to a socket address

727

* @param packet Buffer containing packet data

728

* @param recipient Destination socket address

729

* @return Future that completes when packet is sent

730

*/

731

Future<DatagramSocket> send(Buffer packet, SocketAddress recipient);

732

733

/**

734

* Send a string as packet data

735

* @param str String to send

736

* @param port Destination port

737

* @param host Destination host

738

* @return Future that completes when packet is sent

739

*/

740

Future<DatagramSocket> send(String str, int port, String host);

741

742

/**

743

* Send a string to a socket address

744

* @param str String to send

745

* @param recipient Destination socket address

746

* @return Future that completes when packet is sent

747

*/

748

Future<DatagramSocket> send(String str, SocketAddress recipient);

749

750

/**

751

* Bind socket to listen on a port

752

* @param port Port to bind to

753

* @param host Host to bind to

754

* @return Future that completes when socket is bound

755

*/

756

Future<DatagramSocket> listen(int port, String host);

757

758

/**

759

* Set handler for incoming packets

760

* @param handler Handler for received packets

761

* @return this for chaining

762

*/

763

DatagramSocket handler(Handler<DatagramPacket> handler);

764

765

/**

766

* Set exception handler

767

* @param handler Exception handler

768

* @return this for chaining

769

*/

770

DatagramSocket exceptionHandler(Handler<Throwable> handler);

771

772

/**

773

* Get local socket address

774

* @return Local socket address or null if not bound

775

*/

776

SocketAddress localAddress();

777

778

/**

779

* Close the socket

780

* @return Future that completes when socket is closed

781

*/

782

Future<Void> close();

783

}

784

785

/**

786

* Represents a datagram packet

787

*/

788

interface DatagramPacket {

789

/**

790

* Get sender address

791

* @return Sender socket address

792

*/

793

SocketAddress sender();

794

795

/**

796

* Get packet data

797

* @return Buffer containing packet data

798

*/

799

Buffer data();

800

}

801

802

/**

803

* Datagram socket configuration options

804

*/

805

class DatagramSocketOptions {

806

DatagramSocketOptions setSendBufferSize(int sendBufferSize);

807

DatagramSocketOptions setReceiveBufferSize(int receiveBufferSize);

808

DatagramSocketOptions setReuseAddress(boolean reuseAddress);

809

DatagramSocketOptions setReusePort(boolean reusePort);

810

DatagramSocketOptions setBroadcast(boolean broadcast);

811

DatagramSocketOptions setMulticastNetworkInterface(String iface);

812

DatagramSocketOptions setMulticastTimeToLive(int ttl);

813

DatagramSocketOptions setLogActivity(boolean logActivity);

814

}

815

```

816

817

## Usage Examples

818

819

**Basic DNS Resolution:**

820

821

```java

822

import io.vertx.core.dns.DnsClient;

823

824

DnsClient client = vertx.createDnsClient();

825

826

client.lookup("example.com").onComplete(ar -> {

827

if (ar.succeeded()) {

828

String ip = ar.result();

829

System.out.println("Resolved IP: " + ip);

830

} else {

831

System.err.println("DNS lookup failed: " + ar.cause());

832

}

833

});

834

```

835

836

**Custom DNS Server:**

837

838

```java

839

import io.vertx.core.dns.DnsClientOptions;

840

841

DnsClient client = vertx.createDnsClient(new DnsClientOptions()

842

.setHost("8.8.8.8")

843

.setPort(53)

844

.setQueryTimeout(10000));

845

846

client.resolveMX("example.com").onSuccess(mxRecords -> {

847

for (MxRecord mx : mxRecords) {

848

System.out.println("MX: " + mx.name() + " (priority: " + mx.priority() + ")");

849

}

850

});

851

```

852

853

**UDP Socket Communication:**

854

855

```java

856

import io.vertx.core.datagram.DatagramSocket;

857

import io.vertx.core.datagram.DatagramSocketOptions;

858

859

// Create UDP server

860

DatagramSocket server = vertx.createDatagramSocket(new DatagramSocketOptions()

861

.setReuseAddress(true));

862

863

server.handler(packet -> {

864

System.out.println("Received from " + packet.sender() + ": " + packet.data().toString());

865

866

// Echo back to sender

867

server.send(packet.data(), packet.sender());

868

});

869

870

server.listen(1234, "localhost").onSuccess(socket -> {

871

System.out.println("UDP server listening on " + socket.localAddress());

872

});

873

874

// Create UDP client

875

DatagramSocket client = vertx.createDatagramSocket();

876

877

client.send("Hello UDP!", 1234, "localhost").onSuccess(v -> {

878

System.out.println("UDP packet sent");

879

});

880

```