西安邮电学院通信软件设计说明_第1页
西安邮电学院通信软件设计说明_第2页
西安邮电学院通信软件设计说明_第3页
免费预览已结束,剩余4页可下载查看

下载本文档

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

文档简介

1、西安邮电学院实验报告课 程开课时间至学年第学期这个软件主要功能是以UDP为基础实现两台电脑之间收发简单文本消息。软 件全部使用java编写,并以JavaBuilder8为工具进行编写与调试。布局方式采 用NULL方式,主要使用AWT勺组件和部分SWING勺组件。使用方法如下:1首先在输入IP地址的输入栏中输入想与之进行通信的主机的IP地址,即消息接受方的IP地址。2在消息编辑框中输入想发送的消息容。3编辑完欲发送的消息后,点击“发送消息”按钮进行发送;4双方发送的消息和发送方与接受方的IP地址都会在下面的空白区域-“频道”显示。6如果没有输入IP地址便发送编辑完的文本消息,空白区域会显示出提示

2、信 息提示用户应输入接受方的IP地址。7当频道显示的消息太多时,可以按”刷新频道”来清除频道全部信息!软件运行时候的抓图:Zi« Uhl twdhtr.jTi# 乩丹和g r 0. $A|J话如濡£i ,nt * 自知血* '-H cRiPd"I £M3 (Mg血 t - It/Jdiwt/ctatfc &-i |krapbhrin '|m| A*i¥TQltw |-匸鬥T鬥叮IT址M J:卜uiwXE本机测试结果的抓图效果flL* 14xi 4«* Jt 聲讯士 * tel|J.«j 4*欝1>

3、;卅.Rp口孕耳、暂爭I- x |a厂zJ备殆* 勻” s)卜” ifr 站”也 JO峯*# J "Ftta'cE jicuca"鼻曲冲"!4那仔tm 尊xix*=rLiiC-dJ.:r. eEkcLj ew: ±弓肯“心二qe-_'Ejbxt) rtiflRtf-lHEdJ. ra tEclivj-£D)rCMCFL«ld4. iKEhMiti S#«l> L*UscearFL 凶出.m-tTcKCf' | jn I* fc(4J-fcrrfw* fnm iMFignOBI苹.I口的4川1 i

4、 恃 pi tssxurin卜h-t-, i -卜 i cctftiiFwB1- L4vv1 hM«f2iiEWAmdiirlV祢如孔G laciArvHl'IghflpFie#iijrxLiL_n 卜 uipp程序代码与简要说明1 chatFrame类的代码:package chat;import java .n et.*;import java.io.*;import java.awt.*;import java.awt.eve nt.*;import javax.swi ng.*;* vp>Title: </p>* <p>Descripti

5、o n: v/p>* <p>Copyright: Copyright (c) 2004</p>* <p>Compa ny: v/p>* author not attributable* versio n 1.0*/public class chatFrame exte nds JFrame JPa nel conten tPa ne;Label label1 = new Label();TextField textField1 = new TextField();Label label2 = new Label();TextField textF

6、ield2 = new TextField();Button button1 = new Button();TextArea textArea1 = new TextArea();DatagramPacket sendpacket,receivepacket; / 定义发送和接受数据 包DatagramSocket sendsocket,receivesocket;/ 定义发送和接受 DatagramSocket/Construct the frame public chatFrame() enableEvents(AWTEvent.WINDOW_EVENT_MASK); try jbInit

