Socket和多線程編程的聊天程序?qū)崿F(xiàn)_第1頁
Socket和多線程編程的聊天程序?qū)崿F(xiàn)_第2頁
Socket和多線程編程的聊天程序?qū)崿F(xiàn)_第3頁
Socket和多線程編程的聊天程序?qū)崿F(xiàn)_第4頁
Socket和多線程編程的聊天程序?qū)崿F(xiàn)_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上一、        課題內(nèi)容和要求1.課題內(nèi)容基于Socket和多線程編程的聊天程序?qū)崿F(xiàn)2.課題要求網(wǎng)絡(luò)聊天程序設(shè)計非常復(fù)雜,允許多個人同時聊天更加需要多線程技術(shù)的支持,請實現(xiàn)一個簡單的多線程網(wǎng)絡(luò)聊天程序模擬。二、        設(shè)計思路分析1.    在網(wǎng)絡(luò)越來越發(fā)達的今天,人們對網(wǎng)絡(luò)的依賴越來越強,網(wǎng)絡(luò)聊天已經(jīng)成了許多人生活中必不可少的一部分,基于這樣的需求,出現(xiàn)了許多網(wǎng)絡(luò)聊天通信工具,像QQ,M

2、SN等等,但是人們已經(jīng)不再滿足于單一的兩個人之間的聊天,而是對多人同時聊天產(chǎn)生了興趣,于是出現(xiàn)了網(wǎng)絡(luò)聊天室,不同地方的人可以在那個虛擬的聊天室里面熱烈聊天?;谶@樣的想法,我們用JAVA設(shè)計一個多人同時聊天的小程序,用Socket編程實現(xiàn)網(wǎng)絡(luò)通訊,面向連接的,采用多線程的實現(xiàn)技術(shù)。2.    在程序中,可以設(shè)置加入連接的最大數(shù)目,通過更改IP地址和端口號,成為不同的客戶端,與服務(wù)器端連接,進行多用戶聊天。3.    為方便用戶交互,我們采用圖形化的用戶界面。實現(xiàn)了好友添加,消息收發(fā)、顯示等基本功能。三、  

3、0;     概要設(shè)計該網(wǎng)絡(luò)聊天程序大致分為三個主要部分:客戶端、服務(wù)器端和用戶圖形界面。各個部分的初步設(shè)計思想、流程及存儲結(jié)構(gòu)如下:1.    程序整體框架:主程序監(jiān)聽一端口,等待客戶接入;同時構(gòu)造一個線程類,準備接管會話。當一個Socket會話產(chǎn)生后,將這個會話交給線程處理,然后主程序繼續(xù)監(jiān)聽。 打開Socket命 名監(jiān)聽端口建立連接收發(fā)消息關(guān)閉連接打開Socket 連接服務(wù)器收發(fā)消息關(guān)閉連接服務(wù)器端程序客戶端程序 2.    客戶端(Client)客戶端,使用

4、Socket對網(wǎng)絡(luò)上某一個服務(wù)器的某一個端口發(fā)出連接請求,一旦連接成功,打開會話;會話完成后,關(guān)閉Socket??蛻舳瞬恍枰付ù蜷_的端口,通常臨時的、動態(tài)的分配一個端口。3.    服務(wù)器端(Server)服務(wù)器端,使用ServerSocket監(jiān)聽指定的端口,端口可以隨意指定(由于1024以下的端口通常屬于保留端口,在一些操作系統(tǒng)中不可以隨意使用,所以建議使用大于1024的端口),等待客戶連接請求,客戶連接后,會話產(chǎn)生;在完成會話后,關(guān)閉連接。4.    用戶圖形界面用戶圖形界面方便程序與用戶的交互,多個用戶參加,完成會話功能,具體

5、的設(shè)計要方便用戶的使用,直觀清晰,簡潔明了,友好美觀。5.    存儲結(jié)構(gòu)下面列出主要存儲結(jié)構(gòu)或變量:存儲結(jié)構(gòu)、變量、對象類型說明postInetAddress標識IP地址Portint標識端口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()Publi

