基于GUI的網(wǎng)絡(luò)通信程序設(shè)計(jì)_第1頁(yè)
基于GUI的網(wǎng)絡(luò)通信程序設(shè)計(jì)_第2頁(yè)
基于GUI的網(wǎng)絡(luò)通信程序設(shè)計(jì)_第3頁(yè)
基于GUI的網(wǎng)絡(luò)通信程序設(shè)計(jì)_第4頁(yè)
基于GUI的網(wǎng)絡(luò)通信程序設(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論