java socket中文乱码

天下奇闻 2023-03-27 20:53www.nkfx.cn天下奇闻

java socket中文乱码怎么解决呢?不知道的小伙伴来看看趣谈网小编今天的分享吧!

可以使用代码

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

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

示例

解决前

import java.io.;

import java..; 

public class SocketServerEx1 {

public static void main(String args[]) {

System.out.println("Server");

ServerSocket serverSocket = null;

Socket clientSocket = null;

int connects = 0;

try {

serverSocket = ne ServerSocket(82, 5); //端口82,最大链接数5

//最多连接10次

hile(connects < 10) {

connects++;

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

clientSocket = serverSocket.aept(); //等待连接

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

ServiceClient(clientSocket);

}

serverSocket.close();

} catch(IOException ioe) {

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

}}

public static void ServiceClient(Socket client) thros IOException {

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

InputStreamReader inSR = null;

OutputStreamWriter outSW = null;

try {

//读取数据

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

BufferedReader br = ne BufferedReader(inSR);

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

BufferedWriter b = ne BufferedWriter(outSW);

String str = "";

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

str = str.trim();

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

b.rite("已收到信息" + str + " \r\n"); //向客户端反馈消息,加上分行符以便客户端接收

b.flush();

}

} finally {

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

inSR.close();

outSW.close();

client.close();

}

System.out.println("已断开");

}

}

解决后

import java.io.;

import java..;

 public class SocketClientEx1 {

public static void main(String[] args) {

System.out.println("Client");

try {

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

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

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

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

BufferedReader br = ne BufferedReader(inSR);

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

BufferedWriter b = ne BufferedWriter(outSW);

//输入信息

byte bytes[] = ne byte[100];

hile(true) {

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

System.in.read(bytes);

String str = ne String(bytes);

str = str.trim();

if (str == "exit") {

break;

}

//发送数据

b.rite(str + "\r\n"); //加上分行符,以便服务器按行读取

b.flush();

//接收数据

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

str = str.trim();

System.out.println("服务器回复" + str);

break;

}

}

inSR.close();

dataIS.close();

dataOS.close();

clientSocket.close();

} catch(UnknonHostException 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 {

}

}

}

以上就是趣谈网小编今天的分享了,希望可以帮助到大家。

Copyright © 2016-2025 www.nkfx.cn 趣谈网 版权所有 Power by