2025年2月21日17:23:56
commit
6b872416ae
|
|
@ -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
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.zhangmeng</groupId>
|
||||
<artifactId>Tcp-Server</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Tcp-Server</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>21</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.27</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>21</source>
|
||||
<target>21</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue