java实验3技术实验报告模板_第1页
java实验3技术实验报告模板_第2页
java实验3技术实验报告模板_第3页
已阅读5页,还剩3页未读, 继续免费阅读

付费下载

下载本文档

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

文档简介

1、Java 技术实验实验(三):年月日学院计算机与信息学院专业班级计算机科学与技术 13-3 班学号2013211700实验名称基于 GUI 的网络通信程序设计指导教师教师评语教师签名:年月日一、实验目的掌握 Java 中 GUI 程序的编写,包括事件 机制。掌握 Java 的网络通信编程,ServerSocket,Socket 类的使用。 3掌握 Java 中多线程的编程,Thread 类,Runnable 接口的使用。 4掌握用面 象的方法分析和解决复杂问题。二、实验原理三、使用硬件、环境PC 计算机一台,配置为 CPU 为 1.5G,内存为 4GB,硬盘为 128G,安装 Windows7

2、 操作系统。另外,使用 JCreator,JDK1.6.0 等四、实验过程、步骤及原始(算法、原程序、,分析等)1 安装 Jdk1.5.0 集成环境,安装成功后,配置 path,classpath 路径,让用户在任何目录下均可用到 Java 的系统资源。2 源代码:服务器:import import import importimportjava.io.*; .*; javax.swing.*; java.awt.event.*;java.awt.*;publicclass ServerUI extendsJFrameJTextArea mainArea; JTextField sendAre

3、a; JTextField indexArea; server;public void setServer( this.server = server;public ServerUI() super(服务器);server)Container contain = getContentPane(); contain.setLayout(new BorderLayout(); mainArea = new JTextArea(10,30);JScrollPane mainAreaP = new JScrollPane(mainArea);JPanel JPanelJLabelpanel = new

4、 JPanel(); myPanel=new JPanel();myLabel2=new JLabel(Port:,SwingConstants.LEFT);JTextField myField1=new JTextField(30); JButton button1=new JButton(Start); button1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ae) server.sendMsg(PORT:1234); mainArea.append(Server start

5、ing + n); sendArea.setText(););myField1.setText(1234); myPanel.add(myLabel2); myPanel.add(myField1); myPanel.add(button1); panel.setLayout(new BorderLayout(); sendArea = new JTextField(25); JButton sendBtn = new JButton(Say:);sendBtn.addActionListener(new ActionListener()/动作器public void actionPerfor

6、med(ActionEvent ae) server.sendMsg(sendArea.getText();/把信息传递到客户端mainArea.append(【Server】 + sendArea.getText()+n);/把信息显示在服务器的 sendArea.setText();区域);panel.add(new JLabel(Say:),BorderLayout.WEST);panel.add(sendBtn, BorderLayout.EAST);panel.add(sendArea, BorderLayoENTER);contain.add(myPanel, BorderLayo

7、ut.NORTH);contain.add(mainAreaP, BorderLayoENTER);contain.add(panel,BorderLayout.SOUTH); setSize(500, 300);setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);pack();public sic void main(String args)ServerUI ui = new ServerUI();server = new(ui);/创建并启动网络通讯线程,准备接受客户端 数据包/* 通讯类*/ class/网络通

8、讯类Socket c负责守候数据到来extends Threadnt;ServerSocket soc;BufferedReader in;PrWriter out;ServerUI ui;/ ChatServer server;public(ServerUI ui)类初始化this.ui = ui; ui.setServer(this); trysoc = new ServerSocket(1234);/ 开设服务器端口 1234System.out.prln(启动服务器成功,等待端:1234);cnt = soc.accept();/当客户机请求连接时,创建一条System.out.prl

9、n(连接成功!来自 + cnt.toString();in = new BufferedReader(new InputStreamReader(cnt.getInputStream();out = new PrWriter(cnt.getOutputStream(),catch (Exception ex)true);System.out.prln(ex);start();public void run()/用于客户端发送来的信息String msg = ; while (true)trymsg = in.readLine();/从 in 对象上读数据信息catch (SocketExcep

10、tion ex)System.out.prbreak;ln(ex);catch (Exception ex)System.out.prln(ex);if (msg != null & System.out.prmsg.trim()ln( +!= )msg);ui.mainArea.append(msg + n);public void sendMsg(String msg)/用于发送信息 tryout.prln(【Server】 + msg);/把信息写入输出流catch (Exception e) System.out.prln(e);客户端:import import import imp

11、ort importpublicjava.io.*; .*; javax.swing.*; java.awt.event.*;java.awt.*;class CntUI extendsJFrameJTextArea mainArea;JTextFieldsendArea;ChatCntcnt;JTextFieldJTextFieldipArea;portArea;JButton btnLink;public void setCnt(ChatC t = cnt;nt cnt)public CntUI()super(客户端);Container contain = getContentPane(

12、); contain.setLayout(new BorderLayout(); mainArea = new JTextArea(10,30);JScrollPane mainAreaP = new JScrollPane(mainArea); JPanel panel = new JPanel();panel.setLayout(new BorderLayout(); sendArea = new JTextField(25); JButton sendBtn = new JButton(发送);sendBtn.addActionListener(new ActionListener()p

13、ublic void actionPerformed(ActionEvent ae)cndMsg(sendArea.getText();mainArea.append(【Cnt】 + sendArea.getText()sendArea.setText();+ n););JPanel ipPanel = new JPanel();ipPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10); ipPanel.add(new JLabel(Server IP:);ipArea = new JTextField(15);ipArea.setTe

14、xt(127.0.0.1); ipPanel.add(ipArea); ipPanel.add(new JLabel(Port:); portArea =new JTextField(8); ipPanel.add(portArea);btnLink = new JButton(Connection!); ipPanel.add(btnLink); btnLink.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ae) String port=portArea.getText();if(

15、 port.equals(1234)mainArea.append(Connection to Server!+n);cnt = newChatCnt(ipArea.getText(),1234,CntUI.this);CntUI.this.setCnt(cnt);elsemainArea.append(Port Error!););panel.add(new JLabel(Say:),BorderLayout.WEST); panel.add(sendBtn, BorderLayout.EAST);panel.add(sendArea, BorderLayoENTER);contain.ad

16、d(ipPanel, BorderLayout.NORTH);contain.add(mainAreaP, BorderLayoENTER);contain.add(panel, BorderLayout.SOUTH); setSize(500, 300);setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);pack();public sic void main(String args)CntUI ui = new CntUI();class ChatCSocket sc;nt extends Thread Buff

17、eredReader in;PrWriter out;CntUI ui;public ChatCthis.ui =nt(String ip,ui;port, CntUI ui)trysc = new Socket(ip, port);out = new PrWriter(sc.getOutputStream(), true);in = new BufferedReader(new InputStreamReader(sc.getInputStream();catch (Exception e) System.out.prln(e);start();public void run() String msg = ; while (true)trymsg = in.readLine();catch (SocketException ex) System.out.pr ln(ex); break;catch (Exception ex) System.out.prln(ex);if(msg != null & msg.trim() != ) System.out.prln( + msg); ui.mainArea.append(msg + n);public v

温馨提示

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

最新文档

评论

0/150

提交评论