java聊天室实验报告 网络编程技术实验报告_第1页
java聊天室实验报告 网络编程技术实验报告_第2页
java聊天室实验报告 网络编程技术实验报告_第3页
java聊天室实验报告 网络编程技术实验报告_第4页
java聊天室实验报告 网络编程技术实验报告_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

网络编程技术

大作业项目开董海阳开发人2009-03开发人2009082303开发人专软件工发人班级学号业程项目名称基于Java的TCP—对一聊天程序成绩(70%)网络编程技术大作业版本:0.1.0网络编程技术大作业版本:0.1.0第#页5.1.2通信时序图客户通过Socket建立与服务器的连接。服务器与客户都通过构造objectInputStream,objectoutputStream来建立输入输出流,然后双方通过该输入输出流来相互传递信息,一旦收到客户方的连接请求,服务器accept()方法返回一个新建的Socket对象。客户端然后向服务器发送消息,如图所示:客户端侦听服务器发送请求客户端侦听服务器建立连接消息通信5.2用户接口模块客户端及服务器端核心代码,界面设计及模块调用如下:publicclassOne2OneChat{publicstaticvoidmain(Stringargs[]){LoginFramelf=newLoginFrame("输入目标");lf.show();}}/////////////////////////////////////////////////////////登录界面输入IP地址及端口classLoginFrameextendsJFrame{JButtonBOK;JLabelLdesAddr,Lport;JTextFieldTFdesAddr,TFport;StringdesAddr,port;LoginFrame(){}LoginFrame(Stringtitle){super(title);Framet=this;BOK=newJButton("确定");LdesAddr=newJLabel("目标IP");Lport=newJLabel("端口J;TFdesAddr=newJTextField(desAddr,12);TFport=newJTextField(port,12);//TFpassword.setEchoChar('*');BOK.addActionListener(newBOKListener(t));setBackground(Color.black);setBounds(350,250,200,128);setLayout(newFlowLayout(FlowLayout.CENTER,5,7));add(LdesAddr);add(TFdesAddr);add(Lport);add(TFport);add(BOK);setResizable(false);//setVisible(true);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}//建立监听classBOKListenerimplementsActionListener{Framet;BOKListener(){}BOKListener(Framet){this.t=t;}publicvoidactionPerformed(ActionEvente){desAddr=TFdesAddr.getText();port=TFport.getText();t.setVisible(false);Messengerm=newMessenger(desAddr,port);m.start();}}}/////////////////////////////////////////////////////classMessengerextendsThread{StringdesAddr;Stringport;intiport;TextAreacontent,send;JButtonBsend;ChatFramecf;Stringtitle;Socketclient;ServerSocketss;OutputStreamWriterosw=null;InputStreamReaderisr=null;BufferedReaderbr;Stringline;booleanflag;//端口号是否正确booleancbc;//canbeclient?inttryTurns=3;//客户方式尝试的次数Messenger(){}Messenger(StringdesAddr,Stringport){super("LANChatMessenger");content=newTextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);send=newTextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);Bsend=newJButton("发送");Bsend.setEnabled(false);title="与董海阳的机器人《====="+desAddr+"=====》聊天";flag=true;cbc=true;cf=newChatFrame(title,content,send,Bsend);cf.show();this.desAddr=desAddr;this.port=port;try{iport=Integer.parseInt(port);}catch(NumberFormatExceptionnfe){content.append("非法的端口,程序4秒后关闭。\n");flag=false;}}publicvoidrun(){if(!flag){try{Thread.sleep(4000);}catch(InterruptedExceptionie){System.exit(0);}System.exit(0);}content.append("正在以客户端方式进行连接\n");do{cbc=true;try{client=newSocket(desAddr,iport);}catch(Exceptione){content.append("错误,无法连接地址:"+desAddr+":"+port+'\n');content.append("等待1秒再连接,剩余["+(tryTurns-1)+"]次。\n");cbc=false;try{Thread.sleep(1000);}catch(InterruptedExceptionie){System.exit(0);}}--tryTurns;}while((tryTurns>0)&&!cbc);if(cbc){content.append("连接成功,可以开始了。"+"\n\n");cf.setTitle("与董海阳的机器人《====="+desAddr+":"+port+"=====》聊天");//"与董海阳的机器人《====="+desAddr+"=====》聊天";send.requestFocus();}else{content.append("客户端方式失败,现在启动服务器并等待连接。"+'\n');try{ss=newServerSocket(iport);}catch(IOExceptionioe){content.append("\n无法创建服务,程序将在4秒后退出。\n");try{Thread.sleep(4000);}catch(InterruptedExceptionie){System.exit(0);}System.exit(0);}try{cf.setTitle("等待连接中在端口:"+port);client=ss.accept();}catch(Exceptione){content.append("\nss.accept()方法失败,程序将在4秒后退出。\n");try{Thread.sleep(4000);}catch(InterruptedExceptionie){System.exit(0);}System.exit(0);}content.append("连接成功,可以开始。"+client.getInetAddress().toString()+"\n\n");cf.setTitle("与"+client.getInetAddress().toString()+":"+port+"聊天");send.requestFocus();}try{osw=newOutputStreamWriter(client.getOutputStream());isr=newInputStreamReader(client.getInputStream());br=newBufferedReader(isr);//content.append("\nbrcreated\n");}catch(IOExceptionioe){content.append("创建流错误,程序将在4秒后退出。\n");try{Thread.sleep(4000);}catch(InterruptedExceptionie){System.exit(0);}System.exit(0);}Bsend.setEnabled(true);Bsend.addActionListener(newBsendListener(content,send,osw));try{line=br.readLine();}catch(IOExceptionioe){content.append("流读取错误,程序将在4秒后退出。\n");try{Thread.sleep(4000);}catch(InterruptedExceptionie){System.exit(0);}System.exit(0);}while(true){content.append("他说:"+line+'\n');try{Thread.sleep(1000);}catch(InterruptedExceptionie){System.exit(0);}try{line=br.readLine();}catch(IOExceptionioe){content.append("流读取错误,程序将在4秒后退出。\n");try{Thread.sleep(4000);}catch(InterruptedExceptionie){System.exit(0);}System.exit(0);}}}classBsendListenerimplementsActionListener{TextAreacontent;TextAreasend;OutputStreamWriterosw;BsendListener(TextAreacontent,TextAreasend,OutputStreamWriterosw){this.content=content;this.send=send;this.osw=osw;}publicvoidactionPerformed(ActionEvente){Stringinput;input=send.getText();if(input.length()>0){content.append("董海阳说:"+input+'\n');try{osw.write(input+'\n',0,input.length()+1);osw.flush();//晕}catch(Exceptionee){content.append("不能发送\""+input+"\",发生了错误:"+ee.getMessage()+'\n');}send.setText("");send.requestFocus();}///////////////////////////////////////////////////////////////classChatFrameextendsJFrame{TextAreacontent;TextAreasend;JButtonBsend;ChatFrame(){}ChatFrame(StringframeTitle,TextAreacontent,TextAreasend,JButtonBsend){super(frameTitle);this.content=content;this.send=send;this.Bsend=Bsend;this.send.setEditable(true);setLayout(null);setBounds(300,100,510,500);add(content);content.setBackground(Color.WHITE);content.setBounds(2,0,500,340);content.setFocusable(true);add(send);send.setBounds(2,355,500,70);send.setFocusable(true);add(Bsend);Bsend.setBounds(410,432,60,30);setResizable(false);setDefaultCloseOperation(EXIT_ON_CLOSE);}}5.3类/接口/函数说明chatFrame():创建基本窗体LoginFrame..BOKListener(

温馨提示

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

评论

0/150

提交评论