




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、濱江學(xué)院課 程 設(shè) 計(jì)任務(wù)書 學(xué)年學(xué)期 2012-2013學(xué)年第二學(xué)期 課程名稱 用socket實(shí)現(xiàn)聊天程序 院 系 計(jì)算機(jī)系專 業(yè) 計(jì)科 姓 名 周洲 指導(dǎo)教師 朱節(jié)中二一三 年 七月 七 日用Socket 的編程機(jī)制實(shí)現(xiàn)聊天程序10計(jì)科 周洲 20092308065摘要: Socket 是基于TCP/ IP 的網(wǎng)絡(luò)上運(yùn)行的兩個(gè)程序間雙向通訊的一種連接機(jī)制, 包中包含Socket 類, 用以快速地開發(fā)網(wǎng)絡(luò)通信程序。通過實(shí)例模型探討基于Java 的Socket 通信程序的一般結(jié)構(gòu)。對開發(fā)一般性的通信軟件, 有一定的參考價(jià)值。關(guān)鍵詞: Java; 網(wǎng)絡(luò); SocketSocket Program
2、ming Based on TCP/IP in JavaAbstract :Socket is one of the two- way communication' s end between two procedures that are operated on the network based on TCP/IP. J bag includes Sockets, which is used to develop the network communication procedure fast.This paper probes into the general structure
3、based on Socket communication procedure of Java through the instance model.The general communication software can be developed basedon this one,so there is certain reference value.Key words : ava;network;Socket;communication一、課題內(nèi)容和要求1.課題內(nèi)容基于Socket和多線程編程的聊天程序?qū)崿F(xiàn)2.課題要求網(wǎng)絡(luò)聊天程序設(shè)計(jì)非常復(fù)雜,允許多個(gè)人同時(shí)聊天更加需要多線程技術(shù)的支持
4、,請實(shí)現(xiàn)一個(gè)簡單的多線程網(wǎng)絡(luò)聊天程序模擬。二、設(shè)計(jì)思路分析1. 在網(wǎng)絡(luò)越來越發(fā)達(dá)的今天,人們對網(wǎng)絡(luò)的依賴越來越強(qiáng),網(wǎng)絡(luò)聊天已經(jīng)成了許多人生活中必不可少的一部分,基于這樣的需求,出現(xiàn)了許多網(wǎng)絡(luò)聊天通信工具,像QQ,MSN等等,但是人們已經(jīng)不再滿足于單一的兩個(gè)人之間的聊天,而是對多人同時(shí)聊天產(chǎn)生了興趣,于是出現(xiàn)了網(wǎng)絡(luò)聊天室,不同地方的人可以在那個(gè)虛擬的聊天室里面熱烈聊天?;谶@樣的想法,我們用JAVA設(shè)計(jì)一個(gè)多人同時(shí)聊天的小程序,用Socket編程實(shí)現(xiàn)網(wǎng)絡(luò)通訊,面向連接的,采用多線程的實(shí)現(xiàn)技術(shù)。2. 在程序中,可以設(shè)置加入連接的最大數(shù)目,通過更改IP地址和端口號,成為不同的客戶端,與服務(wù)器端連接,
5、進(jìn)行多用戶聊天。3.為方便用戶交互,我們采用圖形化的用戶界面。實(shí)現(xiàn)了好友添加,消息收發(fā)、顯示等基本功能。三、概要設(shè)計(jì)該網(wǎng)絡(luò)聊天程序大致分為三個(gè)主要部分:客戶端、服務(wù)器端和用戶圖形界面。各個(gè)部分的初步設(shè)計(jì)思想、流程及存儲結(jié)構(gòu)如下:1. 程序整體框架:主程序監(jiān)聽一端口,等待客戶接入;同時(shí)構(gòu)造一個(gè)線程類,準(zhǔn)備接管會話。當(dāng)一個(gè)Socket會話產(chǎn)生后,將這個(gè)會話交給線程處理,然后主程序繼續(xù)監(jiān)聽。 打開Socket命 名監(jiān)聽端口建立連接收發(fā)消息關(guān)閉連接打開Socket 連接服務(wù)器收發(fā)消息關(guān)閉連接服務(wù)器端程序客戶端程序 2.客戶端(Client)客戶端,使用Socket對網(wǎng)絡(luò)上
6、某一個(gè)服務(wù)器的某一個(gè)端口發(fā)出連接請求,一旦連接成功,打開會話;會話完成后,關(guān)閉Socket??蛻舳瞬恍枰付ù蜷_的端口,通常臨時(shí)的、動態(tài)的分配一個(gè)端口。3.服務(wù)器端(Server)服務(wù)器端,使用ServerSocket監(jiān)聽指定的端口,端口可以隨意指定(由于1024以下的端口通常屬于保留端口,在一些操作系統(tǒng)中不可以隨意使用,所以建議使用大于1024的端口),等待客戶連接請求,客戶連接后,會話產(chǎn)生;在完成會話后,關(guān)閉連接。4.用戶圖形界面用戶圖形界面方便程序與用戶的交互,多個(gè)用戶參加,完成會話功能,具體的設(shè)計(jì)要方便用戶的使用,直觀清晰,簡潔明了,友好美觀。5.存儲結(jié)構(gòu)下面列出主要存儲結(jié)構(gòu)或變量:存
7、儲結(jié)構(gòu)、變量、對象類型說明postInetAddress標(biāo)識IP地址Portint標(biāo)識端口Server ServerThread服務(wù)器端連接數(shù)Client Socket客戶端連接數(shù)Client(String ip,int p,Face chat)publicClient類成員函數(shù)Public void run()VoidClient、Server類成員函數(shù)Server(int port,Face chat)publicServer類成員函數(shù)Face()PublicFace類成員函數(shù) 四、詳細(xì)設(shè)計(jì) 代碼分服務(wù)器端、客戶端、和用戶圖形界面三部分,分別如
8、下:1.服務(wù)器端服務(wù)器端主要是使用ServerSocket類,相當(dāng)于服務(wù)器Socket,用來監(jiān)聽試圖進(jìn)入的連接,當(dāng)新的連接建立后,該類為他們實(shí)例化一個(gè)Socket對象,同時(shí)得到輸入輸出流,調(diào)用相應(yīng)方法完成會話。具體代碼如下:package nupt.java.socket;import java.awt.*;import .*;import java.io.*;public class Server extends Thread ServerSocket skt; / ServerSocket類監(jiān)聽進(jìn)入的連接,為每個(gè)新的連接產(chǎn)生一個(gè)Socket對象
9、; Socket Client =new Socket10; Socket Client1=null; int i = 0; TextArea in; int port,k=0,l=0; PrintStream theOutputStream; Face chat;
10、; public Server(int port, Face chat) try this.port = port; skt = new ServerSocket(port);
11、 this.chat = chat; catch (IOException e) chat.ta.append(e.toString(); public void
12、 run() chat.ta.append("等待連線."); while (true) try Clientk = skt.accept()
13、; /當(dāng)有客戶端連接時(shí)就新建一個(gè)子線程
14、0; if (i < 2) ServerThread server = new ServerThread10; serverk= new ServerThread(Clientk, t
15、his.chat, i); l=server.length; serverk.start(); &
16、#160; chat.ta.append(“客戶端“+ Clientk.getInetAddress() + "已連線n"); /for(int j=0;j<serv
17、er.length;j+) theOutputStream = new PrintStream(serverk.getClient().getOutputStream(); i = serverk.getI();
18、160; k+; else /theOutputStream = new PrintStream(null);
19、60; catch (SocketException e) catch (IOException e)
20、 chat.ta.append(e.toString(); public void dataout(String data) /for(int j=0;j<l;j+)
21、 theOutputStream.println(data); class ServerThread extends Thread ServerSocket skt; Socket Client; TextArea in; int port,i; BufferedReader theInputStream;
22、PrintStream theOutputStream; String readin; Face chat;/服務(wù)端子線程 public ServerThread(Socket s, Face chat, int i) this.i = +i; Client = s;
23、60; this.chat = chat; public int getI() return this.i; public Socket getClient() return this.Client;
24、60; public void run() try theInputStream = new BufferedReader(new InputStreamReader(Client
25、0; .getInputStream(); theOutputStream = new PrintStream(Client.getOutputStream(); while (true) &
26、#160; readin = theInputStream.readLine(); chat.ta.append(readin + "n");
27、160; catch (SocketException e) chat.ta.append("連線中斷!n");
28、; / 設(shè)置組件可用性 chat.clientBtn.setEnabled(true); chat.serverBtn.setEnabled(true); chat.tfaddress.set
29、Enabled(true); chat.tfport.setEnabled(true); try i - -;
30、0; skt.close(); Client.close(); catch (IOException err)
31、0; chat.ta.append(err.toString(); catch (IOException e) ch
32、at.ta.append(e.toString(); public void dataout(String data) theOutputStream.println(data); 2.客戶端客戶端主要是使用Socket類,該類是JAVA實(shí)現(xiàn)網(wǎng)絡(luò)編程重要的基礎(chǔ)類,實(shí)現(xiàn)程序間雙向的面向連接的通信。調(diào)用public So
33、cket(String host,int port)方法設(shè)定IP和端口。建好連接后,用戶通過得到Socket的輸入輸出流對象后,利用流的方法實(shí)現(xiàn)數(shù)據(jù)的傳輸。調(diào)用public InputStream getInputStream()和public OutputStream getOutputStream()方法,分別得到Socket對象的輸入輸出流;具體實(shí)現(xiàn)代碼如下:package nupt.java.socket;import .*;import java.io.*;import javax.swing.Timer;public class Client extends Thread
34、0; Socket skt; / 用于客戶端的連接 InetAddress host; / 主機(jī)地址 int port; / 端口號 BufferedRea
35、der theInputStream; PrintStream theOutputStream; String readin; Face chat; public Client(String ip, int p, Face chat) try host = InetAddress.getByName(ip);
36、; / 獲取IP地址 port = p;
37、0; / 獲取端口號 this.chat = chat; catch (IOException e) chat.ta.append(e.toString();
38、0; public void run() try chat.ta.append("準(zhǔn)備連線,稍后!");
39、 skt = new Socket(host, port); / 新建Socket對象 chat.ta.append("成功n");
40、60; / 緩沖區(qū)末尾添加字符串 theInputStream = new BufferedReader(new InputStreamReader(skt.getInputStream(); theOutputStream = new PrintStream(skt.getOutputStream
41、(); while (true) readin = theInputStream.readLine();
42、chat.ta.append(readin + "n"); catch (SocketException e) chat.ta.append("未連上!n");
43、 chat.clientBtn.setEnabled(true); chat.serverBtn.setEnabled(true); chat.tfaddress.setEnabled(true);
44、60; chat.tfport.setEnabled(true); try skt.close();
45、60; catch (IOException err) chat.ta.append(err.toString(); catch (IOException e) &
46、#160; chat.ta.append(e.toString(); public void dataout(String data) theOutputStream.println(data); 3.用戶圖形界面該部分主
47、要是完成界面的初始化,合理布局組件,方便用戶交互。主要是JAVA按鈕,文本域,標(biāo)簽,布局管理器的使用。主要處理了鍵盤Enter消息接受,下面是實(shí)現(xiàn)代碼:package nupt.java.socket;import java.awt.*;import java.awt.event.*;public class Face extends Frame private static final long serialVersionUID = 1L;
48、0; Button clientBtn, serverBtn; TextArea ta; TextField tfaddress, tfport, tftype; Label lbl1,lbl2,lbl3; int port; Client client; Server server; boolean iamserver;
49、 static Face frm; public Face() / 實(shí)例化組件 clientBtn = new Button("客戶端"); serverBtn = new Button("服務(wù)器");
50、60; ta = new TextArea("", 10, 50, TextArea.SCROLLBARS_BOTH); lbl1 = new Label("IP地址:"); tfaddress = new TextField("192.168.1.104", 10);
51、60; lbl2 = new Label("端口:"); tfport = new TextField("8080"); lbl3 = new Label("發(fā)送內(nèi)容:"); tftype = new TextField(40);
52、; tftype.addKeyListener(new TFListener(); ta.setEditable(false); /向容器中加入以上組件 setLayout(new FlowLayout();
53、0; add(lbl1); add(tfaddress); add(lbl2); add(tfport); add(clientBtn); add(serverBtn);
54、; add(ta); add(lbl3); add(tftype); /設(shè)置格式 setLocation(400, 250);
55、60; /窗口顯示再屏幕的位置坐標(biāo) setSize(400, 300); /設(shè)置窗體大小 setTitle("基于Socket和多線程編程的聊天程序"); this.setVisible(true);
56、; /設(shè)置窗體可見 /事件響應(yīng) clientBtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)
57、 port = Integer.parseInt(tfport.getText(); client = new Client(tfaddress.getText(), port, frm); &
58、#160; client.start(); tfaddress.setEnabled(false); tfport.setEnabled(false);
59、160; serverBtn.setEnabled(false); clientBtn.setEnabled(false);
60、0; ); serverBtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)
61、160; port = Integer.parseInt(tfport.getText(); server = new Server(port, frm); serve
62、r.start(); iamserver = true; tfaddress.setText("成為服務(wù)器");
63、 tfaddress.setEnabled(false); tfport.setEnabled(false); serverBtn.setEnabled(false);
64、 clientBtn.setEnabled(false); ); addWindowListener(new Wind
65、owAdapter() public void windowClosing(WindowEvent e) System.exit(0); &
66、#160; ); public static void main(String args) /主方法 / TODO Auto-generated method stub
67、0; frm = new Face(); private class TFListener implements KeyListener public
68、void keyPressed(KeyEvent e) if (e.getKeyCode() = KeyEvent.VK_ENTER) /按Enter輸出顯示聊天內(nèi)容 ta.append(">" + tftype.getText() + "n");
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025常規(guī)商品進(jìn)口合同范本
- 2025企業(yè)公司技術(shù)轉(zhuǎn)讓合同模板
- 2025年城市住宅租賃合同模板
- 2025年度紀(jì)實(shí)攝影服務(wù)合同
- 重視細(xì)節(jié)在學(xué)習(xí)中的CAD工程師認(rèn)證考試試題及答案
- 2025年加氫工藝模擬考試題及答案
- 酒店行業(yè)歷史與未來試題及答案
- 紡織機(jī)械操作乙級考試試題及答案2024
- 2025關(guān)于簽訂工程承包合同范本
- 供貨保證合同范例
- 普通高中地理課程標(biāo)準(zhǔn)(2023年版)
- 檢驗(yàn)批劃分方案14
- 科普1原地浸出采鈾
- 《公共管理學(xué)》期末考試復(fù)習(xí)題庫(含答案)
- 接觸網(wǎng)工程圖識圖 六跨電分相絕緣錨段關(guān)節(jié)安裝圖的識圖
- 公司實(shí)際控股人協(xié)議書
- 吊裝安全事故經(jīng)驗(yàn)分享
- 研究生干細(xì)胞培訓(xùn)課件
- 科學(xué)研究與方法論P(yáng)PT課件講義
- 機(jī)械制圖習(xí)題集第九章《裝配圖》匯編
- 土建生態(tài)環(huán)保和綠色施工環(huán)境管理培訓(xùn)ppt
評論
0/150
提交評論