




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上Java程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告-實(shí)驗(yàn)3實(shí)驗(yàn)室: 2014 年 12 月 10 日學(xué)院計(jì)算機(jī)與信息學(xué)院專(zhuān)業(yè)班級(jí)姓名成績(jī)課程名稱(chēng)Java程序設(shè)計(jì)實(shí)驗(yàn)項(xiàng)目名 稱(chēng) 實(shí)驗(yàn)三 基于GUI的網(wǎng)絡(luò)通信程序設(shè)計(jì)指導(dǎo)教師教師評(píng)語(yǔ) 教師簽名: 年 月 日1、 實(shí)驗(yàn)?zāi)康?掌握J(rèn)ava中GUI程序的編寫(xiě),包括事件監(jiān)聽(tīng)機(jī)制。2掌握J(rèn)ava的網(wǎng)絡(luò)通信編程,ServerSocket,Socket類(lèi)的使用。3掌握J(rèn)ava中多線程的編程,Thread類(lèi),Runnable接口的使用。4掌握用面向?qū)ο蟮姆椒ǚ治龊徒鉀Q復(fù)雜問(wèn)題。2、 實(shí)驗(yàn)原理1. 利用java.awt和javax.swing包提供的各種組件實(shí)現(xiàn)服務(wù)
2、器和客戶端的界面設(shè)計(jì)。2使用套接字實(shí)現(xiàn)基于TCP協(xié)議的服務(wù)器和客戶端。3.為服務(wù)器和客戶端界面中的有關(guān)組件添加消息相應(yīng),實(shí)現(xiàn)交互。三、使用硬件、軟件環(huán)境 PC 計(jì)算機(jī)一臺(tái),配置為CPU為2.6G,內(nèi)存為4G,硬盤(pán)為1T,安裝Windows8操作系統(tǒng)。另外,使用JCreator,JDK1.8.0等軟件四、實(shí)驗(yàn)過(guò)程、步驟及原始記錄(算法、原程序、測(cè)試結(jié)果,分析等) 1.實(shí)驗(yàn)過(guò)程: 首先實(shí)現(xiàn)界面的編寫(xiě),之后使用套接字實(shí)現(xiàn)基于TCP協(xié)議的通信,再設(shè)置監(jiān)視 器,為相應(yīng)的組件添加消息相應(yīng)。 2.源程序:1.客戶端程序:KeHuDuan.javaimport java.awt.*;import java.a
3、wt.event.*;import java.util.*;import java.io.*;import .*;import javax.swing.*;public class KeHuDuanpublic static void main(String args)MyFrame client = new MyFrame();client.setVisible(true);client.setResizable(false);client.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);class MyFrame extends JFra
4、meJTextField ip;JTextField port;JTextField cin;JTextArea content;JButton connect;JButton say;Socket socket;MyFrame()init();ConnectListen cListener = new ConnectListen();SayListen sListener = new SayListen();connect.addActionListener(cListener);say.addActionListener(sListener);void init() setLayout(n
5、ew FlowLayout(); setSize(400,400); setLocation(800,100); add(new JLabel("Serve ip"); ip = new JTextField("127.0.0.1",8); add(ip); add(new JLabel("Serve port"); port = new JTextField("8888",8); add(port); connect = new JButton("connect"); add(connect)
6、; content = new JTextArea(16,35); JScrollPane scroll = new JScrollPane(content); add(scroll); add(new Label("Say:"); cin = new JTextField(25); add(cin); say = new JButton("say"); add(say);class ConnectListen implements ActionListenerint portNum;public void actionPerformed(ActionE
7、vent e)connect.setEnabled(false);try portNum =Integer.parseInt(port.getText();socket = new Socket(ip.getText(),portNum);ClientThread ct = new ClientThread();ct.start(); catch (Exception ex) class SayListen implements ActionListenerString str;public void actionPerformed(ActionEvent e)try PrintWriter
8、out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),true);str=cin.getText();if(!str.isEmpty()out.println(str);content.append("me:"+str+"n");out.flush();cin.setText(""); catch (Exception ex) class ClientThread extends Threadpublic voi
9、d run()try BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream();String str;while(true)str = in.readLine();/System.out.println ("a");content.append(str+"n"); catch (Exception ex) 2.服務(wù)器端程序:import java.awt.*;import java.awt.event.*;import java.util.*;
10、import java.io.*;import .*;import javax.swing.*;public class FuWuQipublic static void main(String args)MyFrame serve = new MyFrame();serve.setVisible(true);serve.setResizable(false);serve.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);class MyFrame extends JFrameJTextField port;JButton start;JTex
11、tArea content;JTextField cin;JButton say;Socket socket;MyFrame()init();StartListen sListen = new StartListen();SayListen stListen = new SayListen();start.addActionListener(sListen);say.addActionListener(stListen);void init() setLayout(new FlowLayout();setSize(400,400);setLocation(400,100);add(new JL
12、abel("Port:");port = new JTextField("8888",25);add(port);start = new JButton("Start");add(start);content = new JTextArea(15,35);JScrollPane scroll = new JScrollPane(content);add(scroll);add(new JLabel("Say:");cin = new JTextField(26);add(cin);say = new JButton
13、("Say");add(say);class StartListen implements ActionListenerpublic void actionPerformed(ActionEvent e)start.setEnabled(false);try ServerSocket s = new ServerSocket(Integer.parseInt(port.getText();socket = s.accept();PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWrite
14、r(socket.getOutputStream(),true);out.println("連接成功");content.append("連接成功"+"n");ServerThread st = new ServerThread();st.start(); catch (Exception ex) class SayListen implements ActionListenerString str;public void actionPerformed(ActionEvent e)try PrintWriter out = new
15、PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),true);str=cin.getText();if(!str.isEmpty()out.println(str);content.append("me:"+str+"n");out.flush();cin.setText(""); catch (Exception ex) class ServerThread extends Threadpublic void run()try BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream();String str;while(true)str = in.
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 學(xué)生反饋機(jī)制與調(diào)整計(jì)劃
- 提升個(gè)人職業(yè)技能的計(jì)劃
- 金融安全教育學(xué)生網(wǎng)貸詐騙預(yù)防課程
- 實(shí)現(xiàn)班級(jí)文化傳承的途徑計(jì)劃
- 開(kāi)發(fā)職業(yè)技能的長(zhǎng)期計(jì)劃
- 超市貨架布局與消費(fèi)者行為研究
- 跨文化背景下的營(yíng)銷(xiāo)活動(dòng)策劃
- 項(xiàng)目管理中的財(cái)務(wù)控制二十法
- 財(cái)務(wù)透明度提升審計(jì)流程的重要性
- 高中語(yǔ)文課外古詩(shī)文曾國(guó)藩誡子書(shū)原文及翻譯
- 小學(xué)數(shù)學(xué)(含奧數(shù))數(shù)圖形個(gè)數(shù)和找規(guī)律、簡(jiǎn)便運(yùn)算專(zhuān)項(xiàng)及練習(xí)題附答案
- Android Studio開(kāi)發(fā)實(shí)戰(zhàn)(從零基礎(chǔ)到App上線)
- 藥物警戒培訓(xùn)
- 中央民族大學(xué) 學(xué)生休學(xué)申請(qǐng)表
- 哈薩克斯坦勞動(dòng)法中文版
- 創(chuàng)傷病人的氣道管理課件
- MULAND深圳蕉內(nèi)前海中心辦公室方案
- 液化氣噴火槍安全操作規(guī)程
- 教科版五年級(jí)下冊(cè)科學(xué)同步練習(xí)全冊(cè)
- 2023九年級(jí)數(shù)學(xué)中考復(fù)習(xí)計(jì)劃
- 廖常初《FX系列LC編程及應(yīng)用》課后習(xí)題答案
評(píng)論
0/150
提交評(píng)論