高手,您好:
    我在写一个Java的通信程序,我进行了我的一个“Lib类”中的写出方法和读入方法的测试,测试代码如下:
    (C端)
    package s;import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;public class C_port {
Socket s = null;
OutputStream outputstream = null;
public C_port(){
try {
s = new Socket("127.0.0.1",60000);
outputstream = new DataOutputStream(s.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = "CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人\r\n我们群里的人,都决心互相学习,增长本领";
Lib.write(s, str);
}

public static void main(String[] args){
new C_port();
}

}    (上文中的第24行代码的Lib类的write方法,是我要测试的“系统读写功能的一个环节”..)
    (S端)
    package s;import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class S_port {
String str = "";
ServerSocket s =null;
Socket so = null;
S_port(){
try {
s = new ServerSocket(60000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
try {
so = s.accept();
SystemThread m1 = new SystemThread(so);
m1.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public static void main(String[] args){
new S_port();
}
}    (上文中S端的的第21行的SystemThread子线程类,他的源代码如下:)
     package s;import java.io.DataInputStream;
import java.io.InputStream;
import java.net.Socket;public class SystemThread extends Thread{
Socket so = null;
DataInputStream dips = null;
InputStream inputstream = null;
String str = null;

public SystemThread(Socket s){
so = s;
}

public void run(){
String str0 = Lib.readsocketUTF(so);
        System.out.println(str0);
}
}    (上文中的第18行代码的“Lib类的readsocketUTF()方法”是我要测试的“系统读写功能”的另外一个环节..)
     上文中的Lib类,我在我的项目中的两个端点,都进行了“配置”..
    (Lib类的代码如下:)
    package s;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;public class Lib {
    static InputStream is = null;
    static OutputStream outputstream = null;
    static String MyKey = "CJCO5882";
    static PrintStream ps;
    static BufferedReader br;
static String buffer0;
static OutputStream os = null;
static byte[] readbuf = null;
static byte[] writebuf = null;
static BufferedInputStream bis = null;
static BufferedOutputStream bos = null;
static int num  = 0;
static String str = "";
    
static String readsocketUTF(Socket s){
        String info = "";
         try {
is = s.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
try {
str = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
info = Systemcrypt.HloveyRC4(str,MyKey);
        return info;
    }

static void write(Socket s,String str0){ System.out.println("接受到一个客户端消息:" + s);
OutputStream os = null;
try {
os = s.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintStream ps = new PrintStream(os);
String sendInfo = Systemcrypt.HloveyRC4(str0, MyKey);
ps.println(sendInfo);
}
}    高手,上文中的Lib类里的第45行和第60行的“Systemcrypt类”和他的“HloveyRC4()方法”,是弟我打算进行“面向用户的通信隐私的保证的需求”,所进行的“系统端点读字符串数据时面向内存传入本类的字符串的解密”和“系统端点写字符串时面向内存中传入自己的参数列表的字符串进行加密”的功能实施的一个配置到我的两个通信端点的负责加密解密功能的一个“加密类”文件.
    她的源代码如下:
    package s;
public class Systemcrypt{public static String HloveyRC4(String aInput,String aKey)   
    {   
        int[] iS = new int[256];   
        byte[] iK = new byte[256];   
          
        for (int i=0;i<256;i++)   
            iS[i]=i;   
              
        int j = 1;   
          
        for (short i= 0;i<256;i++)   
        {   
            iK[i]=(byte)aKey.charAt((i % aKey.length()));   
        }   
          
        j=0;   
          
        for (int i=0;i<255;i++)   
        {   
            j=(j+iS[i]+iK[i]) % 256;   
            int temp = iS[i];   
            iS[i]=iS[j];   
            iS[j]=temp;   
        }   
      
      
        int i=0;   
        j=0;   
        char[] iInputChar = aInput.toCharArray();   
        char[] iOutputChar = new char[iInputChar.length];   
        for(short x = 0;x<iInputChar.length;x++)   
        {   
            i = (i+1) % 256;   
            j = (j+iS[i]) % 256;   
            int temp = iS[i];   
            iS[i]=iS[j];   
            iS[j]=temp;   
            int t = (iS[i]+(iS[j] % 256)) % 256;   
            int iY = iS[t];   
            char iCY = (char)iY;   
            iOutputChar[x] =(char)( iInputChar[x] ^ iCY) ;      
        }   
          
        return new String(iOutputChar);   
                  
    } 
}
    这里的RC4算法所实现的“HloveyRC4()方法”他的参数列表的两个参数“HloveyRC4(String aInput,String aKey) ”的作用和意义为:
    “aInput”为开发者写入的“待加密字符串”,其中的“aKey”,也是一个String类型的数据,他的作用是在这个HloveyRC4()方法所加密的“字符串”的“加密密钥”和“解密密钥”上,进行设置.让我的HloveyRC4(String aInput,String aKey) 这个方法,能够选择对应的密钥,进行加密或者解密的功能实现.
    :
    如果实施“加密”功能的话,采用了一个字符串作为密钥,那么进行本字符串的解密功能的实施的时候,就业要向上面的“HloveyRC4(String aInput,String aKey) ”方法的“aKey”参数进行本字符串被加密时所选择的密钥,进行解密.才能够将明文给拿到.
    现在我运行了S端的入口类,又运行了我的C端的入口类之后,得到的S端的console窗口中的内容如下:
    
    但是,我在C端写入的字符串的内容是:
    String str = "CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人\r\n我们群里的人,都决心互相学习,增长本领";
    现在经过弟我的检查,我的测试Java源代码文件的编码已经全部被我修改为下面的编码格式了:
    
    我的MyEclipse编译器所采取的编码格式为:
    
    希望得到高手的相助:
    弟我的这个测试类文件中S端出现的乱码,是什么原因造成的..?
    应该怎么进行修改,能够让我的这个工程中所有的乱码,都能清清澈澈地变成“C端用户的通信数据”..?
    谢谢高手的点拨!!
    弟我在线等!!
    一百分奉上!!
                                                             一位日日夜夜向着理想奔跑的筑梦者
                                                            2013年11月15日星期五下午18点41分Java乱码编码格式Java SocketJava调试Java改错

解决方案 »

  1.   

    不好意思,高手,忙乱中,我传错了一个图片...
        弟我的console窗口的S端的显示内容是:
        
        (上面的“我们梦想的山峰”,是弟我的群:“共攀通信软件高峰”群的群头像,我的群的群号是:
        308332398.
        欢迎诸位高手的参加.)
        
      

  2.   

    纳兰弦歌,请您担待我的错误..
        我已经采用“DeBug模式”将错误“调回来了”...
      

  3.   

    高手,您好:
        经过一个晚上的调试,我最新的进度为:
        找到了一个:“转码类”,能够让我的String类型的数据,进行“转码”操作.
        现在,我得到的转码类的代码如下:
        package s;import java.io.UnsupportedEncodingException;/**
    * 转换字符串的编码
    * @author joe
    *
    */public class ChangeCharset {
          /** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块      */
         public static final String US_ASCII = "US-ASCII";
          /** ISO拉丁字母表 No.1,也叫做ISO-LATIN-1     */
         public static final String ISO_8859_1 = "ISO-8859-1";
          /** 8 位 UCS 转换格式     */
         public static final String UTF_8 = "UTF-8";
          /** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序     */
         public static final String UTF_16BE = "UTF-16BE";
          /** 16 位 UCS 转换格式,Litter Endian(最高地址存放地位字节)字节顺序     */
         public static final String UTF_16LE = "UTF-16LE";
          /** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识     */
         public static final String UTF_16 = "UTF-16";
          /** 中文超大字符集     **/
         public static final String GBK = "GBK";
         
         public static final String GB2312 = "GB2312";
         
          /** 将字符编码转换成US-ASCII码     */
          public static String toASCII(String str) throws UnsupportedEncodingException {
             return changeCharset(str, US_ASCII);
         }
         
          /** 将字符编码转换成ISO-8859-1     */
          public static String toISO_8859_1(String str) throws UnsupportedEncodingException {
             return changeCharset(str, ISO_8859_1);
         }
         
          /** 将字符编码转换成UTF-8     */
          public static String toUTF_8(String str) throws UnsupportedEncodingException {
             return changeCharset(str, UTF_8);
         }
         
          /** 将字符编码转换成UTF-16BE     */
          public static String toUTF_16BE(String str) throws UnsupportedEncodingException{
             return changeCharset(str, UTF_16BE);
         }
         
          /** 将字符编码转换成UTF-16LE     */
          public static String toUTF_16LE(String str) throws UnsupportedEncodingException {
             return changeCharset(str, UTF_16LE);
         }
         
          /** 将字符编码转换成UTF-16     */
          public static String toUTF_16(String str) throws UnsupportedEncodingException {
             return changeCharset(str, UTF_16);
         }
         
          /** 将字符编码转换成GBK     */
          public static String toGBK(String str) throws UnsupportedEncodingException {
             return changeCharset(str, GBK);
         }
         
          /** 将字符编码转换成GB2312     */
          public static String toGB2312(String str) throws UnsupportedEncodingException {
             return changeCharset(str,GB2312);
         }
         
          /**
          * 字符串编码转换的实现方法
          * @param str    待转换的字符串
          * @param newCharset    目标编码
          */
          public static String changeCharset(String str, String newCharset) throws UnsupportedEncodingException {
              if(str != null) {
                 //用默认字符编码解码字符串。与系统相关,中文windows默认为GB2312
                 byte[] bs = str.getBytes();
                 return new String(bs, newCharset);    //用新的字符编码生成字符串
             }
             return null;你
        今天,我在各个高手聚居的群中经过反复请教,得到:“多尝试”“多修改”“能够将问题解决”,的指导.
        于是,我将我的“Lib类”进行了下述的修改,得到了下述的实验结果:
        [code=java]package s;import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.io.UnsupportedEncodingException;
    import java.net.Socket;public class Lib {
        static InputStream is = null;
        static OutputStream outputstream = null;
        static String MyKey = "CJCO5882";
        static PrintStream ps;
        static BufferedReader br;
    static String buffer0;
    static OutputStream os = null;
    static byte[] readbuf = null;
    static byte[] writebuf = null;
    static BufferedInputStream bis = null;
    static BufferedOutputStream bos = null;
    static int num  = 0;
    static String str = "";
        
    static String readsocketUTF(Socket s) throws UnsupportedEncodingException{
            String info = "";
             try {
    is = s.getInputStream();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    try {
    str = br.readLine();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    info = Systemcrypt.HloveyRC4(str,MyKey);
            return ChangeCharset.toUTF_8(info);
        }

    static void write(Socket s,String str0) throws UnsupportedEncodingException{ System.out.println("接受到一个客户端消息:" + s);
    OutputStream os = null;
    try {
    os = s.getOutputStream();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    PrintStream ps = new PrintStream(os);
    String sendInfo = Systemcrypt.HloveyRC4(ChangeCharset.toUTF_8(str0), MyKey);
    ps.println(sendInfo);
    }
    }    得到的结果如下:
        
        我又进行了下面的Lib类的修改:
        package s;import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.io.UnsupportedEncodingException;
    import java.net.Socket;public class Lib {
        static InputStream is = null;
        static OutputStream outputstream = null;
        static String MyKey = "CJCO5882";
        static PrintStream ps;
        static BufferedReader br;
    static String buffer0;
    static OutputStream os = null;
    static byte[] readbuf = null;
    static byte[] writebuf = null;
    static BufferedInputStream bis = null;
    static BufferedOutputStream bos = null;
    static int num  = 0;
    static String str = "";
        
    static String readsocketUTF(Socket s) throws UnsupportedEncodingException{
            String info = "";
             try {
    is = s.getInputStream();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    try {
    str = br.readLine();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    info = Systemcrypt.HloveyRC4(str,MyKey);
            return ChangeCharset.toGBK(info);
        }

    static void write(Socket s,String str0) throws UnsupportedEncodingException{ System.out.println("接受到一个客户端消息:" + s);
    OutputStream os = null;
    try {
    os = s.getOutputStream();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    PrintStream ps = new PrintStream(os);
    String sendInfo = Systemcrypt.HloveyRC4(ChangeCharset.toGBK(str0), MyKey);
    ps.println(sendInfo);
    }
    }    得到的结果如下:
        
        之后,我将Lib类进行了类似上述两块代码位置的“ChangeCharset.toGB2312()方法”,进行了修改.
        得到的结果如下:
        
        我还将我的上述代码进行了类似上面两块代码位置的“ChangeCharset.toISO_8859_1()”的代码的修改,得到的结果如下:
        
        希望得到CSDN中的高手的点拨:
        我的代码的运行结果,为什么会有“乱码”出现?
        经过弟我的修改,为什么无法将乱码的问题解决?
        希望CSDN,我的大学,能够点拨我的错误的原因,让这个困扰了我3天的时间的问题,得到老师的指导.
        谢谢CSDN!!
        谢谢我的老师!!我的朋友!!
      

  4.   

    这是我的一个小号,上面的类型,我的测试环境,总共就是上面的:“S_port”,“C_port”,“SystemThread”,“Systemcrypt”,和弟我由于粗心,写到一起的“ChangeCharset”和“Lib”类,弟我的测试环境中的代码,弟我已经上齐了.
    希望能够得到CSDN的朋友,老师的重视!!
    为弟我,将乱码的原因揪出来,并且告诉我:
    怎么修改..?
    谢谢CSDN!!我的大学!!
      

  5.   

    CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人
    我们群里的人,都决心互相学习,增长本领
    CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人
    我们群里的人,都决心互相学习,增长本领
    CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人
    我们群里的人,都决心互相学习,增长本领这是我运行的结果,如果这是你想要的,那就上面的代码是没有问题的,转码类不需要,只是c_port的输出覆盖了s_port的输出
      

  6.   

    问题已经帮你解决了,主要是因为你使用RC4加密完的字符串传输到服务端的时候部分字符变成了无法识别的字符;这样解密出来自然就出现问题了。基于这样一个问题,我也没有去使用设置编码的方式来解决,我是将加密完的字符转换为byte类型的数组进行传输,到服务端的时候再将byte数组转为字符串,这样就不会出现在传输的过程中导致字符被替换的问题了
    修改完的代码如下:Systemcrypt 加密类返回的是一个字节数组
    package s;
    public class Systemcrypt { public static byte[] HloveyRC4(String aInput, String aKey) {
    int[] iS = new int[256];
    byte[] iK = new byte[256]; for (int i = 0; i < 256; i++)
    iS[i] = i; int j = 1; for (short i = 0; i < 256; i++) {
    iK[i] = (byte) aKey.charAt((i % aKey.length()));
    } j = 0; for (int i = 0; i < 255; i++) {
    j = (j + iS[i] + iK[i]) % 256;
    int temp = iS[i];
    iS[i] = iS[j];
    iS[j] = temp;
    } int i = 0;
    j = 0;
    char[] iInputChar = aInput.toCharArray();
    char[] iOutputChar = new char[iInputChar.length];
    for (short x = 0; x < iInputChar.length; x++) {
    i = (i + 1) % 256;
    j = (j + iS[i]) % 256;
    int temp = iS[i];
    iS[i] = iS[j];
    iS[j] = temp;
    int t = (iS[i] + (iS[j] % 256)) % 256;
    int iY = iS[t];
    char iCY = (char) iY;
    iOutputChar[x] = (char) (iInputChar[x] ^ iCY);
    } byte[] resByte = new byte[iOutputChar.length * 2];

    for(short x = 0; x < iOutputChar.length; x++){
    resByte[2*x] = (byte) ((iOutputChar[x] & 0xFF00) >> 8);
    resByte[2*x + 1] = (byte) (iOutputChar[x] & 0xFF);
    }

    return resByte; }

    }
    package s;import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.net.Socket;public class Lib {
    static InputStream is = null;
    static OutputStream outputstream = null;
    static String MyKey = "CJCO5882";
    static PrintStream ps;
    static BufferedReader br;
    static String buffer0;
    static OutputStream os = null;
    static byte[] readbuf = null;
    static byte[] writebuf = null;
    static BufferedInputStream bis = null;
    static BufferedOutputStream bos = null;
    static int num = 0;
    static String str = ""; static String readsocketUTF(Socket s) {
    String info = "";
    try {
    is = s.getInputStream();
    } catch (IOException e) {
    e.printStackTrace();
    }
    try {
    //使用字节来传输
    int r = -1;
    int index = 0;
    byte[] b = new byte[2];
    while((r = is.read()) != -1){
    b[index] = (byte)r;
    if(index==1){
    //将字节数据转回字符
    char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
    index = 0;
    str += c;
    }else{
    index++;
    }
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    byte[] bResult = Systemcrypt.HloveyRC4(str, MyKey);
    char[] cResult = new char[bResult.length / 2];
    for(int i = 0;i<bResult.length/2;i++){
    cResult[i] =  (char) (((bResult[2*i] & 0xFF) << 8) | (bResult[2*i+1] & 0xFF));
    }
    info = new String(cResult);
    return info;
    } static void write(Socket s, String str0) { System.out.println("接受到一个客户端消息:" + s);
    OutputStream os = null;
    try {
    os = s.getOutputStream();
    PrintStream ps = new PrintStream(os);
    byte[] sendInfo = Systemcrypt.HloveyRC4(str0, MyKey);
    System.out.println(sendInfo);
    ps.write(sendInfo, 0, sendInfo.length);
    ps.close();
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }希望对你有用,代码可以再重构一下,这主要是个思路而已哈
      

  7.   

     
    谢谢happytaocool哥的点拨!!
    请问happytaocool哥:
        为什么会有:“传输的String类型的字符串”“在通信的过程中会出现:可能字符串被替换的问题”.
        而:“传输的byte[]就能够在传输的过程中,保证完整无缺”...?
        字符串在通信的过程中,应该也是在进行“0和1”的高低电频的传输的吧...?
        传输的介质一样,为什么会有:
        String类型的数据可以被替换,而byte[]类型的数据不能够被替换的场景出现呢...?