西安邮电学院 通信软件设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

西安邮电学院实验报告课程通信软件设计开课时间至学年第学期这个软件主要功能是以UDP为基础实现两台电脑之间收发简单文本消息。软件全部使用java编写,并以JavaBuilder8为工具进行编写与调试。布局方式采用NULL方式,主要使用AWT的组件和部分SWING的组件。使用方法如下:1首先在输入IP地址的输入栏中输入想与之进行通信的主机的IP地址,即消息接受方的IP地址。2在消息编辑框中输入想发送的消息内容。3编辑完欲发送的消息后,点击“发送消息”按钮进行发送;4双方发送的消息和发送方与接受方的IP地址都会在下面的空白区域----“频道”内显示。6如果没有输入IP地址便发送编辑完的文本消息,空白区域内会显示出提示信息提示用户应输入接受方的IP地址。7当频道内显示的消息太多时,可以按”刷新频道”来清除频道内全部信息!软件运行时候的抓图:本机测试结果的抓图效果程序代码与简要说明1chatFrame类的代码:packagechat;importjava.net.*;importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;/***pTitle:/p*pDescription:/p*pCopyright:Copyright(c)2004/p*pCompany:/p*@authornotattributable*@version1.0*/publicclasschatFrameextendsJFrame{JPanelcontentPane;Labellabel1=newLabel();TextFieldtextField1=newTextField();Labellabel2=newLabel();TextFieldtextField2=newTextField();Buttonbutton1=newButton();TextAreatextArea1=newTextArea();DatagramPacketsendpacket,receivepacket;//定义发送和接受数据包DatagramSocketsendsocket,receivesocket;//定义发送和接受DatagramSocket//ConstructtheframepublicchatFrame(){enableEvents(AWTEvent.WINDOW_EVENT_MASK);try{jbInit();}catch(Exceptione){e.printStackTrace();}}//ComponentinitializationprivatevoidjbInit()throwsException{//定义个组件和他们的布局contentPane=(JPanel)this.getContentPane();label2.setAlignment(Label.CENTER);label2.setFont(newjava.awt.Font(SansSerif,0,15));label2.setForeground(Color.black);label2.setText(消息编辑框);label1.setBounds(newRectangle(14,17,133,32));contentPane.setLayout(null);this.setSize(newDimension(500,400));this.setTitle(用UDP实现聊天);textField1.setBackground(SystemColor.text);textField1.setColumns(0);textField1.setFont(newjava.awt.Font(Dialog,0,16));textField1.setText();textField1.setBounds(newRectangle(129,72,200,61));label1.setAlignment(Label.CENTER);label1.setFont(newjava.awt.Font(Dialog,0,15));label1.setText(输入对方IP地址);label2.setBounds(newRectangle(23,86,108,28));try{sendsocket=newDatagramSocket(5000);//初始化receivesocket=newDatagramSocket(5001);}catch(SocketExceptionse){//可能产生Socketexception类的异常se.printStackTrace();System.exit(0);}button1.setBackground(Color.magenta);button1.setFont(newjava.awt.Font(Dialog,0,13));button1.setLabel(发送消息);button1.setBounds(newRectangle(374,23,88,42));button1.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(ActionEvente){button1_actionPerformed(e);}});textField2.setBackground(SystemColor.activeCaptionBorder);textField2.setFont(newjava.awt.Font(Dialog,1,18));textField2.setLocale(java.util.Locale.getDefault());textField2.setText();textField2.setBounds(newRectangle(166,21,169,29));textArea1.setFont(newjava.awt.Font(Dialog,0,13));textArea1.setText();textArea1.setBounds(newRectangle(46,157,382,185));contentPane.setBackground(newColor(113,111,159));jButton1.setBackground(Color.pink);jButton1.setBounds(newRectangle(374,86,87,42));jButton1.setFont(newjava.awt.Font(Dialog,0,13));jButton1.setText(刷新频道);jButton1.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(ActionEvente){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);}//OverriddensowecanexitwhenwindowisclosedprotectedvoidprocessWindowEvent(WindowEvente){super.processWindowEvent(e);if(e.getID()==WindowEvent.WINDOW_CLOSING){System.exit(0);}}publicvoidwaitforpackets(){//这个方法完成接受数据包的功能while(true){try{byte[]array=newbyte[100];receivepacket=newDatagramPacket(array,array.length);//创建接受数据包的缓冲receivesocket.receive(receivepacket);//接受数据包textArea1.append(\nfrom+receivepacket.getAddress()+:);bytedata[]=receivepacket.getData();Stringreceived=newString(data,0);//将字节数组转化为字符串textArea1.append(received);}catch(IOExceptionse){//可能产生的IOException类异常textArea1.append(se.toString()+/n);se.printStackTrace();}}}voidbutton1_actionPerformed(ActionEvente){//这个方法完成发送数据包Stringstr=textField2.getText();if(str.compareTo()!=0){try{textArea1.append(\nto+textField2.getText()+:+textField1.getText());Strings=textField1.getText();bytedata[]=newbyte[100];s.getBytes(0,s.length(),data,0);sendpacket=newDatagramPacket(data,s.length(),InetAddress.getByName(textField2.getText()),5001);//创建发送的数据包sendsocket.send(sendpacket);//发送数据包}catch(IOExceptionexc){textArea1.append(exc.toString()+\n);exc.printStackTrace();}}elsetextArea1.setText(pleaseinputyourfriend'sIPfirst!);}JButtonjButton1=newJButton();voidjButton1_actionPerformed(ActionEvente){textArea1.setText();}}2.chatapp类的代码:packagechat;importjava.net.*;importjava.io.*;importjavax.swing.UIManager;importjava.awt.*;/***pTitle:/p*pDescription:/p*pCopyright:Copyright(c)2004/p*pCompany:/p*@authornotattributable*@version1.0*/publicclasschatApp{booleanpackFrame=false;//ConstructtheapplicationpublicchatApp(){chatFrameframe=newchatFrame();//Validateframesthathavepresetsizes//Packframesthathaveusefulpreferredsizeinfo,e.g.fromtheirlayoutif(packFrame){frame.pack();}else{frame.validate();}//CenterthewindowDimensionscreenSize=Toolkit.getDef

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功