6、cFace類成員函數(shù) 四、        詳細設(shè)計    代碼分服務(wù)器端、客戶端、和用戶圖形界面三部分,分別如下:1.服務(wù)器端服務(wù)器端主要是使用ServerSocket類,相當于服務(wù)器Socket,用來監(jiān)聽試圖進入的連接,當新的連接建立后,該類為他們實例化一個Socket對象,同時得到輸入輸出流,調(diào)用相應(yīng)方法完成會話。具體代碼如下:package nupt.java.socket;import java.awt.*;import .*;import java.io.*;public c

7、lass Server extends Thread ServerSocket skt;   / ServerSocket類監(jiān)聽進入的連接,為每個新的連接產(chǎn)生一個Socket對象          Socket Client =new Socket10;    Socket Client1=null;    int i = 0;    TextArea in;   

8、int port,k=0,l=0;    PrintStream theOutputStream;    Face chat;    public Server(int port, Face chat)         try             this.port = port;   

9、0;        skt = new ServerSocket(port);            this.chat = chat;        catch (IOException e)             chat.ta.ap

10、pend(e.toString();                public void run()         chat.ta.append("等待連線.");        while (true)         &#

11、160;   try             Clientk = skt.accept();                                &#

12、160;             /當有客戶端連接時就新建一個子線程            if (i < 2)               ServerThread server = new ServerThread10; 

13、;             serverk= new ServerThread(Clientk, this.chat, i);                 l=server.length;          

14、0;      serverk.start();                chat.ta.append(“客戶端“+ Clientk.getInetAddress() + "已連線n");               

15、60;               /for(int j=0;j<server.length;j+)                theOutputStream = new PrintStream(serverk.getClient().getOutputStream();  &#

16、160;             i = serverk.getI();                k+;             else       &

17、#160;         /theOutputStream = new PrintStream(null);                    catch (SocketException e)             ca

18、tch (IOException e)                 chat.ta.append(e.toString();                            public void d

19、ataout(String data)         /for(int j=0;j<l;j+)        theOutputStream.println(data);    class ServerThread extends Thread     ServerSocket skt;    Socket Client;   

20、TextArea in;    int port,i;    BufferedReader theInputStream;    PrintStream theOutputStream;    String readin;    Face chat;/服務(wù)端子線程    public ServerThread(Socket s, Face chat, int i)     &#

21、160;   this.i = +i;        Client = s;        this.chat = chat;        public int getI()         return this.i;        public

22、 Socket getClient()         return this.Client;        public void run()           try             theInputStream = new BufferedReader(

23、new InputStreamReader(Client                    .getInputStream();            theOutputStream = new PrintStream(Client.getOutputStream();  &

24、#160;         while (true)                 readin = theInputStream.readLine();                chat.ta.append(r

25、eadin + "n");                    catch (SocketException e)             chat.ta.append("連線中斷!n");      

26、60;                    / 設(shè)置組件可用性            chat.clientBtn.setEnabled(true);            chat.se

27、rverBtn.setEnabled(true);            chat.tfaddress.setEnabled(true);            chat.tfport.setEnabled(true);            try  

28、60;              i - -;                skt.close();                Client.close();

29、60;           catch (IOException err)                 chat.ta.append(err.toString();                

30、    catch (IOException e)             chat.ta.append(e.toString();                public void dataout(String data)         theOutp

31、utStream.println(data);    2.客戶端客戶端主要是使用Socket類,該類是JAVA實現(xiàn)網(wǎng)絡(luò)編程重要的基礎(chǔ)類,實現(xiàn)程序間雙向的面向連接的通信。調(diào)用public Socket(String host,int port)方法設(shè)定IP和端口。建好連接后,用戶通過得到Socket的輸入輸出流對象后,利用流的方法實現(xiàn)數(shù)據(jù)的傳輸。調(diào)用public InputStream getInputStream()和public OutputStream getOutputStream()方法,分別得到Socket對象的輸入輸出流;具體實現(xiàn)代碼如下:packag

32、e nupt.java.socket;import .*;import java.io.*;import javax.swing.Timer;public class Client extends Thread       Socket skt;                         &#

33、160;        / 用于客戶端的連接    InetAddress host;                        / 主機地址    int port;     

34、0;                               / 端口號    BufferedReader theInputStream;    PrintStream theOutputStream;  

35、0; String readin;    Face chat;    public Client(String ip, int p, Face chat)         try             host = InetAddress.getByName(ip);       

36、60;    / 獲取IP地址            port = p;                                &#

37、160;                 / 獲取端口號            this.chat = chat;        catch (IOException e)        &

38、#160;    chat.ta.append(e.toString();                public void run()         try             chat.ta.append("準備連線,稍后!"

39、);            skt = new Socket(host, port);                     / 新建Socket對象            ch

40、at.ta.append("成功n");                   / 緩沖區(qū)末尾添加字符串            theInputStream = new BufferedReader(new InputStreamReader(skt.getInputStream();

41、60;           theOutputStream = new PrintStream(skt.getOutputStream();            while (true)                 readin =

42、theInputStream.readLine();                chat.ta.append(readin + "n");                    catch (SocketException e)  &#

43、160;          chat.ta.append("未連上!n");            chat.clientBtn.setEnabled(true);            chat.serverBtn.setEnabled(true); 

44、60;          chat.tfaddress.setEnabled(true);            chat.tfport.setEnabled(true);            try        &

45、#160;        skt.close();            catch (IOException err)                 chat.ta.append(err.toString();    

46、0;               catch (IOException e)             chat.ta.append(e.toString();                public void dataout(

47、String data)         theOutputStream.println(data);    3.用戶圖形界面該部分主要是完成界面的初始化,合理布局組件,方便用戶交互。主要是JAVA按鈕,文本域,標簽,布局管理器的使用。主要處理了鍵盤Enter消息接受,下面是實現(xiàn)代碼:package nupt.java.socket;import java.awt.*;import java.awt.event.*;public class Face extends Frame  &#

48、160;         private static final long serialVersionUID = 1L;    Button clientBtn, serverBtn;    TextArea ta;    TextField tfaddress, tfport, tftype;    Label lbl1,lbl2,lbl3;    int por

49、t;    Client client;    Server server;    boolean iamserver;    static Face frm;    public Face()           / 實例化組件        clientBtn = new Button(&quo

50、t;客戶端");        serverBtn = new Button("服務(wù)器");        ta = new TextArea("", 10, 50, TextArea.SCROLLBARS_BOTH);        lbl1 = new Label("IP地址:");  

51、0;     tfaddress = new TextField("192.168.1.104", 10);        lbl2 = new Label("端口:");        tfport = new TextField("8080");        lbl3 = new L

52、abel("發(fā)送內(nèi)容:");        tftype = new TextField(40);        tftype.addKeyListener(new TFListener();        ta.setEditable(false);        /向容器中加入以上組件 

53、;       setLayout(new   FlowLayout();        add(lbl1);        add(tfaddress);        add(lbl2);        add(tfport);

54、0;       add(clientBtn);        add(serverBtn);        add(ta);        add(lbl3);        add(tftype);     

55、;   /設(shè)置格式        setLocation(400, 250);                /窗口顯示再屏幕的位置坐標        setSize(400, 300);        

56、60;             /設(shè)置窗體大小        setTitle("基于Socket和多線程編程的聊天程序");        this.setVisible(true);           &

57、#160;       /設(shè)置窗體可見        /事件響應(yīng)        clientBtn.addActionListener(new ActionListener()             public void actionPerformed(ActionEvent e) &

58、#160;               port = Integer.parseInt(tfport.getText();                client = new Client(tfaddress.getText(), port, frm);    &#

59、160;           client.start();                tfaddress.setEnabled(false);                tfport.set

60、Enabled(false);                serverBtn.setEnabled(false);                clientBtn.setEnabled(false);        

61、;            );        serverBtn.addActionListener(new ActionListener()             public void actionPerformed(ActionEvent e)      

62、60;          port = Integer.parseInt(tfport.getText();                server = new Server(port, frm);             &

63、#160;  server.start();                iamserver = true;                tfaddress.setText("成為服務(wù)器");      &

64、#160;         tfaddress.setEnabled(false);                tfport.setEnabled(false);                serverBtn.s

65、etEnabled(false);                clientBtn.setEnabled(false);                    );        addWindow

66、Listener(new WindowAdapter()             public void windowClosing(WindowEvent e)                 System.exit(0);         

67、60;          );          public static void main(String args)         /主方法             / TODO Auto-generated method stub 

68、;                        frm = new Face();            private class TFListener implements KeyListener      

69、60;  public 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論