Java-SE实验报告9

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1实验九:综合性实验班级:姓名:学号:实验目的:通过一个网络聊天程序设计,综合应用多种JavaAPI,使学生掌握设计一个综合性的应用程序的方法。实验内容:GUI设计;数据库操作实现;网络通信机制设计。实验步骤:1.实验步骤01.完成GUI设计代码的编写、运行02.数据库的操作与实现03.网络通信机制设计与调试2.实验源代码01.聊天服务器源代码(ChatServer.java)importjava.awt.event.*;importjava.io.*;importjava.net.*;importjava.util.*;importjavax.swing.*;importjavax.swing.tree.*;publicclassChatServerextendsJFrameimplementsActionListener{privateJSplitPanesplitPaneV,splitPaneH;privateJScrollPanespCenter,spRight;privateJPanelpdown;privateJTextAreatxtContent;privateJLabellblSend;privateJTextFieldtxtSend;privateJButtonbtnSend;privateDefaultMutableTreeNoderoot;privateDefaultTreeModelmodel;privateJTreetree;//消息服务套接字privateServerSocketmsgServerSocket;//用户服务套接字privateServerSocketuserServerSocket;privateArrayListSocketmsgSocket=newArrayListSocket();privateArrayListPrintWriterprintWriter=new2ArrayListPrintWriter();privateArrayListBufferedReaderbufferedReader=newArrayListBufferedReader();privateLinkedListStringmsgList=newLinkedListString();privateArrayListSocketuserSocket=newArrayListSocket();privateArrayListPrintWriterprintUser=newArrayListPrintWriter();privateArrayListObjectInputStreamreadUser=newArrayListObjectInputStream();privateArrayListUseruserList=newArrayListUser();//定义一个全局布尔变量,控制线程是否运行,初始值为trueprivatestaticbooleanisRun=true;publicChatServer()throwsException{super(服务器);txtContent=newJTextArea();txtContent.setEditable(false);spCenter=newJScrollPane(txtContent);pdown=newJPanel();lblSend=newJLabel(系统消息:);txtSend=newJTextField(20);btnSend=newJButton(发送);pdown.add(lblSend);pdown.add(txtSend);pdown.add(btnSend);btnSend.addActionListener(this);root=newDefaultMutableTreeNode(目前在线用户(用户名:昵称));model=newDefaultTreeModel(root);tree=newJTree(model);spRight=newJScrollPane(tree);splitPaneV=newJSplitPane(JSplitPane.VERTICAL_SPLIT,spCenter,pdown);splitPaneV.setDividerLocation(420);splitPaneV.setResizeWeight(1);splitPaneH=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitPaneV,spRight);splitPaneH.setDividerLocation(400);splitPaneH.setResizeWeight(1);this.add(splitPaneH);this.setSize(600,500);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.addWindowListener(newWindowEventHandle());msgServerSocket=newServerSocket(Integer.parseInt(Config3.getValue(msgport)));userServerSocket=newServerSocket(Integer.parseInt(Config.getValue(userport)));//启动接收用户套接字的线程newAcceptUserSocketThread().start();//启动接收消息套接字的线程newAcceptMsgSocketThread().start();//启动发送消息的线程newSendMessageThread().start();}//处理事件的方法publicvoidactionPerformed(ActionEvente){if(e.getSource()==btnSend){Stringstr=txtSend.getText();if(!str.equals()){msgList.addFirst(系统消息:+str);}}}//接收客户端发送的有关用户套接字的线程classAcceptUserSocketThreadextendsThread{publicvoidrun(){while(isRun){try{Socketsocket=userServerSocket.accept();ObjectInputStreamreaderFromClient=newObjectInputStream(socket.getInputStream());PrintWriterprintToClient=newPrintWriter(socket.getOutputStream());printUser.add(printToClient);Useruser=(User)readerFromClient.readObject();if(user!=null){userList.add(user);//在根节点下添加一新节点root.add(newDefaultMutableTreeNode(user.userName+:+user.nickName));//刷新树tree.setModel(model);tree.updateUI();//调用sendUserListToClient()方法向所有客户端发送目前所有用户信息sendUserListToClient();4}}catch(Exceptione){e.printStackTrace();}}}}//向所有客户端发送目前所有用户信息privatevoidsendUserListToClient(){Stringpeople=;for(inti=0;iuserList.size();i++){people+=&+userList.get(i).nickName+&+userList.get(i).headImg;}for(inti=0;iprintUser.size();i++){if(printUser.get(i)!=null){try{printUser.get(i).println(people);printUser.get(i).flush();}catch(Exceptione){e.printStackTrace();}}}}//接收客户端发送的有关消息套接字的线程classAcceptMsgSocketThreadextendsThread{publicvoidrun(){while(isRun){try{Socketsocket=msgServerSocket.accept();BufferedReaderbufferedFromClient=newBufferedReader(newInputStreamReader(socket.getInputStream()));PrintWriterprintWriterToClient=newPrintWriter(socket.getOutputStream());printWriter.add(printWriterToClient);bufferedReader.add(bufferedFromClient);//启动接收此客户发送聊天信息的线程newGetMessageThread(bufferedFromClient).start();}catch(Exceptione){e.printStackTrace();}5}}}//接收客户发送聊天消息的线程classGetMessageThreadextendsThread{privateStringstringFromClient=null;privateBufferedReaderbufferedReaderFromClient;publicGetMessageThread(BufferedReaderbuffer)throwsException{bufferedReaderFromClient=buffer;}publicvoidrun(){try{do{stringFromClient=bufferedReaderFromClient.readLine();msgList.add(stringFromClient);}while(!stringFromClient.toLowerCase().endsWith(bye));intindex=bufferedReader.indexOf(bufferedReaderFromClient);bufferedReader.remove(index);printWriter.remove(index);}catch(Exceptione){e.printStackTrace();}}}//向所有客户端发送消息的线程classSendMessageThreadextendsThread{publicSendMessageThread()throwsException{super();}publicvoidrun(){while(isRun){try{Strings=null;if(!msgList.isEmpty()){s=(String)msgList.removeLast();txtContent.append(s+\n);for(inti=0;iprintWriter.size();i++){if(printWriter.get(i)!=null){printWriter.get(i).println(s);printWriter.get(i).flush();}6}}else{sleep(100);}}catch(Exceptione){e.printStackTrace();}}}}

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功