|
|
@@ -1,85 +0,0 @@
|
|
|
-/*
|
|
|
-package com.zksy.lamp.server;
|
|
|
-
|
|
|
-import cn.hutool.core.lang.func.VoidFunc0;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import javax.annotation.PreDestroy;
|
|
|
-import java.io.*;
|
|
|
-import java.net.*;
|
|
|
-
|
|
|
-@Component
|
|
|
-public class TcpServer {
|
|
|
- private int type = 3; // 建议使用小写字母开头以符合Java命名规范
|
|
|
- private BufferedReader reader;
|
|
|
- private BufferedWriter writer;
|
|
|
- private ServerSocket server;
|
|
|
- private Socket client;
|
|
|
- private int serverPort = 15678;
|
|
|
-
|
|
|
- private final Object lock = new Object();
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void init(){
|
|
|
- Runnable func0 = this::startServer;
|
|
|
- Thread thread = new Thread(func0);
|
|
|
- thread.start();
|
|
|
- }
|
|
|
-
|
|
|
- @PreDestroy
|
|
|
- public void closeResource(){
|
|
|
- try {
|
|
|
- if (reader != null) reader.close();
|
|
|
- if (writer != null) writer.close();
|
|
|
- if (client != null) client.close();
|
|
|
- if (server != null) server.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- */
|
|
|
-/**
|
|
|
- * 启动服务器并等待客户端连接
|
|
|
- *//*
|
|
|
-
|
|
|
- public void startServer() {
|
|
|
- try {
|
|
|
- System.out.println("本机作为服务端");
|
|
|
- System.out.println("本机端口:" + serverPort);
|
|
|
- server = new ServerSocket(serverPort);
|
|
|
-
|
|
|
- System.out.println("等待客户机的连接");
|
|
|
- client = server.accept();
|
|
|
- System.out.println("连接成功");
|
|
|
-
|
|
|
- String clientAddr = client.getRemoteSocketAddress().toString().substring(1);
|
|
|
- String[] clientAddrs = clientAddr.split(":");
|
|
|
- System.out.println("客户端IP:" + clientAddrs[0] + " 端口:" + clientAddrs[1]);
|
|
|
-
|
|
|
- reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "GB2312"));
|
|
|
- writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "GB2312"));
|
|
|
- synchronized (lock) {
|
|
|
- lock.notify(); // 通知等待的线程
|
|
|
- }
|
|
|
- System.out.println("测试完成!");
|
|
|
-
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- public synchronized BufferedReader getReader() {
|
|
|
- return this.reader;
|
|
|
- }
|
|
|
-
|
|
|
- public synchronized BufferedWriter getWriter() {
|
|
|
- return this.writer;
|
|
|
- }
|
|
|
-
|
|
|
- public void awaitInitialization() throws InterruptedException {
|
|
|
- synchronized (lock) {
|
|
|
- lock.wait(); // 阻塞当前线程,直到有通知
|
|
|
- }
|
|
|
- }
|
|
|
-}*/
|