Build Your First smart-socket Application: Step‑by‑Step Environment Setup, Dependencies, and Code

This tutorial walks you through preparing the Java development environment, adding smart-socket Maven or Gradle dependencies, implementing a simple string‑based server and client with AIO, running both components, and observing the expected console output to understand the framework's basic communication workflow.

Three Knives
Three Knives
Three Knives
Build Your First smart-socket Application: Step‑by‑Step Environment Setup, Dependencies, and Code

Chapter Overview

Environment setup and project dependency configuration

Create a simple server and client

Run and test the communication

3.1 Environment Preparation

Before using smart-socket , ensure the following system requirements:

Java version : JDK 8 or higher

Build tool : Maven 3.6+ or Gradle 6+ (Maven is recommended)

IDE : IntelliJ IDEA, Eclipse, or any Java IDE

3.1.2 Project Dependency Configuration

smart-socket provides two main modules:

aio-core : core module offering basic AIO communication functions

aio-pro : extension module for plugins, protocol extensions, and other advanced features

For a Maven project, add the following dependencies:

<dependencies>
    <!-- smart-socket core module -->
    <dependency>
        <groupId>io.github.smartboot.socket</groupId>
        <artifactId>aio-core</artifactId>
        <version>1.7.5</version>
    </dependency>

    <!-- smart-socket extension module (optional) -->
    <dependency>
        <groupId>io.github.smartboot.socket</groupId>
        <artifactId>aio-pro</artifactId>
        <version>1.7.5</version>
    </dependency>
</dependencies>

If you prefer Gradle, use:

dependencies {
    implementation 'io.github.smartboot.socket:aio-core:1.7.5'
    implementation 'io.github.smartboot.socket:aio-pro:1.7.5'
}

3.2 Create the First Server Application

The server receives a string from the client and echoes it back.

3.2.1 Server Code Implementation

import org.smartboot.socket.extension.processor.AbstractMessageProcessor;
import org.smartboot.socket.extension.protocol.StringProtocol;
import org.smartboot.socket.transport.AioQuickServer;
import org.smartboot.socket.transport.AioSession;

public class StringServer {
    public static void main(String[] args) throws Exception {
        // Define message processor
        AbstractMessageProcessor<String> processor = new AbstractMessageProcessor<String>() {
            @Override
            public void process0(AioSession session, String msg) {
                System.out.println("收到客户端消息: " + msg);
                // Echo the message back to the client
                byte[] bytes = msg.getBytes();
                session.writeBuffer().writeInt(bytes.length);
                session.writeBuffer().write(bytes);
                session.writeBuffer().flush();
            }
        };

        // Create server instance
        AioQuickServer<String> server = new AioQuickServer<>(8888, new StringProtocol(), processor);
        // Start the server
        server.start();
        System.out.println("服务端已启动,监听端口: 8888");
    }
}

3.2.2 Code Explanation

Message processor : Implements AbstractMessageProcessor<String> to handle incoming string messages.

Server instance creation : Instantiates AioQuickServer<String> with three arguments – port (8888), protocol ( StringProtocol), and the message processor.

Start service : Calls server.start() to begin listening on the specified port.

3.3 Create the First Client Application

The client connects to the server, sends a message, and prints the response.

3.3.1 Client Code Implementation

import org.smartboot.socket.extension.processor.AbstractMessageProcessor;
import org.smartboot.socket.extension.protocol.StringProtocol;
import org.smartboot.socket.transport.AioQuickClient;
import org.smartboot.socket.transport.AioSession;

public class StringClient {
    public static void main(String[] args) throws Exception {
        // Define message processor
        AbstractMessageProcessor<String> processor = new AbstractMessageProcessor<String>() {
            @Override
            public void process0(AioSession session, String msg) {
                System.out.println("收到服务端响应: " + msg);
            }
        };

        // Create client instance
        AioQuickClient<String> client = new AioQuickClient<>("localhost", 8888, new StringProtocol(), processor);
        // Connect to server
        AioSession session = client.start();
        System.out.println("已连接到服务端");

        // Send message
        byte[] bytes = "Hello, smart-socket!".getBytes();
        session.writeBuffer().writeInt(bytes.length);
        session.writeBuffer().write(bytes);
        session.writeBuffer().flush();
        System.out.println("已发送消息: Hello, smart-socket!");

        // Wait for response
        Thread.sleep(1000);

        // Close connection
        client.shutdown();
        System.out.println("客户端已关闭");
    }
}

3.3.2 Code Explanation

Client instance creation : Uses AioQuickClient<String> with four parameters – server address, port, protocol, and message processor.

Connect to server : Calls client.start() which returns an AioSession for data transmission.

Send message : Writes the length and bytes of the string to the session’s write buffer and flushes it.

3.4 Run and Test

3.4.1 Execution Steps

Start server :

Run StringServer.main.

Console prints: 服务端已启动,监听端口: 8888.

Start client :

Run StringClient.main.

Client connects, sends the message, and receives the echo.

3.4.2 Expected Output

Server console:

服务端已启动,监听端口: 8888
收到客户端消息: Hello, smart-socket!

Client console:

已连接到服务端
已发送消息: Hello, smart-socket!
收到服务端响应: Hello, smart-socket!
客户端已关闭

3.4.3 Execution Flow

The communication proceeds as follows:

Server starts and listens on port 8888.

Client connects to the server.

Client sends the string "Hello, smart-socket!".

Server receives the message, logs it, and writes the same data back.

Client receives the echoed response and logs it.

Client shuts down the connection.

3.5 Understanding the Workflow

The example demonstrates the basic lifecycle of a smart-socket application:

Server startup : Create an AioQuickServer instance and call start().

Client connection : Create an AioQuickClient instance and invoke start() to obtain an AioSession.

Data transmission : Client uses session.writeBuffer() to send bytes.

Protocol parsing : Server’s StringProtocol decodes the incoming byte stream.

Message processing : Server’s MessageProcessor handles the decoded string.

Response : Server writes the response back via the session’s write buffer.

Client reception : Client’s MessageProcessor processes the server’s reply.

Connection closure : Client explicitly shuts down the connection.

3.6 Chapter Summary

By completing this chapter you have learned how to:

Configure the development environment and project dependencies.

Create and launch a smart-socket server.

Create, connect, and communicate with a smart-socket client.

Implement custom protocols and message processors.

Understand the end‑to‑end communication workflow of the framework.

The example showcases smart-socket’s simplicity and ease of use; more complex business logic and protocols can be built on the same foundation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaGradleMavennetwork programmingclient‑serverAIOsmart-socket
Three Knives
Written by

Three Knives

Every line of code you contribute to open source could help make the future better.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.