java課程設(shè)計(jì)-聊天程序文件_第1頁
java課程設(shè)計(jì)-聊天程序文件_第2頁
java課程設(shè)計(jì)-聊天程序文件_第3頁
java課程設(shè)計(jì)-聊天程序文件_第4頁
java課程設(shè)計(jì)-聊天程序文件_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

./學(xué)院Java程序設(shè)計(jì)課程設(shè)計(jì)題目:聊天程序?qū)W生:Puter學(xué)號:6所在院<系>:數(shù)學(xué)與計(jì)算機(jī)學(xué)院專業(yè):網(wǎng)絡(luò)工程班級:2013級網(wǎng)本指導(dǎo)教師:吳建斌職稱:導(dǎo)師2014年12月31日.學(xué)院本科學(xué)生課程設(shè)計(jì)任務(wù)書題目聊天程序1、課程設(shè)計(jì)的目的通過本課程設(shè)計(jì)使學(xué)生能夠較全面的掌握面向?qū)ο蟪绦蛟O(shè)計(jì)的有關(guān)概念和開發(fā)方法,以便能較全面地理解、掌握和綜合運(yùn)用所學(xué)的知識,提高自身的編程能力。2、課程設(shè)計(jì)的容和要求〔包括原始數(shù)據(jù)、技術(shù)要求、工作要求等該程序?qū)崿F(xiàn)聊天功能,包括服務(wù)器段程序和客戶端程序。要求:<1>客戶端程序:可連接到服務(wù)器,并將消息發(fā)送到服務(wù)器端和接受服務(wù)器端發(fā)送來的消息。<2>服務(wù)器端程序:可將消息發(fā)送到客戶端和接受客戶端發(fā)送來的消息。3、主要參考文獻(xiàn)[1]朱福喜.《Java語言程序設(shè)計(jì)》〔第二版.科學(xué)[2]國君等.《Java程序設(shè)計(jì)基礎(chǔ)》〔第二版.清華大學(xué)[3]Deitel.《Java大學(xué)基礎(chǔ)教程》〔第六版.電子工業(yè)[4]MaryCampione.《Java語言導(dǎo)學(xué)》〔第四版.機(jī)械工業(yè)[5]Y.DanielLiang.《Java語言程序設(shè)計(jì)基礎(chǔ)篇》〔第六版.機(jī)械工業(yè)[6]KathySierra.《HeadFirstJava》〔第二版.東南大學(xué)4、課程設(shè)計(jì)工作進(jìn)度計(jì)劃序號時(shí)間〔天容安排備注11分析設(shè)計(jì)準(zhǔn)備周一22編程調(diào)試階段周二至周三31編寫課程設(shè)計(jì)報(bào)告周四41考核周五總計(jì)5〔天指導(dǎo)教師〔簽字日期年月日教研室意見:年月日學(xué)生〔簽字:接受任務(wù)時(shí)間:年月日注:任務(wù)書由指導(dǎo)教師填寫。課程設(shè)計(jì)〔論文指導(dǎo)教師成績評定表題目名稱聊天程序評分項(xiàng)目分值得分評價(jià)涵工作表現(xiàn)20%01學(xué)習(xí)態(tài)度6遵守各項(xiàng)紀(jì)律,工作刻苦努力,具有良好的科學(xué)工作態(tài)度。02科學(xué)實(shí)踐、調(diào)研7通過實(shí)驗(yàn)、試驗(yàn)、查閱文獻(xiàn)、深入生產(chǎn)實(shí)踐等渠道獲取與課程設(shè)計(jì)有關(guān)的材料。03課題工作量7按期圓滿完成規(guī)定的任務(wù),工作量飽滿。能力水平35%04綜合運(yùn)用知識的能力10能運(yùn)用所學(xué)知識和技能去發(fā)現(xiàn)與解決實(shí)際問題,能正確處理實(shí)驗(yàn)數(shù)據(jù),能對課題進(jìn)行理論分析,得出有價(jià)值的結(jié)論。05應(yīng)用文獻(xiàn)的能力5能獨(dú)立查閱相關(guān)文獻(xiàn)和從事其他調(diào)研;能提出并較好地論述課題的實(shí)施方案;有收集、加工各種信息及獲取新知識的能力。06設(shè)計(jì)〔實(shí)驗(yàn)?zāi)芰?方案的設(shè)計(jì)能力5能正確設(shè)計(jì)實(shí)驗(yàn)方案,獨(dú)立進(jìn)行裝置安裝、調(diào)試、操作等實(shí)驗(yàn)工作,數(shù)據(jù)正確、可靠;研究思路清晰、完整。07計(jì)算及計(jì)算機(jī)應(yīng)用能力5具有較強(qiáng)的數(shù)據(jù)運(yùn)算與處理能力;能運(yùn)用計(jì)算機(jī)進(jìn)行資料搜集、加工、處理和輔助設(shè)計(jì)等。08對計(jì)算或?qū)嶒?yàn)結(jié)果的分析能力〔綜合分析能力、技術(shù)經(jīng)濟(jì)分析能力10具有較強(qiáng)的數(shù)據(jù)收集、分析、處理、綜合的能力。成果質(zhì)量45%09插圖〔或圖紙質(zhì)量、篇幅、設(shè)計(jì)〔論文規(guī)化程度5符合本專業(yè)相關(guān)規(guī)或規(guī)定要求;規(guī)化符合本文件第五條要求。10設(shè)計(jì)說明書〔論文質(zhì)量30綜述簡練完整,有見解;立論正確,論述充分,結(jié)論嚴(yán)謹(jǐn)合理;實(shí)驗(yàn)正確,分析處理科學(xué)。11創(chuàng)新10對前人工作有改進(jìn)或突破,或有獨(dú)特見解。成績指導(dǎo)教師評語指導(dǎo)教師簽名:年月日.目錄摘要1需求分析1概要設(shè)計(jì)3三、詳細(xì)設(shè)計(jì)4四、調(diào)試分析4五、結(jié)論14六、實(shí)驗(yàn)分工14七、參考文獻(xiàn)14.摘要在網(wǎng)絡(luò)越來越發(fā)達(dá)的今天,人們對網(wǎng)絡(luò)的依賴越來越多,越來越離不開網(wǎng)絡(luò),由此而產(chǎn)生的聊天工具越來越多,例如,國外的ICQ、國騰訊公司開發(fā)的OICQ?;贘ava網(wǎng)絡(luò)編程的強(qiáng)大功能,本次畢業(yè)設(shè)計(jì)使用Java編寫一個(gè)聊天系統(tǒng)。一般來說,聊天工具大多數(shù)由客戶端程序和服務(wù)器程序外加服務(wù)器端用于存放客戶數(shù)據(jù)的數(shù)據(jù)庫組成,本系統(tǒng)采用客戶機(jī)/服務(wù)器架構(gòu)模式通過Java提供的Soket類來連接客戶機(jī)和服務(wù)器并使客戶機(jī)和服務(wù)器之間相互通信,由于聊天是多點(diǎn)對多點(diǎn)的而Java提供的多線程功能用多線程可完成多點(diǎn)對多點(diǎn)的聊天,數(shù)據(jù)庫管理系統(tǒng)用SQLServer2000完成并通過JDBC-ODBC橋訪問數(shù)據(jù)庫?,F(xiàn)在的各種聊天工具功能越來越強(qiáng)大,但是它們的唯一的不足就是必須要聯(lián)網(wǎng)或者下載到它們的服務(wù)端,要不然是沒法脫離Intenet使用的。為了能在一個(gè)沒有連接到Intenet的局域網(wǎng)使用聊天工具,那就必須要有服務(wù)器端。本系統(tǒng)就是包括服務(wù)器端與客戶端,可以在局域網(wǎng)聊天的一個(gè)工具。建立在JAVA平臺上,系統(tǒng)的設(shè)計(jì)使用了面向?qū)ο蠹夹g(shù)和面向?qū)ο蟮脑O(shè)計(jì)原則。系統(tǒng)采用C/S結(jié)構(gòu),客戶端與客戶端以及客戶端與服務(wù)器端之間通過Socket傳送消息。使用JAVA語言編寫,開發(fā)工具采用Eclipse。服務(wù)器端設(shè)計(jì)與實(shí)現(xiàn)過程中,采用了多線程技術(shù),可以在單個(gè)程序當(dāng)中同時(shí)運(yùn)行多個(gè)不同的線程,執(zhí)行不同的任務(wù)。大大增強(qiáng)了程序?qū)Ψ?wù)器資源的利用。聊天系統(tǒng)完成后將可進(jìn)行多人對多人的聊天,對好友進(jìn)行添加、刪除,對新用戶的注冊,發(fā)送消息、接受消息等等功能。關(guān)鍵詞:多線程;客戶機(jī)/服務(wù)器;JAVA;Socket;Eclipse;TCP/IP.需求分析為了更好的掌握java這門課程的容,同時(shí)更好的理解與掌握socket編程,對所有面向?qū)ο蟮木幊趟枷胍约熬幊谭椒ǖ囊粋€(gè)總結(jié)、回顧和實(shí)踐,掌握J(rèn)ava語言編程技巧。要求使用圖形用戶界面。能實(shí)現(xiàn)一個(gè)聊天室中多人聊天??梢詢扇怂搅摹!蔡崾荆菏褂胹ocket通信程序描述清晰準(zhǔn)確,思路清晰,程序能夠?qū)崿F(xiàn)相應(yīng)的功能。聊天時(shí)系統(tǒng)是通過即時(shí)通訊技術(shù)來實(shí)現(xiàn)在線聊天、交流的軟件。目前有2種架構(gòu)形式,1、一種是C/S架構(gòu),采用客戶端/服務(wù)器形式,用戶使用過程中需要下載安裝客戶端軟件,典型的代表有:QQ、百度HI、SkypeQQVSGU、Gtalk、新浪UC、MSN等;2、采用B/S架構(gòu),即瀏覽器/服務(wù)端形式,這種形式的即時(shí)通訊軟件,直接借助互聯(lián)網(wǎng)為媒介、客戶端無需安裝任何軟件,既可以體驗(yàn)服務(wù)器端進(jìn)行溝通對話,一般運(yùn)用在電子商務(wù)的服務(wù)商,典型的代表有Websitelive、53KF、live800等二、概要設(shè)計(jì)目前國外做聊天系統(tǒng)的公司很多,產(chǎn)品也琳瑯滿目,國有諸如騰訊QQ、新浪UC、網(wǎng)易泡泡等,國外有著名的MSN〔新版改名為LiveMessenger以及跨平臺Gaim等。本人取最具代表性的QQ和MSN進(jìn)行了一些研究,作為我開發(fā)聊天系統(tǒng)的準(zhǔn)備。騰訊無疑是國即時(shí)通訊市場的霸主,自從99年進(jìn)入即時(shí)通訊領(lǐng)域并迅速占市場之后,其在國用戶數(shù)量始終高居榜首,即使近幾年面對微軟MSN的強(qiáng)大攻勢,騰訊QQ的時(shí)常占有率依然穩(wěn)步增長。騰訊的成功與其對QQ的不斷創(chuàng)新和完善是分不開的。參考了許多網(wǎng)絡(luò)上的資料,以及自己通過觀察騰訊QQ運(yùn)行時(shí)的各種細(xì)節(jié)??梢源_定騰訊QQ是以多服務(wù)器提供服務(wù)、服務(wù)器總控客戶端、客戶端之間UDP直連通信的。并且在兩個(gè)客戶端之間不能建立直連的情況下,才由服務(wù)器進(jìn)行中轉(zhuǎn)通信。其模型如圖1-1圖1.1詳細(xì)設(shè)計(jì)通過TCP網(wǎng)絡(luò)編程,主要編寫有客戶端與服務(wù)器端兩個(gè)部分,服務(wù)器端通過循環(huán)一直從客戶端接收信息,并通過線程實(shí)現(xiàn)多個(gè)客戶端連接。服務(wù)器發(fā)送信息則通過定義鏈表保存客戶端信息。每個(gè)客戶端向服務(wù)器發(fā)送信息時(shí),則通過鏈表中的所有客戶端的連接而發(fā)送信息給所有客戶端界面從而實(shí)現(xiàn)局域網(wǎng)多人聊天。而客戶端則主要是界面的編寫,通過一個(gè)線程一直從服務(wù)器獲取信息,如果不為空時(shí)就在界面上顯示。調(diào)試分析首先運(yùn)行服務(wù)器如圖1.2:圖1.2分析:服務(wù)器進(jìn)入阻塞狀態(tài)。等待連接。主要代碼:package.server;importjava.io.IOException;import.ServerSocket;import.Socket;importjava.util.ArrayList;importjava.util.List;import.server.thread.ThreadServer;publicclassServer{ //服務(wù)器功能實(shí)現(xiàn) privateServerSocketserver=null; publicstaticList<Socket>list=newArrayList<Socket><>;//定義鏈表儲存客戶信息 privateSocketclient=null; privateintcount=0; publicServer<>{ try{ server=newServerSocket<8888>; //創(chuàng)建服務(wù)器 }catch<IOExceptione1>{ e1.printStackTrace<>; } System.out.println<"等待服務(wù)器被連接······">; booleanflag=true; while<flag>{ synchronized<this>{ try{ client=server.accept<>; count++; //記錄客戶連接數(shù)目 }catch<IOExceptione1>{ e1.printStackTrace<>; } list.add<client>; System.out.println<"第"+count+"客戶端被連接!">; try{ newThread<newThreadServer<client>>.start<>;//定義線程,實(shí)現(xiàn)多個(gè)客戶端連接 }catch<Exceptione>{ e.printStackTrace<>; flag=false; } } } try{ server.close<>; }catch<IOExceptione>{ e.printStackTrace<>; } }}package.server.thread;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.io.PrintStream;import.Socket;importjava.util.Iterator;import.server.util.ListCO;publicclassThreadServerimplementsRunnable{ /定義線程,實(shí)現(xiàn)多個(gè)客戶端連接 privateBufferedReaderbuf=null; privateSocketclient=null; publicThreadServer<Socketclient>throwsException{ this.client=client; buf=newBufferedReader<newInputStreamReader<this.client.getInputStream<>>>; //連接從客戶端獲取信息 } Override publicvoidrun<>{ //線程 Stringstr=null; booleanf=true; while<f>{ //一直與連接的客戶端通訊 try{ str=buf.readLine<>; }catch<Exceptione>{ f=false; } if<"bye".equals<str>>{ f=false; }else{ ListCOlout=null; try{ lout=newListCO<>; }catch<Exceptione>{ e.printStackTrace<>; } Iterator<PrintStream>iter=lout.getLout<>.iterator<>;//向所有的客戶端發(fā)送信息 while<iter.hasNext<>>{ PrintStreamo=iter.next<>; o.println<str>; } } }}}package.server.util;importjava.io.PrintStream;import.Socket;importjava.util.ArrayList;ator;importjava.util.List;import.server.Server;publicclassListCO{ //保存客戶端的連接 privateList<Socket>list=null; //通過鏈表保存 privateList<PrintStream>lout=null; publicListCO<>throwsException{ this.list=Server.list; lout=newArrayList<PrintStream><>; Iterator<Socket>ilist=list.iterator<>; while<ilist.hasNext<>>{ PrintStreamout=null; Socketc=ilist.next<>; out=newPrintStream<c.getOutputStream<>>; lout.add<out>; } } publicList<PrintStream>getLout<>{ returnlout; } publicvoidsetLout<List<PrintStream>lout>{ this.lout=lout; }}2、客戶端的啟動(dòng)如圖1.3、圖1.4:圖1.3分析:登錄界面先設(shè)置聊天使用的名字圖1.4分析:啟動(dòng)時(shí)先輸入聊天,然后才進(jìn)入聊天,這樣就可以進(jìn)行多人聊天了。主要代碼:package.client;importjavax.swing.JFrame;importjavax.swing.JTextArea;importjavax.swing.JLabel;importjavax.swing.JScrollPane;importjava.awt.Color;importjava.awt.Font;importjavax.swing.JTextField;importjavax.swing.JButton;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.IOException;importjava.io.PrintStream;import.Socket;publicclassclientAPP{ //客戶端界面 privateJFrameframe; privateJTextFieldtextField_1; privateJTextFieldtextField_2=newJTextField<>; privatefinalJTextAreatextArea=newJTextArea<>; JLabellblNewLabel=newJLabel<>; JButtonbtnNewButton2=null; privateSocketclient=null; privatePrintStreamout=null; privateStringname; /** *Launchtheapplication. */ publicstaticvoidmain<String[]args>{ clientAPPwindow=newclientAPP<>; window.startAPP<>; } /** *Createtheapplication. *return */ publicvoidstartAPP<>{ //登錄界面 finalJFramef=newJFrame<"聊天帥名">; f.getContentPane<>.setForeground<Color.GREEN>; f.getContentPane<>.setFont<newFont<"新宋體",Font.BOLD,20>>; f.setBounds<700,320,450,300>; f.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>; f.getContentPane<>.setLayout<null>; JLabellblNewLabel2=newJLabel<"\u8BF7\u8BBE\u7F6E\u4F60\u9017\u540D">; lblNewLabel2.setForeground<Color.RED>; lblNewLabel2.setBackground<Color.RED>; lblNewLabel2.setFont<newFont<"新宋體",Font.BOLD,25>>; lblNewLabel2.setBounds<108,23,226,50>; f.getContentPane<>.add<lblNewLabel2>; textField_2.setForeground<Color.CYAN>; textField_2.setFont<newFont<"新宋體",Font.BOLD,22>>; textField_2.setBounds<130,97,168,41>; f.getContentPane<>.add<textField_2>; textField_2.setColumns<10>; btnNewButton2=newJButton<"\u8FDB\u5165">; btnNewButton2.setFont<newFont<"微軟雅黑",Font.BOLD,30>>; btnNewButton2.setForeground<Color.BLUE>; btnNewButton2.setBounds<145,165,127,41>; f.getContentPane<>.add<btnNewButton2>; f.setVisible<true>; btnNewButton2.addActionListener<newActionListener<>{ Override publicvoidactionPerformed<ActionEvente>{ frame.setVisible<true>; //登錄后才顯示聊天界面 f.setVisible<false>; //隱藏登錄界面 lblNewLabel.setText<"我叫:"+textField_2.getText<>>; } }>; } publicclientAPP<>{ initialize<>; } /** *Initializethecontentsoftheframe. */ privatevoidinitialize<>{ //聊天界面 frame=newJFrame<"逗比聊天室byPuter">; frame.getContentPane<>.setFont<newFont<"楷體",Font.BOLD,18>>; frame.getContentPane<>.setForeground<Color.BLACK>; frame.setForeground<Color.RED>; frame.setTitle<"\u8BA8\u8BBA\u7EC4">; frame.setBounds<450,150,994,631>; frame.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>; frame.getContentPane<>.setLayout<null>; JLabellabel=newJLabel<"\u9017\u6BD4\u804A\u5929\u5BA4">; label.setFont<newFont<"楷體",Font.BOLD,30>>; label.setForeground<Color.RED>; label.setBounds<383,23,186,50>; frame.getContentPane<>.add<label>; JScrollPanescrollPane=newJScrollPane<>; scrollPane.setBounds<246,110,466,332>; frame.getContentPane<>.add<scrollPane>; textArea.setBackground<Color.BLACK>; textArea.setFont<newFont<"楷體",Font.BOLD,17>>; scrollPane.setViewportView<textArea>; textArea.setEnabled<false>; finalJButtonbtnNewButton=newJButton<"\u53D1\u9001">; btnNewButton.setFont<newFont<"宋體",Font.BOLD,15>>; btnNewButton.setForeground<Color.GREEN>; btnNewButton.setBounds<629,484,83,40>; frame.getContentPane<>.add<btnNewButton>; textField_1=newJTextField<>; textField_1.setFont<newFont<"楷體",Font.BOLD,17>>; textField_1.setBounds<246,484,361,40>; frame.getContentPane<>.add<textField_1>; textField_1.setColumns<10>; try{ client=newSocket<"localhost",8888>; }catch<IOExceptione>{ e.printStackTrace<>; } try{ out=newPrintStream<client.getOutputStream<>>; }catch<IOExceptione>{ e.printStackTrace<>; } newThread<newOutInfo<this.textArea,client>>.start<>; //線程時(shí)刻刷新聊天界面〔客戶端的精華后面附上 lblNewLabel.setForeground<Color.RED>; lblNewLabel.setFont<newFont<"新宋體",Font.BOLD,24>>; lblNewLabel.setBounds<29,115,180,40>; frame.getContentPane<>.add<lblNewLabel>; if<textField_1.getText<>!=null>{ btnNewButton.addActionListener<newActionListener<>{ Override publicvoidactionPerformed<ActionEvente>{ if<textField_1.getText<>!=null>{ out.println<textField_2.getText<>+"說:"+textField_1.getText<>>; textField_1.setText<"">; } } }>; } }}package.client;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;import.Socket;importjavax.swing.JTextArea;publicclassOutInfoimplementsRunnable{ //客戶端的精華-實(shí)現(xiàn)部分 privateJTextAreat=null; //向界面顯示信息 privateSocketclient=null; privateBufferedReaderbuf=null; publicOutInfo<JTextAreat,Socketclient>{ this.t=t; this.client=client; //界面?zhèn)鬟^來實(shí)例化 } Override publicvoidrun<>{ //線程 booleanflag=true; Stringstr=null; try{ buf=newBufferedReader<newInputStreamReader<client.getInputStream<>>>; }catch<IOExceptione>{ e.printStackTrace<>; } while<flag>{ //一直監(jiān)聽服務(wù)器端口接收信息 synchronized<this>{ try{ str=buf.readLine<>; //接收 }catch<IOExceptione>{ e.printStackTrace<>; } if<str!=null>{ System.out.println<str>; t.append<str+"\n">; //顯示 } } } }}五、結(jié)論收貨很多,學(xué)會了團(tuán)隊(duì)合作的重要性,也熟悉了業(yè)務(wù)開發(fā)流程,更重要的是這是對自己所學(xué)的一種總結(jié)。詞程序最主要困難的地方就是服務(wù)器要發(fā)給所有的客戶端,和客戶端要時(shí)刻更新接收到服務(wù)器的信息。六、實(shí)驗(yàn)分工分工王科茗、嬌報(bào)告書綜合整理和程序測試王科茗制作流程圖和一些資料查找嬌資料查找及任務(wù)書的整理王科茗程序查找和調(diào)試及資料整合嬌前端程序的界面部分王科茗后端具體的實(shí)現(xiàn)和前端的結(jié)合王科茗:55%〔+-5%嬌:45%〔+-5%七、參考文獻(xiàn)[1]朱福喜.《Java語言程序設(shè)計(jì)》〔第二版.科學(xué)[2]國君等.《Java程序設(shè)計(jì)基礎(chǔ)》〔第二版.清華大學(xué)[3]Deitel.《Java大學(xué)基礎(chǔ)教程》〔第六版.電子工業(yè)[4]MaryCampione.《Java語言導(dǎo)學(xué)》〔第四版.機(jī)械工業(yè)[5]Y.DanielLiang.《Java語言程序設(shè)計(jì)基礎(chǔ)篇》〔第六版.機(jī)械工業(yè)[6]KathySierra.《HeadFirstJava》〔第二版.東南大學(xué)源代碼〔先啟動(dòng)服務(wù)器才能啟動(dòng)客戶端,局域網(wǎng)通訊是客戶端的"localhost"改為"服務(wù)器所在電腦的IP地址":服務(wù)器:package.server;importjava.io.IOException;import.ServerSocket;import.Socket;importjava.util.ArrayList;importjava.util.List;import.server.thread.ThreadServer;publicclassServer{ privateServerSocketserver=null; publicstaticList<Socket>list=newArrayList<Socket><>; privateSocketclient=null; privateintcount=0; publicServer<>{ try{ server=newServerSocket<8888>; }catch<IOExceptione1>{ e1.printStackTrace<>; } System.out.println<"等待服務(wù)器被連接······">; booleanflag=true; while<flag>{ synchronized<this>{ try{ client=server.accept<>; count++; }catch<IOExceptione1>{ e1.printStackTrace<>; } list.add<client>; System.out.println<"第"+count+"客戶端被連接!">; try{ newThread<newThreadServer<client>>.start<>; }catch<Exceptione>{ e.printStackTrace<>; flag=false; } } } try{ server.close<>; }catch<IOExceptione>{ e.printStackTrace<>; } }}package.server.start;import.server.Server;publicclassServerStart{publicstaticvoidmain<String[]args>{newServer<>; }}package.server.thread;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.io.PrintStream;import.Socket;importjava.util.Iterator;import.server.util.ListCO;publicclassThreadServerimplementsRunnable{ privateBufferedReaderbuf=null; privateSocketclient=null; publicThreadServer<Socketclient>throwsException{ this.client=client; buf=newBufferedReader<newInputStreamReader<this.client.getInputStream<>>>; } Override publicvoidrun<>{ Stringstr=null; booleanf=true; while<f>{ try{ str=buf.readLine<>; }catch<Exceptione>{ f=false; } if<"bye".equals<str>>{ f=false; }else{ ListCOlout=null; try{ lout=newListCO<>; }catch<Exceptione>{ e.printStackTrace<>; } Iterator<PrintStream>iter=lout.getLout<>.iterator<>; while<iter.hasNext<>>{ PrintStreamo=iter.next<>; o.println<str>; } } }}}package.server.util;importjava.io.PrintStream;import.Socket;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;import.server.Server;publicclassListCO{ privateList<Socket>list=null; privateList<PrintStream>lout=null; publicListCO<>throwsException{ this.list=Server.list; lout=newArrayList<PrintStream><>; Iterator<Socket>ilist=list.iterator<>; while<ilist.hasNext<>>{ PrintStreamout=null; Socketc=ilist.next<>; out=newPrintStream<c.getOutputStream<>>; lout.add<out>; } } publicList<PrintStream>getLout<>{ returnlout; } publicvoidsetLout<List<PrintStream>lout>{ this.lout=lout; }}客戶端:package.client;importjavax.swing.JFrame;importjavax.swing.JTextArea;importjavax.swing.JLabel;importjavax.swing.JScrollPane;importjava.awt.Color;importjava.awt.Font;importjavax.swing.JTextField;importjavax.swing.JButton;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.IOException;importjava.io.PrintStream;import.Socket;publicclassclientAPP{ privateJFrameframe; privateJTextFieldtextField_1; privateJTextFieldtextField_2=newJTextField<>; privatefinalJTextAreatextArea=newJTextArea<>; JLabellblNewLabel=newJLabel<>; JButtonbtnNewButton2=null; privateSocketclient=null; privatePrintStreamout=null; privateStringname; /** *Launchtheapplication. */ publicstaticvoidmain<String[]args>{ clientAPPwindow=newclientAPP<>; window.startAPP<>; } /** *Createtheapplication. *return */ publicvoidstartAPP<>{ finalJFramef=newJFrame<"聊天帥名">; f.getContentPane<>.setForeground<Color.GREEN>; f.getContentPane<>.setFont<newFont<"新宋體",Font.BOLD,20>>; f.setBounds<700,320,450,300>; f.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>; f.getContentPane<>.setLayout<null>; JLabellblNewLabel2=newJLabel<"\u8BF7\u8BBE\u7F6E\u4F60\u9017\u540D">; lblNewLabel2.setForeground<Color.RED>; lblNewLabel2.setBackground<Color.RED>; lblNewLabel2.setFont<newFont<"新宋體",Font.BOLD,25>>; lblNewLabel2.setBounds<108,23,226,50>; f.getContentPane<>.add<lblNewLabel2>; textField_2.setForeground<Color.CYAN>; textField_2.setFont<newFont<"新宋體",Font.BOLD,22>>; textField_2.setBounds<130,97,168,41>; f.getContentPane<>.add<textField_2>; textField_2.setColumns<10>; btnNewButton2=newJButton<"\u8FDB\u5165">; btnNewButton2.setFont<newFont<"微軟雅黑",Font.BOLD,30>>; btnNewButton2.setForeground<Color.BLUE>; btnNewButton2.setBounds<145,165,127,41>; f.getContentPane<>.add<btnNewButton2>; f.setVisible<true>; btnNewButton2.addActionListener<newActionListener<>{ Override publicvoidactionPerformed<ActionEvente>{ frame.setVisible<true>; f.setVisible<false>; lblNewLabel.setText<"我叫:"+textField_2.getText<>>; } }>; } publicclientAPP<>{ initialize<>; } /** *Initializethecontentsoftheframe. */ privatevoidinitialize<>{ frame=newJFrame<"逗比聊天室byPuter">; frame.getContentPane<>.setFont<newFont<"楷體",Font.BOLD,18>>; frame.getContentPane<>.setForeground<Color.BLACK>; frame.setForeground<Color.RED>; frame.setTitle<"\u8BA8\u8BBA\u7EC4">; frame.setBounds<450,150,994,631>; frame.setDefaultCloseOperation<JFrame.EXIT_ON_CLOSE>; frame.getContentPane<>.setLayout<null>; JLabellabel=newJLabel<"\u9017\u6BD4\u804A\u5929\u5BA4">; label.setFont<newFont<"楷體",Font.BOLD,30>>; label.setForeground<Color.RED>; label.setBounds<383,23,186,50>; frame.getContentPane<>.add<label>; JScrollPanescrollPane=newJScrollPane<>; scrollPane.setBounds<246,110,466,332>; frame.getContentPane<>.add<scrollPane>; textArea.setBackground<Color.BLACK>; textArea.setFont<newFont<"楷體",Font.BOLD,17>>; scrollPane.setViewportView<textArea>; textArea.setEnabled<false>; finalJButtonbtnNewButton=

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論