Java自定义协议进行socket连接的简单认证和消息.doc_第1页
Java自定义协议进行socket连接的简单认证和消息.doc_第2页
Java自定义协议进行socket连接的简单认证和消息.doc_第3页
Java自定义协议进行socket连接的简单认证和消息.doc_第4页
Java自定义协议进行socket连接的简单认证和消息.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java自定义协议进行socket连接的简单认证和消息 1、 服务器端的几次连接认证和状态的改变如上图所示: 1、服务器端进行协议认证的关键代码: public static String processInput(String theInput) String theOutput = null; if (state = WAITING) theOutput = waiting; state = 1; else if (state = 1) if (theInput.equalsIgnoreCase(hello!) theOutput = step1.; state = 2; else theOutput = Sorry, you are wrong. Please try again.; state = 0; else if (state = 2) if (theInput.equalsIgnoreCase(peng_zhanxuan) theOutput = step2; state = 3; else theOutput = error!; state = 0; else if (state = 3) if (theInput.equalsIgnoreCase(try!) theOutput = success!; state = 4; else theOutput = error!; state = 0; return theOutput; 2、客户端进行请求连接的关键代码: s = new Socket(,2010); InputStreamReader isr=new InputStreamReader(s.getInputStream(); BufferedReader br=new BufferedReader(isr); PrintWriter pw=new PrintWriter(s.getOutputStream(),true); String receive = null; pw.println(start); receive = br.readLine(); jta.append(来自服务器的验证消息: + receive + n); pw.println(hello!); receive = br.readLine(); jta.append(来自服务器的验证消息: + receive + n); pw.println(peng_zhanxuan); receive = br.readLine(); jta.append(来自服务器的验证消息: + receive + n); pw.println(try!); receive = br.readLine(); jta.append(来自服务器的验证消息: + receive + n); /如果验证成功则开启线程 if(receive.equals(success!) ClientThread ct = new ClientThread(s,jta); ct.start(); jta.append(连接成功! + n); else isr.close(); pw.close(); br.close(); s.close(); jta.append(服务器端认证失败! + n); 3、服务器端接收连接认证分析的代码: if(ConnProtocol.getState() != 4) System.out.println(=连接认证=); String outMes = ConnPcessInput(info); pw.println(outMes); if(ConnProtocol.getState() = 0) break; # 2、使用自定义协议封装的消息结构如上图所示: 1、客户端发送消息封装信息的关键代码: String version = v1.0; String author = pzx; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(yyyy-mm-dd hh:mm:ss); Date date = new Date(); String time = simpleDateFormat.format(date); String end = end; if(e.getSource()=jb) String info=jtf.getText(); /把客户端发送的信息显示到jta jta.append(客户机对服务器说:rn+ +info+rn); /在这里进行对数据的封装 String message = version + author + time + info + end; PrintWriter pw;2、服务器解析接收到的信息的关键代码:else /在这里进行协议解析 System.out.println(=协议解析=); Protocol pro = new Protocol(); String result = pro.analyzing(info); if(result = pzx ) if(pro.getMessage_text().equals(bye) /服务器端转发给客户端socket关闭中,也可不转发直接在接受到消息前客户端自动关闭,也不需要第二步了 pw.println(ok); pw.close(); br.close(); isr.close(); ss.close(); break; /退出服务器的接收循环,但还在等待客

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论