java聊天室系統(tǒng)程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第1頁(yè)
java聊天室系統(tǒng)程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第2頁(yè)
java聊天室系統(tǒng)程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第3頁(yè)
java聊天室系統(tǒng)程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第4頁(yè)
java聊天室系統(tǒng)程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩64頁(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)介

PAGEPAGE68第68頁(yè)共69頁(yè)Java聊天室系統(tǒng)程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告題目:******

院(系):******

年級(jí)專業(yè):******

學(xué)生姓名:******

指導(dǎo)老師:******

開始時(shí)間:******

完成時(shí)間:******第一部分:課程設(shè)計(jì)總述課程設(shè)計(jì)目的:1.學(xué)會(huì)Java程序開發(fā)的環(huán)境搭建與配置,并在實(shí)際運(yùn)用中學(xué)習(xí)和掌握J(rèn)ava程序開發(fā)的全過程。2.進(jìn)一步熟悉掌握J(rèn)ava程序設(shè)計(jì)語(yǔ)言的基礎(chǔ)內(nèi)容,如用戶圖形界面設(shè)計(jì)、Java多線程編程、Applet程序的設(shè)計(jì)、文件和網(wǎng)絡(luò)輸入輸出流編程、Java數(shù)據(jù)庫(kù)編程等。3.通過親自動(dòng)手寫程序,一方面拓展知識(shí)面,一方面鍛煉調(diào)試能力。課程設(shè)計(jì)總體要求:1.問題的描述與程序?qū)⒁獙?shí)現(xiàn)的具體功能。2.程序功能實(shí)現(xiàn)的具體設(shè)計(jì)思路或框架,并檢查流程設(shè)計(jì)。3.代碼實(shí)現(xiàn)。4.程序測(cè)試(如程序代碼、功能測(cè)試、可靠性測(cè)試和效率測(cè)試等)。5.設(shè)計(jì)小結(jié)(包括)課程設(shè)計(jì)題目:目標(biāo) 本軟件是一個(gè)實(shí)用聊天程序。項(xiàng)目要實(shí)現(xiàn)以下目標(biāo):1)允許用戶注冊(cè);2)允許注冊(cè)用戶登錄聊天室;3)登錄用戶之間進(jìn)行聊天文件傳輸;4)用戶可以更改背景音樂;用戶信息的保存本軟件只是一個(gè)聊天室程序,所以用戶只要打開程序就可以登錄。本軟件使用文本文件或者數(shù)據(jù)庫(kù)來(lái)保存用戶數(shù)據(jù)。需求分析對(duì)功能的規(guī)定1.系統(tǒng)的用例圖(對(duì)注冊(cè)用戶)2.用戶注冊(cè)過程3.用戶登錄過程對(duì)性能的規(guī)定 由于本軟件只是一個(gè)聊天室程序,程序只是提供用戶之間的聊天和文件傳輸,故對(duì)網(wǎng)絡(luò)傳輸數(shù)據(jù)要求不是很高,只要正常的傳輸速度就可以了。需要管理的數(shù)據(jù)用戶的信息如下:用戶名,密碼,頭像,電子郵件,地址。4開發(fā)環(huán)境 本軟件采用Java語(yǔ)言編寫,Java語(yǔ)言是一種跨平臺(tái)的編程語(yǔ)言,所以本軟件對(duì)操作系統(tǒng)沒有特別的要求。而網(wǎng)絡(luò)傳輸方面采用TCP/IP網(wǎng)絡(luò)傳輸協(xié)議或者是RMI??傮w設(shè)計(jì)思路系統(tǒng)主要包含兩個(gè)java文件,Server.java和UserLogin.java圖形界面主要通過\\org\netbeans\lib\awtextra目錄下的兩個(gè)類:AbsoluteConstraints.class和AbsoluteLayout.class來(lái)控制。啟動(dòng)系統(tǒng)通過依次點(diǎn)擊批處理文件:1.bat,2.batServer.java中包含:其中Server類又包括如下類和方法:UserLogin.java包括:其中UserLogin類又包括如下類和方法:而UserLoginWindow類又包含如下事件和方法:系統(tǒng)為C/S模式。首先根據(jù)功能模塊將系統(tǒng)分為兩部分:服務(wù)端(Server)和客戶端(UserLogin)。然后構(gòu)造可以序列化的User類,這是本系統(tǒng)的對(duì)象。根據(jù)需求功能細(xì)分類和方法。建立文件和網(wǎng)絡(luò)的傳輸通信。實(shí)現(xiàn)聊天室的基本功能具體設(shè)計(jì)代碼://server.javaimportjavax.swing.*;importjava.awt.*;importjava.awt.event.*;import.*;importjava.io.*;importjava.util.*;//用戶信息classUserimplementsSerializable{ StringUserName; StringUserPassword; StringUserHeadPhoto; StringUserEmail; StringUserAge; StringSinkName; StringFileName; byteFileData[]; StringMessage;StringCommand; VectorvectorUserList;}publicclassServerextendsJFrame{staticVectorvectorStreamToUser=newVector(1,1); staticVectorvectorUserList=newVector(1,1); privateServerSocketserverSocket; privateJMenuItemaboutMenuItem;privateJMenucommandMenu;privateJMenufileMenu;privateJMenuhelpMenu;privateJMenuItemhelpMenuItem;privateJLabeljLabel1;privateJLabeljLabel2;privateJLabeljLabel3;privateJLabeljLabel4;privateJLabeljLabel5;privateJListjList1;privateJPaneljPanel1;privateJScrollPanejScrollPane1;privateJMenuItemwarnMenuItem;privateJMenuItemkickMenuItem;privateJMenuBarmenuBar;privateJMenuItemmsgMenuItem;privateJMenuItemstartMenuItem;privateJMenuItemstopMenuItem;//右鍵事件菜單privatestaticJPopupMenumenu;privateStringBlackList;//建立server框架publicServer(){initComponents();}privatevoidinitComponents(){jPanel1=newJPanel();jLabel2=newJLabel();jLabel3=newJLabel();jLabel4=newJLabel();jLabel5=newJLabel();jScrollPane1=newJScrollPane();jList1=newJList();jLabel1=newJLabel();menuBar=newJMenuBar();fileMenu=newJMenu();startMenuItem=newJMenuItem();stopMenuItem=newJMenuItem();commandMenu=newJMenu();warnMenuItem=newJMenuItem();kickMenuItem=newJMenuItem();msgMenuItem=newJMenuItem();helpMenu=newJMenu();helpMenuItem=newJMenuItem();aboutMenuItem=newJMenuItem();getContentPane().setLayout(newbeans.lib.awtextra.AbsoluteLayout());setTitle("DaoDao2009服務(wù)器端");setResizable(false);//用戶不能手動(dòng)調(diào)整列寬度。addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevt){exitForm(evt);}});jPanel1.setLayout(newbeans.lib.awtextra.AbsoluteLayout());try{ //建立InetAddress對(duì)象InetAddressintel=InetAddress.getLocalHost();StringSerName=intel.getHostName();StringSerIp=intel.getHostAddress();jLabel2.setText("服務(wù)器名:"+SerName);//顯示服務(wù)器端主機(jī)名jLabel3.setText("服務(wù)器IP:"+SerIp);//顯示服務(wù)器端IP}catch(UnknownHostExceptione2){ System.out.println("發(fā)生錯(cuò)誤!");}jLabel2.setForeground(newColor(255,0,0));jLabel2.setFont(newFont("宋體",0,14));jPanel1.add(jLabel2,newbeans.lib.awtextra.AbsoluteConstraints(20,10,-1,-1));jLabel3.setForeground(newColor(255,0,0));jLabel3.setFont(newFont("宋體",0,14));jPanel1.add(jLabel3,newbeans.lib.awtextra.AbsoluteConstraints(20,30,-1,-1));jLabel4.setText("服務(wù)器狀態(tài):未啟動(dòng)");jLabel4.setForeground(newColor(255,255,0));jLabel4.setFont(newFont("宋體",0,14));jPanel1.add(jLabel4,newbeans.lib.awtextra.AbsoluteConstraints(20,240,-1,-1));jLabel5.setText("共有:0個(gè)客戶在線");jLabel5.setForeground(newColor(255,255,51));jLabel5.setFont(newFont("宋體",0,14));jPanel1.add(jLabel5,newbeans.lib.awtextra.AbsoluteConstraints(250,240,-1,-1));menu=newJPopupMenu();//右鍵事件菜單 JMenuItemAdmonition=newJMenuItem("警告"); Admonition.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEvente) { intstate=JOptionPane.showOptionDialog(null,"確實(shí)要警告用戶"+BlackList+"嗎?","確認(rèn)",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,null,null); if(state==JOptionPane.OK_OPTION) { warnMenuItemActionPerformed(e); } } }); JMenuItemDelete=newJMenuItem("刪除"); Delete.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEvente) { intstate=JOptionPane.showOptionDialog(null,"確實(shí)要?jiǎng)h除用戶"+BlackList+"嗎?","確認(rèn)",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,null,null); if(state==JOptionPane.OK_OPTION) { kickMenuItemActionPerformed(e); } } }); menu.add(Admonition); menu.add(Delete);jList1.setModel(newAbstractListModel(){String[]strings={};publicintgetSize(){returnstrings.length;}publicObjectgetElementAt(inti){returnstrings[i];}});jList1.setFont(newFont("Dialog",Font.BOLD|Font.ITALIC,16));jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);jList1.addMouseListener(newMouseAdapter() { publicvoidmousePressed(MouseEventevt) { if(evt.getClickCount()==1) { BlackList=(String)jList1.getSelectedValue(); } } publicvoidmouseReleased(MouseEventevt) { if(evt.isPopupTrigger()) { menu.show(evt.getComponent(),evt.getX(),evt.getY()); } } });jScrollPane1.setViewportView(jList1);jPanel1.add(jScrollPane1,newbeans.lib.awtextra.AbsoluteConstraints(20,45,362,190));jLabel1.setIcon(newImageIcon("img\\Server.jpg"));jPanel1.add(jLabel1,newbeans.lib.awtextra.AbsoluteConstraints(0,-10,-1,-1));getContentPane().add(jPanel1,newbeans.lib.awtextra.AbsoluteConstraints(0,0,400,270));menuBar.setBorder(null);menuBar.setBackground(newColor(0,0,0));fileMenu.setForeground(newColor(255,255,255));fileMenu.setText("文件");fileMenu.setBackground(newColor(0,0,0));fileMenu.setFont(newFont("宋體",0,14));startMenuItem.setForeground(newColor(255,255,255));startMenuItem.setFont(newFont("宋體",0,14));startMenuItem.setText("啟動(dòng)");startMenuItem.setBackground(newColor(0,0,0));startMenuItem.setBorder(null);startMenuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){startMenuItemActionPerformed(evt);}});fileMenu.add(startMenuItem);stopMenuItem.setForeground(newColor(255,255,255));stopMenuItem.setFont(newFont("宋體",0,14));stopMenuItem.setText("結(jié)束");stopMenuItem.setBackground(newColor(0,0,0));stopMenuItem.setBorder(null);stopMenuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){stopMenuItemActionPerformed(evt);}});fileMenu.add(stopMenuItem);menuBar.add(fileMenu);commandMenu.setForeground(newColor(255,255,255));commandMenu.setText("命令");commandMenu.setBackground(newColor(0,0,0));commandMenu.setFont(newFont("宋體",0,14));msgMenuItem.setForeground(newColor(255,255,255));msgMenuItem.setFont(newFont("宋體",0,14));msgMenuItem.setText("公告");msgMenuItem.setBackground(newColor(0,0,0));msgMenuItem.setBorder(null);msgMenuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){msgMenuItemActionPerformed(evt);}});commandMenu.add(msgMenuItem);warnMenuItem.setForeground(newColor(255,255,255));warnMenuItem.setFont(newFont("宋體",0,14));warnMenuItem.setText("警告");warnMenuItem.setBackground(newColor(0,0,0));warnMenuItem.setBorder(null);warnMenuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){warnMenuItemActionPerformed(evt);}});commandMenu.add(warnMenuItem);kickMenuItem.setForeground(newColor(255,255,255));kickMenuItem.setFont(newFont("宋體",0,14));kickMenuItem.setText("踢人");kickMenuItem.setBackground(newColor(0,0,0));kickMenuItem.setBorder(null);kickMenuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){kickMenuItemActionPerformed(evt);}});commandMenu.add(kickMenuItem);menuBar.add(commandMenu);helpMenu.setForeground(newColor(255,255,255));helpMenu.setText("幫助");helpMenu.setBackground(newColor(0,0,0));helpMenu.setFont(newFont("宋體",0,14));helpMenu.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){helpMenuActionPerformed(evt);}});helpMenuItem.setForeground(newColor(255,255,255));helpMenuItem.setFont(newFont("宋體",0,14));helpMenuItem.setText("幫助主題");helpMenuItem.setBackground(newColor(0,0,0));helpMenuItem.setBorder(null);helpMenuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){helpMenuItemActionPerformed(evt);}});helpMenu.add(helpMenuItem);aboutMenuItem.setForeground(newColor(255,255,255));aboutMenuItem.setFont(newFont("宋體",0,14));aboutMenuItem.setText("關(guān)于");aboutMenuItem.setBackground(newColor(0,0,0));aboutMenuItem.setBorder(null);aboutMenuItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){aboutMenuItemActionPerformed(evt);}});helpMenu.add(aboutMenuItem);menuBar.add(helpMenu);setJMenuBar(menuBar);pack();}publicstaticvoidmain(Stringargs[])//主函數(shù){newServer().show();}classconnectionextendsThread { privateUserfromUserMessage=newUser(); private UsertoUserMessage=newUser(); privateSocketnetUser; privateObjectInputStreamstreamFromUser; privateObjectOutputStreamstreamToUser; privatebooleanexistName,existPassword; privateStringonlineNumber="Immovability";//判斷在線人數(shù)的變化 publicconnection(SocketUserSocket) { netUser=UserSocket; try { streamFromUser=newObjectInputStream(netUser.getInputStream()); streamToUser=newObjectOutputStream(netUser.getOutputStream()); } catch(IOExceptione) { try { netUser.close(); } catch(IOExceptione1) { System.out.print("Unabletosetupstream"+e1); return; } } this.start(); } publicbooleanvalidateName(Stringname) { booleanvalidate; Stringrecode,message; RandomAccessFilelogfile; FileInputStreamfsread; InputStreamReaderinputread; BufferedReaderbfread; validate=false; try { fsread=newFileInputStream("User.txt"); inputread=newInputStreamReader(fsread); bfread=newBufferedReader(inputread); while((recode=bfread.readLine())!=null)//驗(yàn)證用戶信息 { StringTokenizerst=newStringTokenizer(recode,":"); while(st.hasMoreTokens()) { message=st.nextToken(); if(message.equals(name)) { validate=true; break; } break; } } } catch(FileNotFoundExceptione) { System.out.println("Thespecifiedfiledoesnotexist"); } catch(IOExceptione) { System.out.println("ErrorperformingIOOperation"); } returnvalidate; } publicbooleanvalidatePassword(Stringpassword) { booleanvalidate; Stringrecode,message; RandomAccessFilelogfile; FileInputStreamfsread; InputStreamReaderinputread; BufferedReaderbfread; validate=false; try { fsread=newFileInputStream("User.txt"); inputread=newInputStreamReader(fsread); bfread=newBufferedReader(inputread); while((recode=bfread.readLine())!=null)//驗(yàn)證用戶信息 { StringTokenizerst=newStringTokenizer(recode,":"); while(st.hasMoreTokens()) { message=st.nextToken(); message=st.nextToken(); if(message.equals(password)) { validate=true; break; } break; } } } catch(FileNotFoundExceptione) { System.out.println("Thespecifiedfiledoesnotexist"); } catch(IOExceptione) { System.out.println("ErrorperformingIOOperation"); } returnvalidate; } publicvoidrun() { try { for(;;) { StringButtonMessage; fromUserMessage=(User)streamFromUser.readObject();//讀用戶信息 ButtonMessage=fromUserMessage.Command;//讀按鈕信息 existName=validateName(fromUserMessage.UserName);//驗(yàn)證用戶名 existPassword=validatePassword(fromUserMessage.UserPassword);//驗(yàn)證用戶密碼 if(ButtonMessage.equals("Login")) { Login(); } if(ButtonMessage.equals("Enter")) { Enter(); } if(ButtonMessage.equals("Leave")) { Leave(); } if(!onlineNumber.equals("Immovability")) { Updating(fromUserMessage.UserName); } if(ButtonMessage.equals("PublicChat")||ButtonMessage.equals("PrivateChat")) { Message(); } if(ButtonMessage.equals("Request")||ButtonMessage.equals("FileCopy")) { toSinkMessage(); } if(ButtonMessage.equals("Agree")||ButtonMessage.equals("Refuse")||ButtonMessage.equals("Succeed")) { toUserMessage(); } } } catch(IOExceptione){} catch(ClassNotFoundExceptione){} try { streamFromUser.close(); streamToUser.close(); netUser.close(); } catch(IOExceptione){} } publicvoidLogin()//注冊(cè)請(qǐng)求處理 { try { if(existName) { toUserMessage.Command="Repeat"; streamToUser.writeObject((User)toUserMessage);//返回操作信息 streamToUser.flush(); } else { //加入新用戶信息 RandomAccessFileUserObj=newRandomAccessFile("User.txt","rw"); UserObj.seek(UserObj.length()); UserObj.writeBytes(fromUserMessage.UserName+":"+fromUserMessage.UserPassword+":" +fromUserMessage.UserHeadPhoto+":"+fromUserMessage.UserEmail+":"+fromUserMessage.UserAge+"\n"); onlineNumber="add"; toUserMessage.Command="Login"; toUserMessage.UserName=fromUserMessage.UserName; toUserMessage.Message="系統(tǒng)消息:大家歡迎"+fromUserMessage.UserName+"加入我們的DaoDao聊天室?。?!"+"\n"; Server.vectorUserList.addElement(fromUserMessage.UserName);//加入用戶到用戶在線列表 toUserMessage.vectorUserList=Server.vectorUserList; streamToUser.writeObject((User)toUserMessage);//返回用戶在線列表和操作信息 streamToUser.flush(); Server.vectorStreamToUser.addElement(streamToUser);//存儲(chǔ)該對(duì)象的輸出流 jLabel5.setText("共有:"+Server.vectorUserList.size()+"個(gè)客戶在線");//服務(wù)器刷新 jList1.setListData(Server.vectorUserList); } } catch(IOExceptione){} } publicvoidEnter()//登陸請(qǐng)求處理 { try { if(existName&&existPassword) { onlineNumber="add"; toUserMessage.Command="Enter"; toUserMessage.UserName=fromUserMessage.UserName; toUserMessage.Message="系統(tǒng)消息:大家歡迎"+fromUserMessage.UserName+"加入我們的歡樂聊天室!?。?+"\n"; Server.vectorUserList.addElement(fromUserMessage.UserName);//加入用戶到用戶在線列表 toUserMessage.vectorUserList=Server.vectorUserList; streamToUser.writeObject((User)toUserMessage);//返回用戶在線列表和操作信息 streamToUser.flush(); Server.vectorStreamToUser.addElement(streamToUser);//存儲(chǔ)該對(duì)象的輸出流 jLabel5.setText("共有:"+Server.vectorUserList.size()+"個(gè)客戶在線");//服務(wù)器刷新 jList1.setListData(Server.vectorUserList); } else { toUserMessage.Command="Error"; streamToUser.writeObject((User)toUserMessage);//返回操作信息 streamToUser.flush(); } } catch(IOExceptione){} } publicvoidLeave()//離開請(qǐng)求處理 { Stringname=fromUserMessage.UserName; Server.vectorStreamToUser.removeElementAt(Server.vectorUserList.indexOf(name)); Server.vectorUserList.removeElement(name); onlineNumber="lessen"; Updating(name); } publicvoidUpdating(Stringname)//群發(fā)新用戶登陸通知,更新用戶在線列表和對(duì)像框 { try { if(onlineNumber.equals("add")) { UsertoUserMessage=newUser(); toUserMessage.Command="add"; toUserMessage.UserName=name; toUserMessage.Message="系統(tǒng)消息:大家歡迎"+name+"加入我們的歡樂聊天室?。?!"+"\n"; for(inti=1;i<Server.vectorStreamToUser.size();i++) { ObjectOutputStreamnewUserStream; newUserStream=(ObjectOutputStream)Server.vectorStreamToUser.elementAt(i-1); newUserStream.writeObject((User)toUserMessage); newUserStream.flush(); } } if(onlineNumber.equalsIgnoreCase("lessen")) { UsertoUserMessage=newUser(); toUserMessage.Command="lessen"; toUserMessage.UserName=name; toUserMessage.Message="系統(tǒng)消息:"+name+"悄悄的走了!?。?+"\n"; for(inti=0;i<Server.vectorStreamToUser.size();i++) { ObjectOutputStreamnewUserStream; newUserStream=(ObjectOutputStream)Server.vectorStreamToUser.elementAt(i); newUserStream.writeObject((User)toUserMessage); newUserStream.flush(); } } jLabel5.setText("共有:"+Server.vectorUserList.size()+"個(gè)客戶在線");//服務(wù)器刷新 jList1.setListData(Server.vectorUserList); onlineNumber="Immovability"; } catch(IOExceptione){} } publicvoidMessage()//聊天信息發(fā)送 { UsertoUserMessage=newUser(); try { toUserMessage.Command="Message"; toUserMessage.Message=fromUserMessage.Message; if(fromUserMessage.Command.equals("PublicChat")) { for(inti=0;i<Server.vectorUserList.size();i++) { if(!(fromUserMessage.UserName.equals(Server.vectorUserList.elementAt(i)))) { streamToUser=(ObjectOutputStream)Server.vectorStreamToUser.elementAt(i); streamToUser.writeObject((User)toUserMessage); streamToUser.flush(); } } } if(fromUserMessage.Command.equals("PrivateChat")) { for(inti=0;i<Server.vectorUserList.size();i++) { if(fromUserMessage.UserName.equals(Server.vectorUserList.elementAt(i))) { streamToUser=(ObjectOutputStream)Server.vectorStreamToUser.elementAt(i); streamToUser.writeObject((User)toUserMessage); streamToUser.flush(); } } } } catch(IOExceptione){} } //文件傳輸信息 publicvoidtoSinkMessage() { try { streamToUser=(ObjectOutputStream)Server.vectorStreamToUser.elementAt(Server.vectorUserList.indexOf(fromUserMessage.SinkName)); streamToUser.writeObject(fromUserMessage); streamToUser.flush(); } catch(IOExceptione){} } publicvoidtoUserMessage() { try { streamToUser=(ObjectOutputStream)Server.vectorStreamToUser.elementAt(Server.vectorUserList.indexOf(fromUserMessage.UserName)); streamToUser.writeObject(fromUserMessage); streamToUser.flush(); } catch(IOExceptione){} } } privatevoidstartMenuItemActionPerformed(ActionEventevt)//啟動(dòng)事件{newAppServer();jLabel4.setText("服務(wù)器狀態(tài):已啟動(dòng)");}classAppServerextendsThread { publicAppServer() { try { serverSocket=newServerSocket(1001); //建立InetAddress對(duì)象 InetAddressintel=InetAddress.getLocalHost(); StringSerName=intel.getHostName(); StringSerIp=intel.getHostAddress(); System.out.println("服務(wù)器名:"+SerName); System.out.println("服務(wù)器IP:"+SerIp); } catch(IOExceptione) { System.out.println("Couldnotstartserver"+"."+e); } System.out.println("Serverstart"); this.start(); } publicvoidrun() { try { while(true) { SocketUserSocket=serverSocket.accept(); connectioncon=newconnection(UserSocket); } } catch(IOExceptione) { System.out.println("Notlistener"+"."+e); } } }privatevoidstopMenuItemActionPerformed(ActionEventevt)//停止事件{ jLabel4.setText("服務(wù)器狀態(tài):即將關(guān)閉"); StringMessage="各位用戶請(qǐng)注意,服務(wù)器5秒鐘后將關(guān)閉!敬請(qǐng)?jiān)彛?; try { UsertoUserMessage=newUser(); toUserMessage.Command="SystemMessage"; toUserMessage.Message=Message; for(inti=0;i<Server.vectorUserList.size();i++) { ObjectOutputStreamstreamToUser=(ObjectOutputStream)Server.vectorStreamToUser.elementAt(i); streamToUser.writeObject((User)toUserMessage); streamToUser.flush(); } } catch(IOExceptione){} try{ Thread.sleep(5000);}catch(InterruptedExceptione){} System.exit(0);}privatevoidmsgMenuItemActionPerformed(ActionEventevt)//公告事件{ try { UsertoUserMessage=newUser(); StringMesage=JOptionPane.showInputDialog(null,"請(qǐng)輸入公告內(nèi)容:","系統(tǒng)消息",JOptionPane.INFORMATION_MESSAGE); toUserMessage.Message="系統(tǒng)消息:"+Mesage+"\n"; toUserMessage.Command="SystemMessage"; for(inti=0;i<Server.vectorUserList.size();i++) { ObjectOutputStreamstreamToUser=(ObjectOutputStream)Server.vectorStreamToUser.elementAt(i); streamToUser.writeObject((User)toUserMessage); streamToUser.flush(); }}catch(IOExceptione){} }privatevoidwarnMenuItemActionPerformed(ActionEventevt)//警告事件{ Objectobj=evt.getSource(); try { if(obj==warnMenuItem) { BlackList=JOptionPane.showInputDialog(null,"請(qǐng)輸入將警告的用戶名:","系統(tǒng)消息",JOptionPane.INFORMATION_MESSAGE);}UsertoUserMessage=newUser(); toUserMessage.Message="警告:請(qǐng)用文明用語(yǔ)!否則你將被強(qiáng)制退出!\n"; toUserMessage.Command="SystemMessage"; ObjectOutputStreamstreamToUser=(ObjectOutputStream)Server.vectorStreamToUser.elementAt(Server.vectorUserList.indexOf(BlackList)); streamToUser.writeObject((User)toUserMessage); streamToUser.flush(); BlackList="";}catch(IOExceptione){} }privatevoidkickMenuItemActionPerformed(ActionEventevt)//踢人事件{ Objectobj=evt.getSource(); try { if(obj==kickMenuItem) { BlackList=JOptionPane.showInputDialog(null,"請(qǐng)輸入將強(qiáng)制退出的用戶名:","系統(tǒng)消息",JOptionPane.INFORMATION_MESSAGE); } UsertoUserMessage=newUser(); for(inti=0;i<Server.vectorUserList.size();i++){ if(BlackList.equals(Server.vectorUserList.elementAt(i))) { toUserMessage.Command="Kick"; toUserMessage.Message="系統(tǒng)消息:對(duì)不起!你已被強(qiáng)制退出!\n"; } else { toUserMessage.UserName=BlackList; toUserMessage.Command="lessen"; toUserMessage.Message="系統(tǒng)消息:"+BlackList+"已被強(qiáng)制退出!\n";} ObjectOutputStreamstreamToUser=(Ob

溫馨提示

  • 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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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)論