PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/JavaProjects/IMSample2011-01-25-1320/IMSample/src/com/heyang/biz/server/test/TestClientSocket.java

https://gitlab.com/BGCX261/zjh-dev-svn-to-git
Java | 335 lines | 242 code | 59 blank | 34 comment | 0 complexity | 429feb16a649c07b2ef880c8ecfc347c MD5 | raw file
  1. package com.heyang.biz.server.test;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7. import org.apache.commons.codec.binary.Base64;
  8. import com.heyang.common.cipher.IMMsgDecryptException;
  9. import com.heyang.common.cipher.IMMsgDecrypter;
  10. import com.heyang.common.cipher.IMMsgEncryptException;
  11. import com.heyang.common.cipher.IMMsgEncrypter;
  12. import com.heyang.common.code.AESSecurityCoder;
  13. import com.heyang.common.code.RSASecurityCoder;
  14. /**
  15. * 此类用来模拟客户端进行测试
  16. * 说明:
  17. * 作者:何杨(heyang78@gmail.com)
  18. * 创建时间:2010-12-24 下午06:16:20
  19. * 修改时间:2010-12-24 下午06:16:20
  20. */
  21. public class TestClientSocket{
  22. public static void main(String[] args) throws Exception{
  23. RSASecurityCoder rsaCoder=new RSASecurityCoder();
  24. byte[] serverPublicKey=getPublicKey();
  25. AESSecurityCoder aesCoder=new AESSecurityCoder();
  26. testRegister(rsaCoder,serverPublicKey,aesCoder);
  27. testLogin(rsaCoder,serverPublicKey,aesCoder);
  28. testGetUserList(rsaCoder,serverPublicKey,aesCoder);
  29. testDispatch(rsaCoder,serverPublicKey,aesCoder);
  30. testLogout(rsaCoder,serverPublicKey,aesCoder);
  31. }
  32. public static void testGetUserList(RSASecurityCoder rsaCoder,byte[] serverPublicKey,AESSecurityCoder aesCoder) throws Exception{
  33. Socket s=new Socket("127.0.0.1",8888);
  34. InputStream inStram=s.getInputStream();
  35. OutputStream outStream=s.getOutputStream();
  36. // 输出
  37. PrintWriter out=new PrintWriter(outStream,true);
  38. // 待加密的明文
  39. StringBuilder sb1=new StringBuilder();
  40. sb1.append("<request>");
  41. sb1.append("<command>getUserList</command>");
  42. sb1.append("<username>张飞</username>");
  43. sb1.append("<password>123456</password>");
  44. sb1.append("</request>");
  45. String plainText=sb1.toString();
  46. String request="";
  47. try{
  48. // 加密过程
  49. IMMsgEncrypter encrypter=new IMMsgEncrypter(plainText,Base64.encodeBase64String(serverPublicKey),rsaCoder,aesCoder);
  50. request=encrypter.getCipheredMsg();
  51. }
  52. catch(IMMsgEncryptException e){
  53. System.out.println("无法对客户端请求进行加密");
  54. }
  55. out.print(request);
  56. out.flush();
  57. s.shutdownOutput();// 输出结束
  58. // 输入
  59. Scanner in=new Scanner(inStram);
  60. StringBuilder sb=new StringBuilder();
  61. while(in.hasNextLine()){
  62. String line=in.nextLine();
  63. sb.append(line);
  64. }
  65. String response=sb.toString();
  66. s.close();
  67. try{
  68. // 解密过程
  69. IMMsgDecrypter decrypter=new IMMsgDecrypter(response,rsaCoder,aesCoder);
  70. String plainResponse=decrypter.getPlainMsg();
  71. System.out.println("plainResponse="+plainResponse);
  72. }catch(IMMsgDecryptException e){
  73. System.out.println("无法对服务器端响应进行解密");
  74. }
  75. }
  76. public static void testDispatch(RSASecurityCoder rsaCoder,byte[] serverPublicKey,AESSecurityCoder aesCoder) throws Exception{
  77. Socket s=new Socket("127.0.0.1",8888);
  78. InputStream inStram=s.getInputStream();
  79. OutputStream outStream=s.getOutputStream();
  80. // 输出
  81. PrintWriter out=new PrintWriter(outStream,true);
  82. // 待加密的明文
  83. StringBuilder sb1=new StringBuilder();
  84. sb1.append("<request>");
  85. sb1.append("<command>dispatch</command>");
  86. sb1.append("<username>张飞</username>");
  87. sb1.append("<password>123456</password>");
  88. sb1.append("<to>张飞</to>");
  89. sb1.append("<msg>你好!张飞!</msg>");
  90. sb1.append("</request>");
  91. String plainText=sb1.toString();
  92. String request="";
  93. try{
  94. // 加密过程
  95. IMMsgEncrypter encrypter=new IMMsgEncrypter(plainText,Base64.encodeBase64String(serverPublicKey),rsaCoder,aesCoder);
  96. request=encrypter.getCipheredMsg();
  97. }
  98. catch(IMMsgEncryptException e){
  99. System.out.println("无法对客户端请求进行加密");
  100. }
  101. out.print(request);
  102. out.flush();
  103. s.shutdownOutput();// 输出结束
  104. // 输入
  105. Scanner in=new Scanner(inStram);
  106. StringBuilder sb=new StringBuilder();
  107. while(in.hasNextLine()){
  108. String line=in.nextLine();
  109. sb.append(line);
  110. }
  111. String response=sb.toString();
  112. s.close();
  113. try{
  114. // 解密过程
  115. IMMsgDecrypter decrypter=new IMMsgDecrypter(response,rsaCoder,aesCoder);
  116. String plainResponse=decrypter.getPlainMsg();
  117. System.out.println("plainResponse="+plainResponse);
  118. }catch(IMMsgDecryptException e){
  119. System.out.println("无法对服务器端响应进行解密");
  120. }
  121. }
  122. public static void testLogout(RSASecurityCoder rsaCoder,byte[] serverPublicKey,AESSecurityCoder aesCoder) throws Exception{
  123. Socket s=new Socket("127.0.0.1",8888);
  124. InputStream inStram=s.getInputStream();
  125. OutputStream outStream=s.getOutputStream();
  126. // 输出
  127. PrintWriter out=new PrintWriter(outStream,true);
  128. // 待加密的明文
  129. StringBuilder sb1=new StringBuilder();
  130. sb1.append("<request>");
  131. sb1.append("<command>logout</command>");
  132. sb1.append("<username>张飞</username>");
  133. sb1.append("<password>123456</password>");
  134. sb1.append("</request>");
  135. String plainText=sb1.toString();
  136. String request="";
  137. try{
  138. // 加密过程
  139. IMMsgEncrypter encrypter=new IMMsgEncrypter(plainText,Base64.encodeBase64String(serverPublicKey),rsaCoder,aesCoder);
  140. request=encrypter.getCipheredMsg();
  141. }
  142. catch(IMMsgEncryptException e){
  143. System.out.println("无法对客户端请求进行加密");
  144. }
  145. out.print(request);
  146. out.flush();
  147. s.shutdownOutput();// 输出结束
  148. // 输入
  149. Scanner in=new Scanner(inStram);
  150. StringBuilder sb=new StringBuilder();
  151. while(in.hasNextLine()){
  152. String line=in.nextLine();
  153. sb.append(line);
  154. }
  155. String response=sb.toString();
  156. s.close();
  157. try{
  158. // 解密过程
  159. IMMsgDecrypter decrypter=new IMMsgDecrypter(response,rsaCoder,aesCoder);
  160. String plainResponse=decrypter.getPlainMsg();
  161. System.out.println("plainResponse="+plainResponse);
  162. }catch(IMMsgDecryptException e){
  163. System.out.println("无法对服务器端响应进行解密");
  164. }
  165. }
  166. public static void testLogin(RSASecurityCoder rsaCoder,byte[] serverPublicKey,AESSecurityCoder aesCoder) throws Exception{
  167. Socket s=new Socket("127.0.0.1",8888);
  168. InputStream inStram=s.getInputStream();
  169. OutputStream outStream=s.getOutputStream();
  170. // 输出
  171. PrintWriter out=new PrintWriter(outStream,true);
  172. // 待加密的明文
  173. StringBuilder sb1=new StringBuilder();
  174. sb1.append("<request>");
  175. sb1.append("<command>login</command>");
  176. sb1.append("<username>张飞</username>");
  177. sb1.append("<password>123456</password>");
  178. sb1.append("<port>8889</port>");
  179. sb1.append("</request>");
  180. String plainText=sb1.toString();
  181. String request="";
  182. try{
  183. // 加密过程
  184. IMMsgEncrypter encrypter=new IMMsgEncrypter(plainText,Base64.encodeBase64String(serverPublicKey),rsaCoder,aesCoder);
  185. request=encrypter.getCipheredMsg();
  186. }
  187. catch(IMMsgEncryptException e){
  188. System.out.println("无法对客户端请求进行加密");
  189. }
  190. out.print(request);
  191. out.flush();
  192. s.shutdownOutput();// 输出结束
  193. // 输入
  194. Scanner in=new Scanner(inStram);
  195. StringBuilder sb=new StringBuilder();
  196. while(in.hasNextLine()){
  197. String line=in.nextLine();
  198. sb.append(line);
  199. }
  200. String response=sb.toString();
  201. s.close();
  202. try{
  203. // 解密过程
  204. IMMsgDecrypter decrypter=new IMMsgDecrypter(response,rsaCoder,aesCoder);
  205. String plainResponse=decrypter.getPlainMsg();
  206. System.out.println("plainResponse="+plainResponse);
  207. }catch(IMMsgDecryptException e){
  208. System.out.println("无法对服务器端响应进行解密");
  209. }
  210. }
  211. public static void testRegister(RSASecurityCoder rsaCoder,byte[] serverPublicKey,AESSecurityCoder aesCoder) throws Exception{
  212. Socket s=new Socket("127.0.0.1",8888);
  213. InputStream inStram=s.getInputStream();
  214. OutputStream outStream=s.getOutputStream();
  215. // 输出
  216. PrintWriter out=new PrintWriter(outStream,true);
  217. // 待加密的明文
  218. StringBuilder sb1=new StringBuilder();
  219. sb1.append("<request>");
  220. sb1.append("<command>register</command>");
  221. sb1.append("<username>诸葛亮</username>");
  222. sb1.append("<password>123456</password>");
  223. sb1.append("</request>");
  224. String plainText=sb1.toString();
  225. String request="";
  226. try{
  227. // 加密过程
  228. IMMsgEncrypter encrypter=new IMMsgEncrypter(plainText,Base64.encodeBase64String(serverPublicKey),rsaCoder,aesCoder);
  229. request=encrypter.getCipheredMsg();
  230. }
  231. catch(IMMsgEncryptException e){
  232. System.out.println("无法对客户端请求进行加密");
  233. }
  234. out.print(request);
  235. out.flush();
  236. s.shutdownOutput();// 输出结束
  237. // 输入
  238. Scanner in=new Scanner(inStram);
  239. StringBuilder sb=new StringBuilder();
  240. while(in.hasNextLine()){
  241. String line=in.nextLine();
  242. sb.append(line);
  243. }
  244. String response=sb.toString();
  245. s.close();
  246. try{
  247. // 解密过程
  248. IMMsgDecrypter decrypter=new IMMsgDecrypter(response,rsaCoder,aesCoder);
  249. String plainResponse=decrypter.getPlainMsg();
  250. System.out.println("plainResponse="+plainResponse);
  251. }catch(IMMsgDecryptException e){
  252. System.out.println("无法对服务器端响应进行解密");
  253. }
  254. }
  255. public static byte[] getPublicKey() throws Exception{
  256. Socket s=new Socket("127.0.0.1",8888);
  257. InputStream inStram=s.getInputStream();
  258. OutputStream outStream=s.getOutputStream();
  259. // 输出
  260. PrintWriter out=new PrintWriter(outStream,true);
  261. out.print("getPublicKey");
  262. out.flush();
  263. s.shutdownOutput();// 输出结束
  264. // 输入
  265. Scanner in=new Scanner(inStram);
  266. StringBuilder sb=new StringBuilder();
  267. while(in.hasNextLine()){
  268. String line=in.nextLine();
  269. sb.append(line);
  270. }
  271. String response=sb.toString();
  272. byte[] arr=Base64.decodeBase64(response);
  273. s.close();
  274. return arr;
  275. }
  276. }