


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
9/9《Java技术》实验报告—实验(三)
实验室:年月日学院计算机与信息学院专业班级计算机科学与技术13-3姓名徐麟学号2013211695实验名称基于GUI的网络通信程序设计指导
教师路强教师评语教师签名:年月日一、实验目的 1.掌握Java中GUI程序的编写,包括事件监听机制。 2.掌握Java的网络通信编程,ServerSocket,Socket类的使用。 3.掌握Java中多线程的编程,Thread类,Runnable接口的使用。 4.掌握用面向对象的方法分析和解决复杂问题。二、实验原理三、使用硬件、软件环境PC计算机一台,配置为CPU为2.6G,内存为4G,硬盘为500G,安装Windows8.1操作系统。另外,使用myeclipise,JDK1.8.0等软件四、实验过程、步骤及原始记录(算法、原程序、测试结果,分析等)实验内容:编写程序完成以下功能:设计一个基于GUI的客户-服务器的通信应用程序,如图1,图2所示。图1Socket通信服务器端界面图2Socket通信客户端界面图1为Socket通信服务器端界面,点击该界面中的【Start】按钮,启动服务器监听服务(在图1界面中间的多行文本区域显示“Serverstarting…”字样)。图2为Socket通信客户端界面,点击该界面中的【Connect】按钮与服务器建立链接,并在图2所示界面中间的多行文本区域显示“Connecttoserver…”字样,当服务器端监听到客户端的连接后,在图1界面中间的多行文本区域追加一行“Clientconnected…”字样,并与客户端建立Socket连接。当图1所示的服务器端和图2所示的客户机端建立Socket连接后,编程实现这两端的数据通信,每次将接收到对方的数据追加显示在多行文本框中。源代码管理器importjava.io.*;.*;importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;publicclassServerUIextendsJFrame{ JTextAreamainArea; JTextFieldsendArea; JTextFieldindexArea; SvrComserver; publicvoidsetServer(SvrComserver){ this.server=server; } publicServerUI(){ super("服务器"); Containercontain=getContentPane(); contain.setLayout(newBorderLayout()); mainArea=newJTextArea(10,30); JScrollPanemainAreaP=newJScrollPane(mainArea); JPanelpanel=newJPanel(); JPanelmyPanel=newJPanel(); JLabelmyLabel2=newJLabel("Port:",SwingConstants.LEFT); JTextFieldmyField1=newJTextField(30); JButtonbutton1=newJButton("Start"); button1.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEventae){ server.sendMsg("PORT:6666"); mainArea.append("Serverstarting>>>"+"\n"); sendArea.setText(""); } }); myField1.setText("6666"); myPanel.add(myLabel2); myPanel.add(myField1); myPanel.add(button1); panel.setLayout(newBorderLayout()); sendArea=newJTextField(25); JButtonsendBtn=newJButton("Say:"); sendBtn.addActionListener(newActionListener()//注册动作监听器 { publicvoidactionPerformed(ActionEventae){ server.sendMsg(sendArea.getText());//把信息传递到客户端 mainArea.append("【Server】"+sendArea.getText()+"\n");//把信息显示在服务器的聊天记录区域 sendArea.setText(""); } }); panel.add(newJLabel("Say:"),BorderLayout.WEST); panel.add(sendBtn,BorderLayout.EAST); panel.add(sendArea,BorderLayout.CENTER); contain.add(myPanel,BorderLayout.NORTH); contain.add(mainAreaP,BorderLayout.CENTER); contain.add(panel,BorderLayout.SOUTH); setSize(500,300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); } publicstaticvoidmain(String[]args){ ServerUIui=newServerUI(); SvrComserver=newSvrCom(ui);//创建并启动网络通讯线程,准备接受客户端数据包 }}/***通讯类SvrCom负责守候数据到来*/classSvrComextendsThread//网络通讯类{ Socketclient; ServerSocketsoc; BufferedReaderin; PrintWriterout; ServerUIui; //ChatServerserver; publicSvrCom(ServerUIui){//初始化SvrCom类 this.ui=ui; ui.setServer(this); try{ soc=newServerSocket(6666);//开设服务器端口6666 System.out.println("启动服务器成功,等待端口号:6666"); client=soc.accept();//当客户机请求连接时,创建一条链接 System.out.println("连接成功!来自"+client.toString()); in=newBufferedReader(newInputStreamReader(client .getInputStream())); out=newPrintWriter(client.getOutputStream(),true); }catch(Exceptionex){ System.out.println(ex); } start(); } publicvoidrun(){//用于监听客户端发送来的信息 Stringmsg=""; while(true){ try{ msg=in.readLine();//从in对象上读数据信息 }catch(SocketExceptionex){ System.out.println(ex); break; }catch(Exceptionex){ System.out.println(ex); } if(msg!=null&&msg.trim()!=""){ System.out.println(">>"+msg); ui.mainArea.append(msg+"\n"); } } } publicvoidsendMsg(Stringmsg){//用于发送信息 try{ out.println("【Server】"+msg);//把信息写入输出流 }catch(Exceptione){ System.out.println(e); } }}客户端.*;importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;importjava.io.*;publicclassClientUIextendsJFrame{ JTextAreamainArea; JTextFieldsendArea; ChatClientclient; JTextFieldipArea; JTextFieldportArea; JButtonbtnLink; publicvoidsetClient(ChatClientclient){ this.client=client; } publicClientUI(){ super("客户端"); Containercontain=getContentPane(); contain.setLayout(newBorderLayout()); mainArea=newJTextArea(10,30); JScrollPanemainAreaP=newJScrollPane(mainArea); JPanelpanel=newJPanel(); panel.setLayout(newBorderLayout()); sendArea=newJTextField(25); JButtonsendBtn=newJButton("发送"); sendBtn.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventae){ client.sendMsg(sendArea.getText()); mainArea.append("【CLient】"+sendArea.getText()+"\n"); sendArea.setText(""); } }); JPanelipPanel=newJPanel(); ipPanel.setLayout(newFlowLayout(FlowLayout.LEFT,10,10)); ipPanel.add(newJLabel("ServerIP:")); ipArea=newJTextField(15); ipArea.setText(""); ipPanel.add(ipArea); ipPanel.add(newJLabel("Port:")); portArea=newJTextField(8); ipPanel.add(portArea); btnLink=newJButton("Connection!"); ipPanel.add(btnLink); btnLink.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventae){ Stringport=portArea.getText(); if(port.equals("6666")){ mainArea.append("ConnectiontoServer!"+"\n"); client=newChatClient(ipArea.getText(),6666,ClientUI.this); ClientUI.this.setClient(client); } else{ mainArea.append("PortError!!!"); } } }); panel.add(newJLabel("Say:"),BorderLayout.WEST); panel.add(sendBtn,BorderLayout.EAST); panel.add(sendArea,BorderLayout.CENTER); contain.add(ipPanel,BorderLayout.NORTH); contain.add(mainAreaP,BorderLayout.CENTER); contain.add(panel,BorderLayout.SOUTH); setSize(500,300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); } publicstaticvoidmain(String[]args){ ClientUIui=newClientUI(); }}classChatClientextendsThread{ Socketsc; BufferedReaderin; PrintWriterout; ClientUIui; publicChatClient(Stringip,intport,ClientUIui){ this.ui=ui; try{ sc=newSocket(ip,port); out=newPrintWriter(sc.getOutputStream(),true); in=newBuff
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 急诊科医师岗位轮换培训计划
- 五年级上册语文知识点系统复习计划
- 2025版汽车维修中心员工劳动合同规范
- 2025版酒店客房设施维修保养及售后服务协议
- 2025版健康医疗居间服务合同
- 2025版山场租赁与农村生态旅游合同
- 2025年度市政工程投标担保书模板合同范本
- 2025届高三应试能力提升计划
- 2025版三旧改造工程土地收购合同范本
- 2025版二手房佣金佣金收益分配调整协议
- 迎中秋庆国庆主题班会
- 初高中衔接数学教学的心得
- 2023-2024学年湖南省耒阳市小学语文六年级下册期末自测测试题
- 12YJ4-1 常用门窗标准图集
- GB/T 12190-1990高性能屏蔽室屏蔽效能的测量方法
- 高血压的危害-课件
- ISO15189医学实验室认可概况课件
- 轻钢龙骨、双层石膏板吊顶施工方案
- 安全网(平网)张挂安全技术要求
- 危险品管理台帐
- 政务云收费标准 云托管收费标准
评论
0/150
提交评论