基于GUI的網絡通信程序設計_第1頁
基于GUI的網絡通信程序設計_第2頁
基于GUI的網絡通信程序設計_第3頁
基于GUI的網絡通信程序設計_第4頁
基于GUI的網絡通信程序設計_第5頁
已閱讀5頁,還剩1頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Java程序設計實驗報告實驗3實驗室: 2014 年 12 月 10 日學院計算機與信息學院專業班級姓名成績課程名稱Java程序設計實驗項目名 稱 實驗三 基于GUI的網絡通信程序設計指導教師教師評語 教師簽名: 年 月 日1、 實驗目的1掌握Java中GUI程序的編寫,包括事件監聽機制。2掌握Java的網絡通信編程,ServerSocket,Socket類的使用。3掌握Java中多線程的編程,Thread類,Runnable接口的使用。4掌握用面向對象的方法分析和解決復雜問題.2、 實驗原理1。 利用java。awt和javax。swing包提供的各種組件實現服務器和客戶端的界面設計.2使用

2、套接字實現基于TCP協議的服務器和客戶端.3。為服務器和客戶端界面中的有關組件添加消息相應,實現交互。三、使用硬件、軟件環境PC 計算機一臺,配置為CPU為2。6G,內存為4G,硬盤為1T,安裝Windows8操作系統。另外,使用JCreator,JDK1。8.0等軟件四、實驗過程、步驟及原始記錄(算法、原程序、測試結果,分析等)1。實驗過程: 首先實現界面的編寫,之后使用套接字實現基于TCP協議的通信,再設置監視 器,為相應的組件添加消息相應. 2。源程序:1.客戶端程序:KeHuDuan。javaimport java。awt.*;import java。awt。event。;import

3、 java。util.*;import java。io。*;import java。net。*;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 JFrameJTextFie

4、ld 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(new FlowLay

5、out(); 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); content = new JTextArea(16,35); JScrollPane

6、 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(ActionEvent e)connect。setEnabled(false);try portNum =Integer。p

7、arseInt(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 out = new PrintWriter(new BufferedWriter(new OutputStre

8、amWriter(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 void run()try BufferedReader in = new BufferedReader(new InputStream

9、Reader(socket。getInputStream()));String str;while(true)str = in。readLine();/System。out.println (”a");content.append(str+”n”); catch (Exception ex) 2.服務器端程序:import java.awt.;import java。awt。event。;import java。util。*;import java。io。;import java。net。*;import javax。swing.;public class FuWuQipublic

10、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;JTextArea content;JTextField cin;JButton say;Socket socket;MyFrame()init();Sta

11、rtListen 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 JLabel(”Port:”);port = new JTextField("8888",25);add(port);start =

12、 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(”Say”);add(say);class StartListen implements ActionListenerpublic void actionPerformed(ActionEven

13、t e)start。setEnabled(false);try ServerSocket s = new ServerSocket(Integer.parseInt(port。getText());socket = s。accept();PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket。getOutputStream(),true);out。println(”連接成功”);content.append("連接成功”+"n”);ServerThread st =

14、 new ServerThread();st.start(); catch (Exception ex) class SayListen implements ActionListenerString str;public void actionPerformed(ActionEvent e)try PrintWriter 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 ServerThread extends Threadpublic void run()try BufferedReader in = new BufferedReader(new InputStreamReader(socket。getInputStream();String str;while(true)str =

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論