版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 店鋪?zhàn)赓U(出租)意向協(xié)議書
- 2025年度高端摩托車租賃及保養(yǎng)服務(wù)合同2篇
- 2025版?zhèn)€人入股合作協(xié)議書:互聯(lián)網(wǎng)公司股權(quán)分配合同4篇
- 2025年度個(gè)人消費(fèi)分期付款合同標(biāo)準(zhǔn)7篇
- 2025-2030全球石墨氮化碳行業(yè)調(diào)研及趨勢分析報(bào)告
- 2025-2030全球封離型CO2激光器冷水機(jī)行業(yè)調(diào)研及趨勢分析報(bào)告
- 2025年全球及中國鼻炎光療儀行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報(bào)告
- 2025年全球及中國常壓等離子體裝置行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報(bào)告
- 2025年度國際貨運(yùn)代理及物流服務(wù)合同
- 商家聯(lián)盟協(xié)議書
- 江蘇省蘇州市2024-2025學(xué)年高三上學(xué)期1月期末生物試題(有答案)
- 銷售與銷售目標(biāo)管理制度
- 人教版(2025新版)七年級(jí)下冊英語:寒假課內(nèi)預(yù)習(xí)重點(diǎn)知識(shí)默寫練習(xí)
- 2024年食品行業(yè)員工勞動(dòng)合同標(biāo)準(zhǔn)文本
- 2025年第一次工地開工會(huì)議主要議程開工大吉模板
- 糖尿病高滲昏迷指南
- 全屋整裝售后保修合同模板
- 壁壘加筑未來可期:2024年短保面包行業(yè)白皮書
- 高中生物學(xué)科學(xué)推理能力測試
- GB/T 44423-2024近紅外腦功能康復(fù)評(píng)估設(shè)備通用要求
- 2024-2030年中國減肥行業(yè)市場發(fā)展分析及發(fā)展趨勢與投資研究報(bào)告
評(píng)論
0/150
提交評(píng)論