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.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,最大链接数: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 {
//读取数据
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;
}
//发送数据
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 {
}
}
}
-
马自达3星骋两厢二手怎么样?
马自达3星骋两厢二手车如果车况好的话还是值得买的,但是购车要注意:1、查询新车市场价格,在购买二手车之前,先要了解该款二手车是什么品牌的哪款车型,然后去查询这款二手车的新车最新...
问答查看全文>> -
女人霸气高冷到爆的句子 霸气高冷到爆的句子
1、如果还不能脱单,就赶紧让自己脱贫吧,你总不能身边没人,卡里又没钱。2、我不是好奇心强,我只是觉得你怎么还会站在我面前3、对你越客气,就证明我,越不想和你有任何关系4、你的戏...
句子查看全文>> -
奥迪a3后雨刮器怎么开?
汽车后窗雨刷的开启步骤:1、正面对着方向盘,在方向盘的右手边可以看到雨刷器的控制杆:2、其中有一个雨刷器控制杆头部是可以转动的开关,这个开关就是用来控制后窗雨刷的:3、轻轻转动...
问答查看全文>>
推荐
- 1电瓶严重亏电指示灯一闪一闪怎么回事104
- 2宝马5系混动版不充电还能跑吗450
- 3描写老师的好句459
- 4xrv本田怎么样好不好?145
- 5qq空间经典伤感语录293
- 6车前草茶制作方法316
- 7刹车泵怎么快速排气?116
- 8新车摇号流程230