I'm writing a Java communication program , I had one of my "Lib class " in the write method and the method of reading the test, the test code is as follows :
(C side )
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();
}
}
( above the first 24 lines of code Lib class write method is that I want to test , " a part of the system to read and write functions " .. )
(S side )
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 side above the first 21 rows SystemThread child thread class , whose source code is as follows :)
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);
}
}
( above the first 18 lines of code "Lib class readsocketUTF () method " Yes I want to test the " system read and write function" is another link .. )
Lib earlier in class in my project two endpoints , have carried out a "Configuration" ..
(Lib class code is as follows :)
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);
}
}
master, Lib class earlier in the 45th row and 60th row "Systemcrypt class " and his "HloveyRC4 () method ", is the brother I intend to conduct "face user's communication needs to ensure privacy , " conducted by " System endpoint -oriented memory when reading string data passed to decrypt the string class "and" System endpoint string for memory write their own list of arguments passed string encryption "feature implemented in a configuration to my two communication endpoints responsible for encryption and decryption functions of an " encryption class "file.
She 's source code as follows:
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);
}
}
Here's RC4 algorithm achieved "HloveyRC4 () method " His argument list two parameters "HloveyRC4 (String aInput, String aKey)" The role and meaning :
"aInput" for developers to write "Pending encrypted string " where "aKey", is a String data type , his role is in this HloveyRC4 () encryption method "String " and " encryption key " and " decryption key ", the set let me HloveyRC4 (String aInput, String aKey) this method can select the corresponding key encryption or decryption function realization .
:
If the implementation of " encryption " feature, using a string as a key , then decryption functions for the implementation of the string when the job To the above "HloveyRC4 ( string aInput, String aKey) " method " aKey " parameter of the string is selected when the key is encrypted , decrypted. was able to get the plaintext .
Now I run the entrance end of the S class , and run my C-terminal entrance class, the resulting S-side of the console window reads as follows:
However, I write in the C-terminus of the string are:
String str = "CJ win ! ! CJCO! ! CJHMF, this issue is resolved , I am pleased , and now in our group , joined the 182 individual \ r \ n our group and those who are determined to learn from each other , grow skills " ;
Now, after checking my brother , my test Java source file encoding have all been modified to me the following encoding formats :
My MyEclipse taken by the compiler encoding format:
hope to get a master 's help :
my brother this test S-side class file appears garbled, is what causes .. ?
how should be modified to allow me this project all garbled , can clear clear Chul becomes "C end user communication data " .. ?
Thank you, master coaching ! !
brother I Online ! !
hundred points offer ! !
a perfect running day and night to build toward the dreamer
November 15, 2013 Friday 18:41
------ Solution ---- ----------------------------------------
CJ win ! ! CJCO! ! CJHMF, this issue is resolved , I am pleased , and now in our group , joined 182 people
our group and those who are determined to learn from each other , grow skills
CJ win ! ! CJCO! ! CJHMF, this issue is resolved , I am pleased , and now in our group , joined 182 people
our group and those who are determined to learn from each other , grow skills
CJ win ! ! CJCO! ! CJHMF, this issue is resolved , I am pleased , and now in our group , joined 182 people
our group and those who are determined to learn from each other , grow skills
This is my result of the operation , if this is what you want , then the above code is not a problem , transcoding classes do not need , just c_port output covers s_port output
------ Solution --------------------------------- -----------
problem has been solved for you , mainly because you are finished using the RC4 encryption string transmitted to the server when some characters became unrecognizable characters ; such decrypted naturally cause a problem. Based on such an issue , I do not set the encoding to use way to solve , I was finished encrypted characters into an array of type byte are transmitted to the server and then when the byte array to a string , so it will not appear in the transmission process causes characters are replaced by a question
modifying the code as follows:
Systemcrypt encryption class returns a byte array
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();
}
}
}
hope useful to you , the code can then reconstruct it, thinking it was a largely Kazakhstan
------ For reference only ---------------- -----------------------
------ For reference only ----------------------- ----------------
sorry, master, hectic , I pass a wrong picture ...
brother of my S-side console window display reads:
( above the " mountain of our dreams " is the brother of my group: "a total peak climbing communication software " group group portrait, my group group number is :
308332398.
welcome you to participate in master . )
------ For reference only -------------------------- -------------
Nalan is music , please be magnanimous my mistake ..
I have used "DeBug Mode" the error "tune back " ...
------ For reference only --------- ------------------------------
master, hello :
After a night of debugging, my latest progress as follows:
found a : " transcoding classes" can make me a String data type , the " transcoding " operation.
Now I got transcoding class code is as follows:
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);
}
}
The results obtained are as follows:
I conducted the following Lib class changes:
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);
}
}
The results obtained are as follows:
After that, I will Lib class were similar to the above two code location "ChangeCharset.toGB2312 () method ," has been modified.
The results obtained are as follows:
I also had my above code similar to the above two code location "ChangeCharset.toISO_8859_1 ()" code changes, the results obtained are as follows:
CSDN
hope to get a master 's in coaching:
results of running my code , why is there " garbled " appears ?
After modifying my brother , why not be able to solve the problem of garbage ?
hope CSDN, my university , my coaching mistakes can cause , so that troubled me three days of the time the problem , get the teacher's guidance.
Thank CSDN! !
thank my teacher ! ! My friend ! !
------ For reference only -------------------------------------- -
this is one of my trumpet , the above type , my test environment , a total that is above : "S_port", "C_port", "SystemThread", "Systemcrypt", and my brother through carelessness , wrote together "ChangeCharset" and "Lib" category , brother of my test environment code , brother I've been on the Qi .
hoping to get CSDN friend, teacher 's attention ! !
for the brother I will kick out the reasons for garbled , and told me:
how to modify .. ?
Thank CSDN! ! My college ! !
------ For reference only -------------------------------------- -
which is engaged in marketing it
------ For reference only ---------------------------- -----------
Thank happytaocool brother on the head ! !
Will happytaocool brother:
Why : "Transmission of type String string" " in the communication process will occur : the question may be replaced with a string ."
And : " transfer byte [] will be able to transfer the process to ensure intact " ... ?
string in the communication process, should also be conducting "0 and 1" high and low frequency transmission , right ... ?
transmission medium , like , why is there :
String types of data can be replaced , and byte [] type of data can not be replaced by a scene appear too ... ?
没有评论:
发表评论