最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot實現(xiàn)TCP連接并進(jìn)行數(shù)據(jù)互傳的方法

 更新時間:2025年01月10日 14:56:38   作者:liaozk_c  
本文詳細(xì)介紹了微服務(wù)架構(gòu)中的翻譯組件使用場景,以及多種開源翻譯組件的解決方案,文中分析了國內(nèi)外多個翻譯服務(wù)如百度翻譯、谷歌翻譯等,以及如何在微服務(wù)項目中集成這些翻譯組件,感興趣的朋友跟隨小編一起看看吧

application.yml

tcp:
  server:
    ip: 192.168.173.25
    port: 20140
server:
  port: 6000

TcpDataSenderController

import com.example.tcpclient.utils.TcpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * tcp接口
 */
@RestController
@RequestMapping("/tcp")
public class TcpDataSenderController {
    private final TcpClientUtil tcpClientUtil;
    @Autowired
    public TcpDataSenderController(TcpClientUtil tcpClientUtil) {
        this.tcpClientUtil = tcpClientUtil;
    }
    /**
     * 連接
     * @return
     */
    @GetMapping("/connect")
    public boolean connect() {
        return tcpClientUtil.connect();
    }
    /**
     * 斷開連接
     * @return
     */
    @GetMapping("/disconnect")
    public boolean disconnect() {
        return tcpClientUtil.disconnect();
    }
    /**
     * 發(fā)送數(shù)據(jù)
     * @param data
     * @return
     */
    @GetMapping("/send")
    public boolean send(@RequestParam("data") String data) {
        return tcpClientUtil.sendData(data);
    }
    /**
     * 發(fā)送數(shù)據(jù)(有返回數(shù)據(jù))
     * @param data
     * @return
     */
    @GetMapping("/sendData")
    public String sendDataToServer(@RequestParam("data") String data) {
        return tcpClientUtil.sendDataToServer(data);
    }
    /**
     * 獲取連接狀態(tài)
     * @return
     */
    @GetMapping("/status")
    public boolean status() {
        return tcpClientUtil.isConnected();
    }
}

TcpClientUtil

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@Component
public class TcpClientUtil {
    private static final Logger logger = LoggerFactory.getLogger(TcpClientUtil.class);
    @Value("${tcp.server.ip}")
    private String tcpServerIp;
    @Value("${tcp.server.port}")
    private int tcpServerPort;
    private Socket socket;
    private OutputStream outputStream;
    private Lock lock = new ReentrantLock();
    private boolean isConnected = false;
    public boolean connect() {
        lock.lock();
        try {
            if (!isConnected) {
                socket = new Socket(tcpServerIp, tcpServerPort);
                outputStream = socket.getOutputStream();
                isConnected = true;
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            lock.unlock();
        }
    }
    public boolean disconnect() {
        lock.lock();
        try {
            if (isConnected) {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                isConnected = false;
                return true;
            }
            return false;
        } finally {
            lock.unlock();
        }
    }
    public boolean sendData(String data) {
        lock.lock();
        try {
            if (isConnected) {
                byte[] bytes = data.getBytes();
                outputStream.write(bytes);
                outputStream.flush();
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            lock.unlock();
        }
    }
    public String sendDataToServer(String data) {
        lock.lock();
        try {
            if (isConnected) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // 發(fā)送數(shù)據(jù)到TCP服務(wù)器
                outputStream.write(data.getBytes(StandardCharsets.UTF_8));
                outputStream.flush();
                // 讀取服務(wù)器返回的數(shù)據(jù)(如果需要處理返回內(nèi)容的話)
                String response = reader.readLine();
                if (response!= null) {
                    logger.info("從TCP服務(wù)器接收到的響應(yīng): {}", response);
                }
                return response;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            lock.unlock();
        }
        return null;
    }
    public boolean isConnected() {
        return isConnected;
    }
}

到此這篇關(guān)于SpringBoot實現(xiàn)TCP連接并進(jìn)行數(shù)據(jù)互傳的文章就介紹到這了,更多相關(guān)Docker翻譯組件Deepl使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決mysql字符串類型的數(shù)字排序出錯:cast(year as signed)

    解決mysql字符串類型的數(shù)字排序出錯:cast(year as signed)

    這篇文章主要介紹了解決mysql字符串類型的數(shù)字排序出錯問題 :cast(year as signed),如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Spring?Security入門到深入實戰(zhàn)步驟詳解

    Spring?Security入門到深入實戰(zhàn)步驟詳解

    小明通過學(xué)習(xí)SpringSecurity,成功為自己的攝影網(wǎng)站添加了完整的登錄認(rèn)證鑒權(quán)功能,包括自定義用戶存儲、權(quán)限控制、自定義登錄頁、JWT集成以及OAuth2第三方登錄,并且對SpringSecurity有了深入的理解和認(rèn)識,喜歡的朋友跟隨小編一起學(xué)習(xí)吧
    2025-11-11
  • Spring中使用JSR303請求約束判空的實現(xiàn)

    Spring中使用JSR303請求約束判空的實現(xiàn)

    這篇文章主要介紹了Spring中使用JSR303請求約束判空的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • MyBatis 引入映射器的方法

    MyBatis 引入映射器的方法

    本文通過實例代碼給大家分享mybatis 引入映射器的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-09-09
  • Hadoop環(huán)境配置之hive環(huán)境配置詳解

    Hadoop環(huán)境配置之hive環(huán)境配置詳解

    這篇文章主要介紹了Hadoop環(huán)境配置之hive環(huán)境配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • SpringBoot設(shè)置動態(tài)定時任務(wù)的方法詳解

    SpringBoot設(shè)置動態(tài)定時任務(wù)的方法詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot設(shè)置動態(tài)定時任務(wù)的方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)有一定的參考價值,需要的可以參考一下
    2022-06-06
  • 一文詳解Java Netty中的Constant類

    一文詳解Java Netty中的Constant類

    這篇文章主要介紹了Constants類即常量類是將一些常用的變量集合到一個地方的類,文中有詳細(xì)的代碼示例,感興趣的同學(xué)可以參考一下
    2023-05-05
  • springboot之如何獲取請求ip方法

    springboot之如何獲取請求ip方法

    這篇文章主要介紹了springboot之如何獲取請求ip方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • java判斷ftp目錄是否存在的方法

    java判斷ftp目錄是否存在的方法

    這篇文章主要為大家詳細(xì)介紹了java判斷ftp目錄是否存在的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Java swing實現(xiàn)的計算器功能完整實例

    Java swing實現(xiàn)的計算器功能完整實例

    這篇文章主要介紹了Java swing實現(xiàn)的計算器功能,結(jié)合完整實例形式分析了java基于swing組件實現(xiàn)計算器布局與運算功能的具體操作技巧,需要的朋友可以參考下
    2017-12-12

最新評論

桂东县| 鄂尔多斯市| 新安县| 光泽县| 保德县| 白山市| 新乐市| 化德县| 连州市| 萝北县| 元阳县| 松原市| 阿合奇县| 余干县| 广灵县| 肃北| 嫩江县| 蒲城县| 洛川县| 高淳县| 南昌县| 崇文区| 德兴市| 佛山市| 扎鲁特旗| 乐亭县| 八宿县| 青神县| 察雅县| 乐亭县| 宁都县| 长子县| 娄烦县| 水富县| 莱阳市| 文成县| 北流市| 庐江县| 尚志市| 莲花县| 滨州市|