From 6b872416ae81a43aba216f4ceb278861f47b60e0 Mon Sep 17 00:00:00 2001 From: qmstyle Date: Fri, 21 Feb 2025 17:24:05 +0800 Subject: [PATCH] =?UTF-8?q?2025=E5=B9=B42=E6=9C=8821=E6=97=A517:23:56?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 38 ++++++ pom.xml | 45 +++++++ .../java/com/zhangmeng/TcpApplication.java | 24 ++++ .../com/zhangmeng/handler/ClientHandler.java | 67 ++++++++++ .../java/com/zhangmeng/service/Server.java | 21 ++++ .../zhangmeng/service/impl/ServerImpl.java | 115 ++++++++++++++++++ src/test/java/com/zhangmeng/AppTest.java | 39 ++++++ 7 files changed, 349 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/zhangmeng/TcpApplication.java create mode 100644 src/main/java/com/zhangmeng/handler/ClientHandler.java create mode 100644 src/main/java/com/zhangmeng/service/Server.java create mode 100644 src/main/java/com/zhangmeng/service/impl/ServerImpl.java create mode 100644 src/test/java/com/zhangmeng/AppTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5ed1bec --- /dev/null +++ b/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + + com.zhangmeng + Tcp-Server + 1.0-SNAPSHOT + jar + + Tcp-Server + http://maven.apache.org + + + UTF-8 + 21 + + + + + + cn.hutool + hutool-all + 5.8.27 + + + + junit + junit + 3.8.1 + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 21 + 21 + + + + + diff --git a/src/main/java/com/zhangmeng/TcpApplication.java b/src/main/java/com/zhangmeng/TcpApplication.java new file mode 100644 index 0000000..4772d21 --- /dev/null +++ b/src/main/java/com/zhangmeng/TcpApplication.java @@ -0,0 +1,24 @@ +package com.zhangmeng; + +import com.zhangmeng.service.impl.ServerImpl; + +/** + * Hello world! + * + */ +public class TcpApplication +{ + public static void main( String[] args ) + { + + ServerImpl.Config config = new ServerImpl.Config(); + config.setName("SimpleServer"); + config.setIPVersion("IPv4"); + config.setIP("127.0.0.1"); + config.setPort(8888); + ServerImpl server = new ServerImpl(); + server.setConfig(config); + server.Start(); + + } +} diff --git a/src/main/java/com/zhangmeng/handler/ClientHandler.java b/src/main/java/com/zhangmeng/handler/ClientHandler.java new file mode 100644 index 0000000..3e7d6a0 --- /dev/null +++ b/src/main/java/com/zhangmeng/handler/ClientHandler.java @@ -0,0 +1,67 @@ +package com.zhangmeng.handler; + +import cn.hutool.core.io.BufferUtil; + +import java.io.*; +import java.net.Socket; + +/** + * @author zm + * @date 2025/2/21 15:40 + * @version: 1.0 + */ +public class ClientHandler implements Runnable { + + private Socket socket; + // private BufferedReader reader; +// private BufferedWriter writer; + private OutputStream outputStream; + private InputStream inputStream; + + public void close() { + try { + outputStream.close(); + inputStream.close(); + socket.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void run() { + while (true) { + try { + byte[] bytes = new byte[1024]; + int len = inputStream.read(bytes); + if (len == -1) { + break; + } + String message = new String(bytes, 0, len); + System.out.println("receive message: " + message); + + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + public Socket getSocket() { + return socket; + } + + public void setSocket(Socket socket) { + this.socket = socket; + } + + public ClientHandler(Socket socket) { + this.socket = socket; + try { + this.inputStream = socket.getInputStream(); + this.outputStream = socket.getOutputStream(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/com/zhangmeng/service/Server.java b/src/main/java/com/zhangmeng/service/Server.java new file mode 100644 index 0000000..13c7bee --- /dev/null +++ b/src/main/java/com/zhangmeng/service/Server.java @@ -0,0 +1,21 @@ +package com.zhangmeng.service; + +/** + * @author zm + * @Description: 服务器接口 + * @date 2025/2/21 15:03 + * @version: 1.0 + */ +public interface Server { + + //启动服务器方法 + public void Start(); + + //停止服务器方法 + public void Stop(); + + //开启业务服务方法 + public void Serve(); + + +} diff --git a/src/main/java/com/zhangmeng/service/impl/ServerImpl.java b/src/main/java/com/zhangmeng/service/impl/ServerImpl.java new file mode 100644 index 0000000..59d8a24 --- /dev/null +++ b/src/main/java/com/zhangmeng/service/impl/ServerImpl.java @@ -0,0 +1,115 @@ +package com.zhangmeng.service.impl; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.BufferUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.log.Log; +import cn.hutool.log.LogFactory; +import cn.hutool.log.StaticLog; +import cn.hutool.socket.aio.AioServer; +import cn.hutool.socket.aio.AioSession; +import cn.hutool.socket.aio.SimpleIoAction; +import com.zhangmeng.handler.ClientHandler; +import com.zhangmeng.service.Server; + +import java.io.*; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * @author zm + * @date 2025/2/21 15:04 + * @version: 1.0 + */ +public class ServerImpl implements Server { + + private final Log log = LogFactory.get(); + + private Config config; + + private ServerSocket serverSocket; + + @Override + public void Start() { + log.info("[START] Server listenner at IP: {}, Port {}, is starting", config.getIP(), config.getPort()); + try { + serverSocket = new ServerSocket(config.getPort()); + + while (true) { + Socket accept = serverSocket.accept(); + log.info("[ACCEPT] Accept a client at IP: {}, Port {}", accept.getInetAddress().getHostAddress(), accept.getPort()); + new Thread(new ClientHandler(accept)).start(); + } + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void Stop() { + log.info("[STOP] Server listenner at IP: {}, Port {}, is stoped", config.getIP(), config.getPort()); + try { + serverSocket.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void Serve() { + + } + + public void setConfig(Config config) { + this.config = config; + } + + public static class Config { + //服务器的名称 + private String Name; + + //tcp4 or other + private String IPVersion; + + //服务绑定的IP地址 + private String IP; + //服务绑定的端口 + private int Port; + + public String getName() { + return Name; + } + + public void setName(String name) { + Name = name; + } + + public String getIPVersion() { + return IPVersion; + } + + public void setIPVersion(String IPVersion) { + this.IPVersion = IPVersion; + } + + public String getIP() { + return IP; + } + + public void setIP(String IP) { + this.IP = IP; + } + + public int getPort() { + return Port; + } + + public void setPort(int port) { + Port = port; + } + } +} diff --git a/src/test/java/com/zhangmeng/AppTest.java b/src/test/java/com/zhangmeng/AppTest.java new file mode 100644 index 0000000..be597b1 --- /dev/null +++ b/src/test/java/com/zhangmeng/AppTest.java @@ -0,0 +1,39 @@ +package com.zhangmeng; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +/** + * Unit test for simple App. + */ +public class AppTest { + + public static void main(String[] args) throws IOException { + Scanner scanner = new Scanner(System.in); + System.out.println("等待连接服务端!"); + Socket socket = new Socket("127.0.0.1", 8888); + System.out.println("连接服务端成功!"); + while (true) { + // 给服务端发信息 + System.out.print("请输入:"); + String s = scanner.next(); + if ("out".equals(s)) { + break; + } + OutputStream outputStream = socket.getOutputStream(); + outputStream.write(s.getBytes(StandardCharsets.UTF_8)); + byte[] bytes = new byte[1024]; + + // 读一下服务端发来的信息 + InputStream inputStream = socket.getInputStream(); + int read = inputStream.read(bytes); + System.out.println("服务端:" + new String(bytes, 0, read, Charset.defaultCharset())); + } + } + +}