7、(); catch(Exception e) e.printStackTrace();/Component initializationprivate void jbInit() throws Exception / 定义个组件和他 们的布局contentPane = (JPanel) this.getContentPane(); label2.setAlignment(Label.CENTER); label2.setFont(new java.awt.Font("SansSerif", 0, 15); label2.setForeground(Color.black);

8、label2.setText(" 消息编辑框 "); label1.setBounds(new Rectangle(14, 17, 133, 32); contentPane.setLayout(null);this.setSize(new Dimension(500, 400);this.setTitle(" 用 UDP实现聊天"); textField1.setBackground(SystemColor.text); textField1.setColumns(0);textField1.setFont(new java.awt.Font(&quo

9、t;Dialog", 0, 16); textField1.setText("");textField1.setBounds(new Rectangle(129, 72, 200, 61); label1.setAlignment(Label.CENTER);label1.setFont(new java.awt.Font("Dialog", 0, 15);label1.setText(" 输入对方 IP 地址"); label2.setBounds(new Rectangle(23, 86, 108, 28);try se

10、ndsocket=new DatagramSocket(5000); / 初始化receivesocket=new DatagramSocket(5001);catch(SocketException se) / 可能产生 Socketexception 类 的异常se.printStackTrace(); System.exit(0);button1.setBackground(Color.magenta); button1.setFont(new java.awt.Font("Dialog", 0, 13);button1.setLabel(" 发送消息 &q

11、uot;); button1.setBounds(new Rectangle(374, 23, 88, 42);button1.addActionListener(new java.awt.event.ActionListener () public void actionPerformed(ActionEvent e)button1_actionPerformed(e); );textField2.setBackground(SystemColor.activeCaptionBorder); textField2.setFont(new java.awt.Font("Dialog&

12、quot;, 1, 18); textField2.setLocale(java.util.Locale.getDefault();textField2.setText("");textField2.setBounds(new Rectangle(166, 21, 169, 29); textArea1.setFont(new java.awt.Font("Dialog", 0, 13); textArea1.setText("");textArea1.setBounds(new Rectangle(46, 157, 382, 185

13、); contentPane.setBackground(new Color(113, 111, 159); jButton1.setBackground(Color.pink);jButton1.setBounds(new Rectangle(374, 86, 87, 42); jButton1.setFont(new java.awt.Font("Dialog", 0, 13);jButton1.setText(" 刷新频道 "); jButton1.addActionListener(new java.awt.event.ActionListene

14、r()public void actionPerformed(ActionEvent e) jButton1_actionPerformed(e););contentPane.add(label1, null);contentPane.add(textField2, null);contentPane.add(textField1, null); contentPane.add(label2, null);contentPane.add(textArea1, null); contentPane.add(jButton1, null);contentPane.add(button1, null

15、);/Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) cessWindowEvent(e);if (e.getID() = WindowEvent.WINDOW_CLOSING) System.exit(0);public void waitforpackets() / / 这个方法完成接受数据包的功能 while(true)trybytearray=new byte100;receivepacket=new DatagramPac

16、ket(array,array.length);/ 创建接受数据包的缓冲 receivesocket.receive(receivepacket);/ 接受数据包 textArea1.append("nfrom "+receivepacket.getAddress() +" : ");byte data=receivepacket.getData();String received=new String(data,0);/ 将字节数组转化为字符串 textArea1.append(received);catch(IOException se)/ 可能产生

17、的 IOException 类异常 textArea1.append(se.toString() +"/n");se.printStackTrace();void button1_actionPerformed(ActionEvent e)/ 这个方法完成发送数据包String str = textField2.getText();if(pareTo("")!=0)trytextArea1.append("nto "+textField2.getText() +" "+textField1.getText();St

18、ring s=textField1.getText(); byte data=new byte100; s.getBytes(0,s.length(),data,0);sendpacket=new DatagramPacket(data,s.length(),InetAddress.getByName(textField2.getText(),5001);/ 创建发送的数据包sendsocket.send(sendpacket);/ 发送数据包 catch(IOException exc)textArea1.append(exc.toString() +"n");exc.p

19、rintStackTrace();elsetextArea1.setText("please input your friend's IP first!");JButton jButton1 = new JButton();void jButton1_actionPerformed(ActionEvent e) textArea1.setText("");2.chatapp 类的代码 :package chat;import .*;import java.io.*;import javax.swing.UIManager;import java.

20、awt.*;/* <p>Title: </p>* <p>Description: </p>* <p>Copyright: Copyright (c) 2004</p>* <p>Company: </p>* author not attributable* version 1.0*/public class chatApp boolean packFrame = false;/Construct the applicationpublic chatApp() chatFrame frame = new chatFrame();/Validate frames that have preset sizes/Pack frames that have useful preferred size info, e.

温馨提示

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

评论

0/150

提交评论