求高手点拨一个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();
}
}
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();
}
}
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);
}
}
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);
}
}
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);
}
}
但是,我在C端写入的字符串的内容是:
String str = "CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,现在我们的群里,加入了182个人\r\n我们群里的人,都决心互相学习,增长本领";
现在经过弟我的检查,我的测试Java源代码文件的编码已经全部被我修改为下面的编码格式了:
我的MyEclipse编译器所采取的编码格式为:
希望得到高手的相助:
弟我的这个测试类文件中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();
}
}
}