java socket中文乱码
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..;
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,最大链接数5
//最多连接10次
while(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) throws IOException {
System.out.println("已链接");
InputStreamReader inSR = null;
OutputStreamWriter outSW = null;
try {
//读取数据
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..;
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;
}
//发送数据
bw.write(str + "\r\n"); //加上分行符,以便服务器按行读取
bw.flush();
//接收数据
while((str = br.readLine()) != null) {
str = str.trim();
System.out.println("服务器回复" + 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 {
}
}
}
以上就是小编今天的分享了,希望可以帮助到大家。
生活百科
- 蚌埠天气预报10天 蚌埠天气预报下载
- 5000个税计算器 个税计算税率表5000
- 30岁不结婚该判刑 30岁不结婚该判刑多久
- 宝鸡天气预报10天 宝鸡天气预报下载
- 3月8日妇女节放假吗 三月八日妇女节放假
- 2024端午节放假通知来了 2024端午假期
- 总公司与分公司 总公司与分公司的法律责任
- 醉驾刑事拘留一般多少天
- 咨询律师要多少钱 咨询律师的费用是多少
- 醉酒撞停放车身亡 醉酒撞停放车身亡责任划分
- 注册一个商标多少钱 注册一个商标大概要多少钱
- 浙传失联女生离世 浙江传媒学院失联学生已确认离世
- 诈骗罪立案标准 诈骗罪立案标准2024
- 占地补偿标准:2024征地补偿标准明细表
- 怎么注册公司要多少钱
- 宅基地继承最新政策