西安邮电学院 通信软件设计_第1页
西安邮电学院 通信软件设计_第2页
西安邮电学院 通信软件设计_第3页
西安邮电学院 通信软件设计_第4页
西安邮电学院 通信软件设计_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

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

2、-“频道”内显示。6 如果没有输入IP地址便发送编辑完的文本消息,空白区域内会显示出提示信息提示用户应输入接受方的IP地址。7 当频道内显示的消息太多时,可以按”刷新频道”来清除频道内全部信息!软件运行时候的抓图:本机测试结果的抓图效果程序代码与简要说明1 chatFrame类的代码:package chat;import java.io.*;import java.awt.*;/* * Title: * Description: * Copyright: Copyright (c) 2021 * Company: * author not attributable * version 1.0

3、 */public class chatFrame extends JFrame JPanel contentPane; Label label1 = new Label(); TextField textField1 = new TextField(); Label label2 = new Label(); TextField textField2 = new TextField(); Button button1 = new Button(); TextArea textArea1 = new TextArea(); DatagramPacket sendpacket,receivepa

4、cket; /定义发送和接受数据包 DatagramSocket sendsocket,receivesocket;/定义发送和接受DatagramSocket /Construct the frame public chatFrame() enableEvents(AWTEvent.WINDOW_EVENT_MASK); try jbInit(); catch(Exception e) e.printStackTrace(); /Component initialization private void jbInit() throws Exception /定义个组件和他们的布局 conte

5、ntPane = (JPanel) this.getContentPane(); label2.setAlignment(Label.CENTER); label2.setForeground(Color.black); label2.setText(消息编辑框); label1.setBounds(new Rectangle(14, 17, 133, 32); contentPane.setLayout(null); this.setSize(new Dimension(500, 400); this.setTitle(用UDP实现聊天); textField1.setBackground(

6、SystemColor.text); textField1.setColumns(0); textField1.setText(); textField1.setBounds(new Rectangle(129, 72, 200, 61); label1.setAlignment(Label.CENTER); label1.setText(输入对方IP地址); label2.setBounds(new Rectangle(23, 86, 108, 28); try sendsocket=new DatagramSocket(5000); /初始化 receivesocket=new Datag

7、ramSocket(5001); catch(SocketException se) /可能产生Socketexception类的异常 se.printStackTrace(); System.exit(0); button1.setBackground(Color.magenta); button1.setLabel(发送消息); button1.setBounds(new Rectangle(374, 23, 88, 42); public void actionPerformed(ActionEvent e) button1_actionPerformed(e); ); textFiel

8、d2.setBackground(SystemColor.activeCaptionBorder); textField2.setText(); textField2.setBounds(new Rectangle(166, 21, 169, 29); textArea1.setText(); textArea1.setBounds(new Rectangle(46, 157, 382, 185); contentPane.setBackground(new Color(113, 111, 159); jButton1.setBackground(Color.pink); jButton1.s

9、etBounds(new Rectangle(374, 86, 87, 42); jButton1.setText(刷新频道); 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, n

10、ull); contentPane.add(jButton1, null); contentPane.add(button1, null); /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() /这个方法完成接受数据包的

11、功能 while(true) try bytearray=new byte100; receivepacket=new DatagramPacket(array,array.length); /创建接受数据包的缓冲 receivesocket.receive(receivepacket); /接受数据包 textArea1.append(nfrom +receivepacket.getAddress() + : ); byte data=receivepacket.getData(); String received=new String(data,0); /将字节数组转化为字符串 textA

12、rea1.append(received); catch(IOException se) /可能产生的IOException类异常 textArea1.append(se.toString() +/n); se.printStackTrace(); void button1_actionPerformed(ActionEvent e) /这个方法完成发送数据包 String str = textField2.getText(); if(pareTo()!=0) try textArea1.append(nto +textField2.getText() + : +textField1.getT

13、ext(); String 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

14、.printStackTrace(); else textArea1.setText(please input your friends IP first!); JButton jButton1 = new JButton(); void jButton1_actionPerformed(ActionEvent e) textArea1.setText(); 2.chatapp类的代码:package chat;/* * Title: * Description: * Copyright: Copyright (c) 2021 * Company: * author not attributa

15、ble * version 1.0 */public class chatApp boolean packFrame = false; /Construct the application public chatApp() chatFrame frame = new chatFrame(); /Validate frames that have preset sizes /Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) frame.pack(); else frame

16、.validate(); /Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height screenSize.height) frameSize.height = screenSize.height; if (frameSize.width screenSize.width) frameSize.width = screenSize.width; frame.setLocation(screenSize.width - frameSize.width) / 2, (screenSize.he

温馨提示

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

评论

0/150

提交评论