網(wǎng)絡(luò)SOCKET編程報(bào)告_第1頁(yè)
網(wǎng)絡(luò)SOCKET編程報(bào)告_第2頁(yè)
網(wǎng)絡(luò)SOCKET編程報(bào)告_第3頁(yè)
網(wǎng)絡(luò)SOCKET編程報(bào)告_第4頁(yè)
網(wǎng)絡(luò)SOCKET編程報(bào)告_第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、XX 大 學(xué) 實(shí) 驗(yàn) 報(bào) 告年 月 日 課題名稱: 計(jì)算機(jī)網(wǎng)絡(luò)實(shí)驗(yàn)名稱:SOCKET編程實(shí)現(xiàn)聊天程序 班級(jí):姓名: 同組人: 指導(dǎo)老師評(píng)定: 簽名: 一、實(shí)驗(yàn)?zāi)康?、掌握網(wǎng)絡(luò)應(yīng)用程序的開(kāi)發(fā)方法;2、掌握Client/ Server結(jié)構(gòu)軟件的設(shè)計(jì)與開(kāi)發(fā)方法;3、掌握Socket機(jī)制的工作原理;4、會(huì)利用編程的方法實(shí)現(xiàn)Socket的工作機(jī)制,實(shí)現(xiàn)聊天程序。二、實(shí)驗(yàn)前的準(zhǔn)備 1、閱讀教材關(guān)于TCP/IP協(xié)議和Socket的相關(guān)內(nèi)容;2、閱讀WinSock編程指南;3、閱讀本實(shí)驗(yàn)所附內(nèi)容; 4、熟悉Eclipse開(kāi)發(fā)工具。三、實(shí)驗(yàn)內(nèi)容使用Win32 Socket 函數(shù)實(shí)現(xiàn)聊天程序:能相互對(duì)發(fā)文本消息。

2、四、實(shí)驗(yàn)步驟和實(shí)驗(yàn)源程序 實(shí)驗(yàn)步驟: 1、打開(kāi)java設(shè)計(jì)軟件Elipse,分別建立兩個(gè)新工程,取名為T(mén)alkProject; 2、在剛建立的工程下建里兩個(gè)類(lèi)ChatServer和ChatClient; 3、在兩個(gè)類(lèi)下分別編寫(xiě)源程序,利用Socket實(shí)現(xiàn)聊天軟件; 4、運(yùn)行服務(wù)器程序后,再運(yùn)行客戶端程序,就可以實(shí)現(xiàn)聊天了。實(shí)驗(yàn)源代碼: ChatServer:import java.io.*;import .*;import java.util.*;public class ChatServer boolean started = false;ServerSocket ss = null;Lis

3、t clients =Collections.synchronizedList(new ArrayList();/List clients =Collections.synchronized(new ArrayList();/clients是共享變量,通過(guò)Collections.synchronized()做同步化處理public static void main(String args) new ChatServer().start();public void start() try ss = new ServerSocket(8888); / 創(chuàng)建一個(gè)監(jiān)聽(tīng)Socket對(duì)象started =

4、 true; catch (IOException e) e.printStackTrace();try while (started) Socket s = ss.accept(); / 等待客戶端發(fā)起連接Client c = new Client(s);System.out.println(a client connected!);new Thread(c).start(); / 啟動(dòng)線程clients.add(c); / 向共享變量中添加ss.close(); / 關(guān)閉Socket catch (IOException e) e.printStackTrace();class Clien

5、t implements Runnable / 實(shí)現(xiàn)Runnable接口private Socket s;private DataInputStream dis = null;private DataOutputStream dos = null;private boolean Connected = false;public Client(Socket s) this.s = s;try dis = new DataInputStream(s.getInputStream(); / 創(chuàng)建輸入流dos = new DataOutputStream(s.getOutputStream(); /

6、創(chuàng)建輸出流Connected = true; catch (IOException e) e.printStackTrace();public void send(String str) try dos.writeUTF(str); / 向輸入流中寫(xiě)入數(shù)據(jù) catch (IOException e) clients.remove(this); / 出錯(cuò)時(shí)(客戶可能已斷線),移除一個(gè)客戶端public void run() try while (Connected) String str = dis.readUTF(); / 從輸出流中讀取數(shù)據(jù) synchronized(clients) / 對(duì)

7、共享的列表進(jìn)行遍歷時(shí)必須要同步化Iterator it = clients.iterator();/ 返回一個(gè)迭代器while(it.hasNext() Client c = it.next();c.send(str);/ 將數(shù)據(jù)發(fā)送出去/while /synchronized /while(Connected)dis.close();/ 關(guān)閉輸入流dos.close();/ 關(guān)閉輸出流s.close();/ 關(guān)閉Socket catch (Exception e) System.out.println(Client closed!); finally clients.remove(this)

8、; / 確保線程結(jié)束時(shí)從共享變量中刪除自己(比如從客戶機(jī)讀數(shù)據(jù)時(shí)出錯(cuò), / 客戶機(jī)可能已掉線,線程會(huì)結(jié)束) /try/runChatClient:import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JButton;public class ChatClient extends Frame Socket s = null;DataOutputStream dos = n

9、ull;DataInputStream dis = null;private boolean Connected = false;TextField tf = new TextField();TextArea ta1 = new TextArea();TextArea ta2 = new TextArea();Button bt1 = new Button(發(fā)送);Thread thread = new Thread(new ClientThread();/ 創(chuàng)建線程public static void main(String args) new ChatClient().call();pub

10、lic void call() bt1.setBackground(Color.cyan);setLocation(400, 300);setSize(400, 300);setLayout(null);/ 取消布局管理器setBackground(Color.cyan);tf.setBounds(250, 40, 70, 25);ta1.setBounds(30, 40, 200, 80);ta2.setBounds(30, 140, 200, 80);bt1.setBounds(265, 250, 70, 30);tf.setBounds(30, 240, 200, 35);tf.addA

11、ctionListener(new MyListener();/ 注冊(cè)事件監(jiān)聽(tīng)器add(tf);add(bt1);add(ta1);add(ta2);add(tf);this.addWindowListener(new WindowAdapter() / 關(guān)閉窗口public void windowClosing(WindowEvent e) disconnect();System.exit(0););bt1.addActionListener(new MyListener();/ 注冊(cè)事件監(jiān)聽(tīng)器setVisible(true);connect();thread.start();/ 啟動(dòng)線程p

12、ublic void connect() try s = new Socket(127.0.0.1, 8888);dos = new DataOutputStream(s.getOutputStream();/ 返回一個(gè)輸出流dis = new DataInputStream(s.getInputStream();/ 返回一個(gè)輸入流System.out.println(connected!);Connected = true; catch (Exception e) e.printStackTrace(); public void disconnect() try dos.close();/

13、關(guān)閉輸出流dis.close();/ 關(guān)閉輸入流s.close();/ 關(guān)閉Socket catch (IOException e) e.printStackTrace();private class MyListener implements ActionListener public void actionPerformed(ActionEvent e) String str = tf.getText().trim();/ 獲取文本框中的數(shù)據(jù)tf.setText();ta2.append(str+n);/ 將文本框中的數(shù)據(jù)添加到文本區(qū)中try dos.writeUTF(str);/ 向輸出流中寫(xiě)入數(shù)據(jù)dos.flush();/ 刷空流 catch (IOException e1) e1.pr

溫馨提示

  • 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)論