java socket通信时,中文字符乱码的一个解决方法

java socket通信时,中文字符乱码的一个解决方法可以使用:in=newBufferedReader(newInputStreamReader(socket.getInputStream(),”UTF-8″));out=newBufferedWriter(newOutputStreamWriter(socket.getOutputStream(),”UTF-8″));socket示例:importjava

可以使用:
in = new BufferedReader(new InputStreamReader(socket.getInputStream(),”UTF-8″));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),”UTF-8″));

socket示例:

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());
			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 {
		}
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/10479.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注