版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 《簡(jiǎn)單隨機(jī)抽樣》教案-人教版高中數(shù)學(xué)必修三
- 【核心素養(yǎng)】第12課美化生活的裝飾布 教學(xué)設(shè)計(jì)2023-2024學(xué)年人美版初中美術(shù)八年級(jí)上冊(cè)
- 粵教版高中信息技術(shù)必修教學(xué)設(shè)計(jì)-5.1.2 信息資源管理的方式方法
- 【核心素養(yǎng)目標(biāo)】第6課 希臘羅馬古典文化教案
- 第3單元 第5章 綠色開(kāi)花植物的生活方式2024年中考生物教學(xué)設(shè)計(jì)(廣東專用版)
- 分一分(二)(教學(xué)設(shè)計(jì))-2023-2024學(xué)年三年級(jí)數(shù)學(xué)下冊(cè)北師大版
- 高一上學(xué)期音樂(lè)人音版(2019)必修音樂(lè)鑒賞15.30作品鑒賞芬蘭頌教學(xué)設(shè)計(jì)
- 2024年軍訓(xùn)開(kāi)營(yíng)儀式發(fā)言稿范文(32篇)
- 2024年初中生入團(tuán)志愿書(shū)格式(5篇)
- 背影教學(xué)反思
- 鍋爐維修保養(yǎng)記錄表(范本)
- 基于教學(xué)評(píng)一體化的初中英語(yǔ)閱讀教學(xué)探索
- 護(hù)理專業(yè)專業(yè)建設(shè)與發(fā)展規(guī)劃
- 車(chē)展培訓(xùn)課件
- 家庭農(nóng)場(chǎng)合伙入股協(xié)議書(shū)
- 2023年07月廣東深圳市龍華區(qū)招考聘用社區(qū)網(wǎng)格員108人上岸筆試必備資料歷年試卷附帶答案名師詳解
- 金屬材料凝固原理與技術(shù)PPT完整版全套教學(xué)課件
- 二年級(jí)上冊(cè)數(shù)學(xué)說(shuō)課稿-連加、連減 |蘇教版
- 品管圈QCC質(zhì)量持續(xù)改進(jìn)案例皮膚科-降低窄頻中波紫外線照射不良反應(yīng)發(fā)生率PDCA
- 學(xué)校消防工作包保責(zé)任制度(二篇)
- 幼兒園園長(zhǎng)一日三巡記錄表實(shí)用文檔
評(píng)論
0/150
提交評(píng)論