下面是一段基于TCP的多客户登录代码,运行以下代码,在控制台输入中文会出现乱码问题,请大家帮忙看看是怎么回事。- package javase.day18;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class MultiLogin {
- public static void main(String[] args) {
- }
- }
- class loginClient {
- public static void main(String[] args )throws Exception{
- System.out.println("client start...");
- Socket clientSocket = new Socket("192.168.1.2",10011);
- BufferedReader consoleMsgReader = new BufferedReader(new InputStreamReader(System.in,"UTF-8"));
- BufferedReader socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"UTF-8"));
- OutputStreamWriter osw = new OutputStreamWriter(clientSocket.getOutputStream(),"UTF-8");
- PrintWriter socketWriter = new PrintWriter(osw,true);
- String inputStr;
- for(int i=0 ; i<5 ; i++){
- System.out.print("please input your login name:");
- inputStr=consoleMsgReader.readLine();
- System.out.println("your input is :"+inputStr);
- if(inputStr==null){
- break;
- }
- socketWriter.println(inputStr);
- String responseMsgFromServer=socketReader.readLine();
- System.out.println(responseMsgFromServer);
- if(responseMsgFromServer.indexOf("successful")!=-1){
- break;
- }
- }
- }
- }
- class loginServer{
- public static void main(String[] args) throws Exception{
- System.out.println("server start...");
- ServerSocket ss = new ServerSocket(10011);
- Socket s = ss.accept();
- new Thread(new ServerThread(s)).start();
- ss.close();
- }
- }
- class ServerThread implements Runnable{
- private Socket s ;
- public ServerThread(Socket s){
- this.s=s ;
- }
- public void run(){
- try{
- BufferedReader socketReader = new BufferedReader(new InputStreamReader(this.s.getInputStream(),"UTF-8"));
- OutputStreamWriter osw = new OutputStreamWriter(this.s.getOutputStream(),"UTF-8");
- PrintWriter socketWriter = new PrintWriter(osw,true);
- String name;
- while((name = socketReader.readLine())!=null){
- System.out.println("name from client:"+name);
- BufferedReader userRecords = new BufferedReader(new FileReader("C:\\java_test\\user.txt"));
- String line=null;
- boolean flag = false;
- while((line=userRecords.readLine())!=null){
- if(line.equals(name)){
- flag=true;
- break;
- }
- }
- if(flag){
- System.out.println(name+"已登录");
- socketWriter.println("Login successful ! welcome to here , "+name);
- }else{
- socketWriter.println("Login failed , 用户名\""+name+"\"不存在,请重新输入");
- }
- }
-
- //this.s.close();
- }catch(Exception e){
- e.printStackTrace();
- }
-
-
- }
-
- }
复制代码 下面是控制台输出信息:
client start...
please input your login name:好好好
your input is :好好�?
Login failed , 用户名"好好�?"不存在,请重新输入
please input your login name:测试一下
your input is :测试�?��
Login failed , 用户名"测试�?��"不存在,请重新输入
please input your login name:哈哈哈哈哈哈哈
your input is :哈哈哈哈哈哈�?
Login failed , 用户名"哈哈哈哈哈哈�?"不存在,请重新输入
please input your login name:哈
your input is :�?
Login failed , 用户名"�?"不存在,请重新输入
please input your login name:never give up
your input is :never give up
Login failed , 用户名"never give up"不存在,请重新输入 |