這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)java中socket亂碼的解決方法,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)專(zhuān)注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、做網(wǎng)站、松山網(wǎng)絡(luò)推廣、小程序設(shè)計(jì)、松山網(wǎng)絡(luò)營(yíng)銷(xiāo)、松山企業(yè)策劃、松山品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供松山建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
解決方法:
首先在服務(wù)端使用InputStreamReader指定客戶端的輸入流為utf-8編碼,以及使用OutputStreamWriter指定輸出流為utf-8編碼;
然后在客戶端同樣的設(shè)置utf-8編碼即可。
in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8")); out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));
示例:
Server:
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("已斷開(kāi)"); } }
Client:
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()); OutputStreamWriter outSW = new OutputStreamWriter(dataOS, "UTF-8"); 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 { } } }
上述就是小編為大家分享的java中socket亂碼的解決方法了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。