西安郵電學(xué)院 通信軟件設(shè)計(jì)_第1頁
西安郵電學(xué)院 通信軟件設(shè)計(jì)_第2頁
西安郵電學(xué)院 通信軟件設(shè)計(jì)_第3頁
西安郵電學(xué)院 通信軟件設(shè)計(jì)_第4頁
西安郵電學(xué)院 通信軟件設(shè)計(jì)_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、最新 精品 Word 歡迎下載 可修改西 安 郵 電 學(xué) 院實(shí) 驗(yàn) 報(bào) 告課 程 通信軟件設(shè)計(jì) 開課時(shí)間 至學(xué) 年第 學(xué)期這個(gè)軟件主要功能是以UDP為基礎(chǔ)實(shí)現(xiàn)兩臺(tái)電腦之間收發(fā)簡單文本消息。軟件全部使用java編寫,并以JavaBuilder8為工具進(jìn)行編寫與調(diào)試。布局方式采用NULL方式,主要使用AWT的組件和部分SWING的組件。使用方法如下:1 首先在輸入IP地址的輸入欄中輸入想與之進(jìn)行通信的主機(jī)的IP地址,即消息接受方的IP地址。2 在消息編輯框中輸入想發(fā)送的消息內(nèi)容。3 編輯完欲發(fā)送的消息后,點(diǎn)擊“發(fā)送消息”按鈕進(jìn)行發(fā)送;4 雙方發(fā)送的消息和發(fā)送方與接受方的IP地址都會(huì)在下面的空白區(qū)域

2、-“頻道”內(nèi)顯示。6 如果沒有輸入IP地址便發(fā)送編輯完的文本消息,空白區(qū)域內(nèi)會(huì)顯示出提示信息提示用戶應(yīng)輸入接受方的IP地址。7 當(dāng)頻道內(nèi)顯示的消息太多時(shí),可以按”刷新頻道”來清除頻道內(nèi)全部信息!軟件運(yùn)行時(shí)候的抓圖:本機(jī)測試結(jié)果的抓圖效果程序代碼與簡要說明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; /定義發(fā)送和接受數(shù)據(jù)包 DatagramSocket sendsocket,receivesocket;/定義發(fā)送和接受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 /定義個(gè)組件和他們的布局 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實(shí)現(xiàn)聊天); textField1.setBackground(

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

7、ramSocket(5001); catch(SocketException se) /可能產(chǎn)生Socketexception類的異常 se.printStackTrace(); System.exit(0); button1.setBackground(Color.magenta); button1.setLabel(發(fā)送消息); 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() /這個(gè)方法完成接受數(shù)據(jù)包的

11、功能 while(true) try bytearray=new byte100; receivepacket=new DatagramPacket(array,array.length); /創(chuàng)建接受數(shù)據(jù)包的緩沖 receivesocket.receive(receivepacket); /接受數(shù)據(jù)包 textArea1.append(nfrom +receivepacket.getAddress() + : ); byte data=receivepacket.getData(); String received=new String(data,0); /將字節(jié)數(shù)組轉(zhuǎn)化為字符串 textA

12、rea1.append(received); catch(IOException se) /可能產(chǎn)生的IOException類異常 textArea1.append(se.toString() +/n); se.printStackTrace(); void button1_actionPerformed(ActionEvent e) /這個(gè)方法完成發(fā)送數(shù)據(jù)包 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); /創(chuàng)建發(fā)送的數(shù)據(jù)包 sendsocket.send(sendpacket); /發(fā)送數(shù)據(jù)包 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論