189 8069 5689

java中socket乱码的解决方法-创新互联

这期内容当中小编将会给大家带来有关java中socket乱码的解决方法,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联主要从事成都网站设计、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务临夏州,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

解决方法:

首先在服务端使用InputStreamReader指定客户端的输入流为utf-8编码,以及使用OutputStreamWriter指定输出流为utf-8编码;

然后在客户端同样的设置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,大链接数: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("已断开");
	}
}

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;
				}
 
				//发送数据
				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 {
		}
	}
}

上述就是小编为大家分享的java中socket乱码的解决方法了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:java中socket乱码的解决方法-创新互联
标题路径:http://jkwzsj.com/article/codpie.html

其他资讯