首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

求好手点拨一个Java的Socket通信程序的故障原因.

2013-11-18 
求高手点拨一个Java的Socket通信程序的故障原因...高手,您好:我在写一个Java的通信程序,我进行了我的一个“

求高手点拨一个Java的Socket通信程序的故障原因...
高手,您好:
    我在写一个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窗口中的内容如下:


    求好手点拨一个Java的Socket通信程序的故障原因.
    但是,我在C端写入的字符串的内容是:
    String str = "CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人\r\n我们群里的人,都决心互相学习,增长本领";
    现在经过弟我的检查,我的测试Java源代码文件的编码已经全部被我修改为下面的编码格式了:
    求好手点拨一个Java的Socket通信程序的故障原因.
    我的MyEclipse编译器所采取的编码格式为:
    求好手点拨一个Java的Socket通信程序的故障原因.
    希望得到高手的相助:
    弟我的这个测试类文件中S端出现的乱码,是什么原因造成的..?
    应该怎么进行修改,能够让我的这个工程中所有的乱码,都能清清澈澈地变成“C端用户的通信数据”..?
    谢谢高手的点拨!!
    弟我在线等!!
    一百分奉上!!
                                                             一位日日夜夜向着理想奔跑的筑梦者
                                                            2013年11月15日星期五下午18点41分 Java乱码 编码格式 Java?Socket Java调试 Java改错
[解决办法]
CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人
我们群里的人,都决心互相学习,增长本领
CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人
我们群里的人,都决心互相学习,增长本领
CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人
我们群里的人,都决心互相学习,增长本领

这是我运行的结果,如果这是你想要的,那就上面的代码是没有问题的,转码类不需要,只是c_port的输出覆盖了s_port的输出
[解决办法]
问题已经帮你解决了,主要是因为你使用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();
}
}
}



希望对你有用,代码可以再重构一下,这主要是个思路而已哈

热点排行