Socket
Threaded Java socket server and client
Install / Use
/learn @areong/SocketREADME
Socket
Threaded Java socket server and client
Example
Create a server at port 5556 with a MessageHandler, which is the EchoHandler in this example.
SocketServer server = new SocketServer(5556, new EchoHandler());
Create a client connecting to localhost's port 5556.
SocketClient client = new SocketClient(InetAddress.getLocalHost(), 5556);
Send a message from the client.
client.println("Hello!");
Print out the message from the server. The function SocketClient.readLine() blocks.
System.out.println(client.readLine());
The EchoHandler implements the MessageHandler interface and overrides the abstract method onReceive(). The argument connection enables you to send a string back to the client.
public class EchoHandler implements MessageHandler {
@Override
public void onReceive(Connection connection, String message) {
connection.send(message);
}
}
Features
SocketServer is threaded. It creates a thread for accepting connections and creates a new thread each time a new client is connected.
SocketClient is not threaded and the function readLine() blocks.
Related Skills
node-connect
342.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
85.3kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
342.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
342.5kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
