• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

    java socket中文亂碼

    來源:懂視網(wǎng) 責(zé)編:李贏贏 時(shí)間:2021-12-24 15:43:54
    文檔

    java socket中文亂碼

    可以使用代碼:in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));解決。
    推薦度:
    導(dǎo)讀可以使用代碼:in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));解決。

    java socket中文亂碼怎么解決呢?不知道的小伙伴來看看小編今天的分享吧!

    可以使用代碼:

    in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));

    out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));

    示例:

    解決前:

    import java.io.*;

    import java.net.*; 

    public class SocketServerEx1 {

    public static void main(String args[]) {

    System.out.println("Server");

    ServerSocket serverSocket = null;

    Socket clientSocket = null;

    int connects = 0;

    try {

    serverSocket = new ServerSocket(82, 5); //端口:82,最大鏈接數(shù):5

    //最多連接10次

    while(connects < 10) {

    connects++;

    System.out.println("--------------------等待連接--------------------------");

    clientSocket = serverSocket.accept(); //等待連接

    System.out.println("第 " + connects + " 次連接");

    ServiceClient(clientSocket);

    }

    serverSocket.close();

    } catch(IOException ioe) {

    System.out.println("Error: " + ioe);

    }}

    public static void ServiceClient(Socket client) throws IOException {

    System.out.println("已鏈接");

    InputStreamReader inSR = null;

    OutputStreamWriter outSW = null;

    try {

    //讀取數(shù)據(jù)

    inSR = new InputStreamReader(client.getInputStream(), "UTF-8");

    BufferedReader br = new BufferedReader(inSR);

    outSW = new OutputStreamWriter(client.getOutputStream(), "UTF-8");

    BufferedWriter bw = new BufferedWriter(outSW);

    String str = "";

    while((str = br.readLine()) != null) {

    str = str.trim();

    System.out.println("收到客戶端消息:" + str);

    bw.write("已收到信息:" + str + " \r\n"); //向客戶端反饋消息,加上分行符以便客戶端接收

    bw.flush();

    }

    } finally {

    //System.out.println("Cleaning up connection: " + client);

    inSR.close();

    outSW.close();

    client.close();

    }

    System.out.println("已斷開");

    }

    }

    解決后:

    import java.io.*;

    import java.net.*;

     public class SocketClientEx1 {

    public static void main(String[] args) {

    System.out.println("Client");

    try {

    Socket clientSocket = new Socket("localhost", 82);

    System.out.println("Client1:" + clientSocket);

    DataInputStream dataIS = new DataInputStream(clientSocket.getInputStream());

    InputStreamReader inSR = new InputStreamReader(dataIS, "UTF-8");

    BufferedReader br = new BufferedReader(inSR);

    DataOutputStream dataOS = new DataOutputStream(clientSocket.getOutputStream());

    BufferedWriter bw = new BufferedWriter(outSW);

    //輸入信息

    byte bytes[] = new byte[100];

    while(true) {

    System.out.println("----------------------------------");

    System.in.read(bytes);

    String str = new String(bytes);

    str = str.trim();

    if (str == "exit") {

    break;

    }

    //發(fā)送數(shù)據(jù)

    bw.write(str + "\r\n"); //加上分行符,以便服務(wù)器按行讀取

    bw.flush();

    //接收數(shù)據(jù)

    while((str = br.readLine()) != null) {

    str = str.trim();

    System.out.println("服務(wù)器回復(fù):" + str);

    break;

    }

    }

    inSR.close();

    dataIS.close();

    dataOS.close();

    clientSocket.close();

    } catch(UnknownHostException uhe) {

    System.out.println("Error:" + uhe.getMessage());

    } catch(ConnectException ce) {

    System.out.println("Error:" + ce.getMessage());

    } catch(IOException ioe) {

    System.out.println("Error:" + ioe.getMessage());

    } finally {

    }

    }

    }

    以上就是小編今天的分享了,希望可以幫助到大家。

    聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    java socket中文亂碼

    可以使用代碼:in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));解決。
    推薦度:
    標(biāo)簽: java 中文亂碼 socket
    • 熱門焦點(diǎn)

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 精品久久久久久国产潘金莲| 久久精品视频免费| 久久久久国产精品| 亚洲国产精品18久久久久久| 精品无码国产污污污免费网站国产 | 无码人妻精品一区二区三区夜夜嗨| 9久热这里只有精品| 97精品久久天干天天天按摩| 无码久久精品国产亚洲Av影片| 久久久久久亚洲精品无码| 国产精品国产欧美综合一区| 奇米精品一区二区三区在线观看| 99re这里只有精品国产精品| 国精无码欧精品亚洲一区| 无码精品一区二区三区在线| 一本一本久久aa综合精品| 欧美亚洲另类精品第一页| 久久精品成人| 精品福利一区二区三区| 国产精品乱码一区二区三区| 3级黄性日本午夜精品| 久久99国产精品一区二区| 9久久9久久精品| 99国产精品一区二区| HEYZO无码综合国产精品| 成人精品视频一区二区三区| freesexvideos精品老师毛多| 99在线精品一区二区三区| 538国产精品一区二区在线| 成人伊人精品色XXXX视频| 精品福利一区二区三| 国产精品视频一区二区三区| 久草视频在线这里精品| 久久96国产精品久久久| 91久久精品电影| 国产三级国产精品国产普男人| 精品国产福利尤物免费| 欧美精品色婷婷五月综合| 亚洲国产小视频精品久久久三级| 中文字幕无码精品亚洲资源网久久| 亚洲国产精品成人精品无码区|